积极答复者
Winform的DataGridView问题

问题
-
最近碰到的问题,不知道该怎么解决。
DataGridView是依据什么来排列DataSource中的数据的列的顺序的?
最近在写个项目,架构和PetShop类似。
数据库中的表有三列,建了相应的实体类。
在窗体中放了一个DataGridView,Collection中添加了三列,分别对应了实体类中的属性名。
然后DataSource等于Bll层中从数据库读取的IList数据集合。
运行时发现第二列和第三列的顺序颠倒了,与我之前在DataGridView中添加列的集合的顺序不一样。
这是怎么回事?
本来Id列是在最前面隐藏的,取值时gridView.SelectedRows[0].Cell[0](象征性的)就应该取到,可现在需要gridView.SelectedRows[0].Cell[2]才能取到。也就是说绑定了Id的列跑到了第三列的位置。
如果更改了列的DisplayIndex,显示是没问题了,可取Id的值还是需要Cell[2]才能取到。
问题总结:
1,希望知道DataGridView依靠什么来给列的顺序排序的。
2,其实上面的如果明白了的话,第二个也就解决了。我想让我的DataGridView的显示&取值与我在DataGridView中之前添加好的列的顺序相同,该怎么做?
问题如果不清楚的话MSN: bengua007@hotmail.com
感谢各位了,比较急。
有人说,充满技术的生活枯燥无味.. 我笑他们不懂.因为只有技术才能充实生活.. 学习就像生活,都需要善于总结,才能发现问题,取得进步.. 生活中充满了数学算式与结构,只要我们善于观察和思考..- 已移动 Sheng Jiang 蒋晟Moderator 2009年5月14日 16:01 Windows表单类库问题 ([Loc]From:Visual C#)
答案
-
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; using System.Web.UI.WebControls; namespace X.WinFormsApp { /// <summary> /// 标题:DataGridView 简单绑定 List 对像 /// 日期:2009-05-14 /// </summary> public partial class X200905142324 : FormBase { public X200905142324() { InitializeComponent(); //this.OptionsDescription.Title = "DataGridView 简单绑定 List 对像"; 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年5月17日 14:09
-
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; using System.Web.UI.WebControls; namespace X.WinFormsApp { /// <summary> /// 标题:DataGridView 简单绑定 List 对像 /// 日期:2009-05-14 /// </summary> public partial class X200905142324 : FormBase { public X200905142324() { InitializeComponent(); //this.OptionsDescription.Title = "DataGridView 简单绑定 List 对像"; 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; XList f = (XList)this.xGrid1.Rows[0].DataBoundItem; // 不一定非得用索引来取,下面的方式更好 int fId = f.Id; string fName = f.Name; bool fIsDeleted = f.IsDeleted; } } 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年5月20日 14:00
全部回复
-
DataGridView中三列:
ID,绑定了Id Visible=false,
名称,绑定了Name,
是否已删除,绑定了IsDeleted.
Id,Name,IsDeleted都是实体类中的属性.
IList中包含着许多实体类的实例.
DataGridView的DataSource等于IList后,运行时显示两列的顺序是
"是否已删除 名称"
取得Id的值的Cell是 gridView.SelectedRows[0].Cells[2].
把名称列的DisplayIndex = 0,是否已删除的DisplayIndex = 1以后,
显示顺序是变了,可取得ID的值还是gridView.SelectedRows[0].Cells[2]..
如何设置DataGridView才能让我的显示和取值都按照我之前设置的列的顺序呢?
有人说,充满技术的生活枯燥无味.. 我笑他们不懂.因为只有技术才能充实生活.. 学习就像生活,都需要善于总结,才能发现问题,取得进步.. 生活中充满了数学算式与结构,只要我们善于观察和思考..- 已编辑 韩懿留-獨翏淚 2009年5月14日 13:34 回行咋没了呢?
-
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; using System.Web.UI.WebControls; namespace X.WinFormsApp { /// <summary> /// 标题:DataGridView 简单绑定 List 对像 /// 日期:2009-05-14 /// </summary> public partial class X200905142324 : FormBase { public X200905142324() { InitializeComponent(); //this.OptionsDescription.Title = "DataGridView 简单绑定 List 对像"; 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年5月17日 14:09
-
this.xGrid1.Columns.Add(fColumn1);
this.xGrid1.Columns.Add(fColumn2);
this.xGrid1.Columns.Add(fColumn3);
像这里。
我那个是AddRange的一个column的数组,那是vs自动生成的代码,AddRange不能规定顺序吗?
而且这个顺序好像和实体类的属性名相关,属性名改了后顺序就好了。
有人说,充满技术的生活枯燥无味.. 我笑他们不懂.因为只有技术才能充实生活.. 学习就像生活,都需要善于总结,才能发现问题,取得进步.. 生活中充满了数学算式与结构,只要我们善于观察和思考.. -
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; using System.Web.UI.WebControls; namespace X.WinFormsApp { /// <summary> /// 标题:DataGridView 简单绑定 List 对像 /// 日期:2009-05-14 /// </summary> public partial class X200905142324 : FormBase { public X200905142324() { InitializeComponent(); //this.OptionsDescription.Title = "DataGridView 简单绑定 List 对像"; 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; XList f = (XList)this.xGrid1.Rows[0].DataBoundItem; // 不一定非得用索引来取,下面的方式更好 int fId = f.Id; string fName = f.Name; bool fIsDeleted = f.IsDeleted; } } 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年5月20日 14:00