本文介绍了如何手动将datagridview列绑定到通用列表C#中的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我正在开发一个winform应用程序,使用的语言是C#并使用sqlserver作为后端。我的应用程序的UI有一个显示值的datagrid视图,我使用业务对象,业务层,数据层设计和实现了应用程序,使用选择查询访问数据库中UI值中按钮的点击事件,结果通过图层传递,最后在UI中作为列表< person>提供。我能够绑定列表< person>使用自动生成到datagridview但是当我尝试通过创建列并将列表中的值分配给列来手动执行它时,数据网格视图中没有显示这些值。我通过不同的答案并搜索了答案但没有任何答案似乎工作,肯定是出于我的无知,任何人都可以指导我吗? 我尝试了什么: public class ContactPerson { List< contactperson> listCP = new 列表< contactperson>(); private string id; public string ID { get { return id; } set {id = value ; } } private string firstName; public string FirstName { get { return firstName; } set {firstName = value ; } } private string lastName; public string LastName { get { return lastName; } set {lastName = value ; } } private string mobileNo; public string MobileNo { 获取 {返回 mobileNo; } set {mobileNo = value ; } } private string altMobNo1; public string AltMobNo1 { get { return altMobNo1; } set {altMobNo1 = value ; } } private string altMobNo2; public string AltMobNo2 { get { return altMobNo2; } set {altMobNo2 = value ; } } private string emailid; public string Emailid { 获取 {返回 emailid; } set {emailid = value ; } } private string offNo; public string OffNo { get { return offNo; } set {offNo = value ; } } } private void btnShow_Click( object sender,EventArgs e) { BULContact obj = new BULContact(); /// / /// /自动绑定gridview(有效) /// / // dgvDisplay.AutoGenerateColumns = true; // var listCP = obj.DisplayBUContacts(); // dgvDisplay.DataSource = listCP; // 这是我想要使用的代码片段,需要进行一些更改..请指导 dgvDisplay.AutoGenerateColumns = false ; var listCP = obj.DisplayBUContacts(); dgvDisplay.DataSource = listCP; dgvDisplay.Columns.Add( FirstNamee, 名字); dgvDisplay.Columns.Add( LastNamee, 姓氏); dgvDisplay.Columns.Add( MobiNoDef, Mob NoDef); dgvDisplay.Columns.Add( AltMobiNo1, AltMob No1); dgvDisplay.Columns.Add( AltMobiNo2, AltMob No2); dgvDisplay.Columns.Add( OfficeeNo, Office No); dgvDisplay.Columns.Add( EmaillID, 电子邮件ID); foreach ( var item in listCP) { dgvDisplay.Columns [ FirstNamee ]。DataPropertyName = item.FirstName; dgvDisplay.Columns [ LastNamee]。DataPropertyName = item.LastName; dgvDisplay.Columns [ MobiNoDef]。DataPropertyName = item.MobileNo; dgvDisplay.Columns [ AltMobiNo1]。DataPropertyName = item.AltMobNo1; dgvDisplay.Columns [ AltMobiNo2]。DataPropertyName = item.AltMobNo2; dgvDisplay.Columns [ OfficeeNo]。DataPropertyName = item.OffNo; dgvDisplay.Columns [ EmaillID]。DataPropertyName = item.Emailid; } } 解决方案 I'm working on a winform application,language used is C# and uses sqlserver as backend.The UI of my application has a datagrid view which displays values,i designed and implemented the application using business object,business layer,Data layer,on click event of a button in the UI values in the database is accessed using select query and the result is passed thru the layers and finally it is available in the UI as a list<person>. am able to bind the list<person> to the datagridview with auto generate but when i try to do it manually by creating columns and assigning the values in the list to the columns the values are not being shown in the datagrid view.i gone thru different answers and googled for an answer but nothing seems to work,surely out of my ignorance,can anyone guide me???What I have tried:public class ContactPerson{List<contactperson> listCP = new List<contactperson>();private string id;public string Id{ get { return id; } set { id = value; }}private string firstName;public string FirstName{ get { return firstName; } set { firstName = value; }}private string lastName;public string LastName{ get { return lastName; } set { lastName = value; }}private string mobileNo;public string MobileNo{ get { return mobileNo; } set { mobileNo = value; }}private string altMobNo1;public string AltMobNo1{ get { return altMobNo1; } set { altMobNo1 = value; }}private string altMobNo2;public string AltMobNo2{ get { return altMobNo2; } set { altMobNo2 = value; }}private string emailid;public string Emailid{ get { return emailid; } set { emailid = value; }}private string offNo;public string OffNo{ get { return offNo; } set { offNo = value; }}}private void btnShow_Click(object sender, EventArgs e) { BULContact obj = new BULContact(); //// ////Autobinding gridview (works) //// //dgvDisplay.AutoGenerateColumns =true; //var listCP = obj.DisplayBUContacts(); //dgvDisplay.DataSource = listCP;//This is the piece of code i want to use, which needs some changes..Please guide dgvDisplay.AutoGenerateColumns = false; var listCP = obj.DisplayBUContacts(); dgvDisplay.DataSource = listCP; dgvDisplay.Columns.Add("FirstNamee", "First Name"); dgvDisplay.Columns.Add("LastNamee", "Last Name"); dgvDisplay.Columns.Add("MobiNoDef", "Mob NoDef"); dgvDisplay.Columns.Add("AltMobiNo1", "AltMob No1"); dgvDisplay.Columns.Add("AltMobiNo2", "AltMob No2"); dgvDisplay.Columns.Add("OfficeeNo", "Office No"); dgvDisplay.Columns.Add("EmaillID", "Email ID"); foreach (var item in listCP) { dgvDisplay.Columns["FirstNamee"].DataPropertyName = item.FirstName; dgvDisplay.Columns["LastNamee"].DataPropertyName = item.LastName; dgvDisplay.Columns["MobiNoDef"].DataPropertyName = item.MobileNo; dgvDisplay.Columns["AltMobiNo1"].DataPropertyName = item.AltMobNo1; dgvDisplay.Columns["AltMobiNo2"].DataPropertyName = item.AltMobNo2; dgvDisplay.Columns["OfficeeNo"].DataPropertyName = item.OffNo; dgvDisplay.Columns["EmaillID"].DataPropertyName = item.Emailid; } } 解决方案 这篇关于如何手动将datagridview列绑定到通用列表C#中的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-22 05:45