积极答复者
dataGridView从数据库读取数据

问题
答案
-
下面是之前写的绑定 List<T> 的示例。列是自己指定的。你参考一下。
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace X.WinFormsApp { /// <summary> /// 标题:DataGridView 简单绑定 List 对像 /// 日期:2009-05-14 /// </summary> public partial class X200905142324 : FormBase { public X200905142324() { InitializeComponent(); this.BindGrid(); } private void BindGrid() { List<XList> fXList = new List<XList>(); fXList.Add(new XList(1, "Rol", true)); fXList.Add(new XList(2, "Chole", false)); fXList.Add(new XList(3, "John", true)); fXList.Add(new XList(4, "X.X.Y", false)); this.xGrid1.AutoGenerateColumns = false; DataGridViewColumn fColumn1 = new DataGridViewColumn(new DataGridViewTextBoxCell()); fColumn1.HeaderText = "Id"; fColumn1.DataPropertyName = "Id"; DataGridViewColumn fColumn2 = new DataGridViewColumn(new DataGridViewTextBoxCell()); fColumn2.HeaderText = "Name"; fColumn2.DataPropertyName = "Name"; //DataGridViewColumn fColumn3 = new DataGridViewColumn(new DataGridViewCheckBoxCell()); //fColumn3.HeaderText = "IsDeleted"; //fColumn3.DataPropertyName = "IsDeleted"; // 这里改变 Add 顺序将会改变列显示顺序 this.xGrid1.Columns.Add(fColumn1); this.xGrid1.Columns.Add(fColumn2); //this.xGrid1.Columns.Add(fColumn3); this.xGrid1.DataSource = fXList; } } public class XList { private int fId; private string fName; private bool fIsDeleted; public int Id { get { return fId; } set { fId = value; } } public string Name { get { return fName; } set { fName = value; } } public bool IsDeleted { get { return fIsDeleted; } set { fIsDeleted = value; } } public XList(int fId, string fName, bool fIsDeleted) { this.fId = fId; this.fName = fName; this.fIsDeleted = fIsDeleted; } } }
知识改变命运,奋斗成就人生!- 已标记为答案 茉莉花 2009年12月10日 8:07
全部回复
-
下面是之前写的绑定 List<T> 的示例。列是自己指定的。你参考一下。
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace X.WinFormsApp { /// <summary> /// 标题:DataGridView 简单绑定 List 对像 /// 日期:2009-05-14 /// </summary> public partial class X200905142324 : FormBase { public X200905142324() { InitializeComponent(); this.BindGrid(); } private void BindGrid() { List<XList> fXList = new List<XList>(); fXList.Add(new XList(1, "Rol", true)); fXList.Add(new XList(2, "Chole", false)); fXList.Add(new XList(3, "John", true)); fXList.Add(new XList(4, "X.X.Y", false)); this.xGrid1.AutoGenerateColumns = false; DataGridViewColumn fColumn1 = new DataGridViewColumn(new DataGridViewTextBoxCell()); fColumn1.HeaderText = "Id"; fColumn1.DataPropertyName = "Id"; DataGridViewColumn fColumn2 = new DataGridViewColumn(new DataGridViewTextBoxCell()); fColumn2.HeaderText = "Name"; fColumn2.DataPropertyName = "Name"; //DataGridViewColumn fColumn3 = new DataGridViewColumn(new DataGridViewCheckBoxCell()); //fColumn3.HeaderText = "IsDeleted"; //fColumn3.DataPropertyName = "IsDeleted"; // 这里改变 Add 顺序将会改变列显示顺序 this.xGrid1.Columns.Add(fColumn1); this.xGrid1.Columns.Add(fColumn2); //this.xGrid1.Columns.Add(fColumn3); this.xGrid1.DataSource = fXList; } } public class XList { private int fId; private string fName; private bool fIsDeleted; public int Id { get { return fId; } set { fId = value; } } public string Name { get { return fName; } set { fName = value; } } public bool IsDeleted { get { return fIsDeleted; } set { fIsDeleted = value; } } public XList(int fId, string fName, bool fIsDeleted) { this.fId = fId; this.fName = fName; this.fIsDeleted = fIsDeleted; } } }
知识改变命运,奋斗成就人生!- 已标记为答案 茉莉花 2009年12月10日 8:07