如何确定绑定到数据源的 datagridview 中显示列的顺序
有没有办法确定显示的列的顺序 将 datagridview 绑定到包含以下内容的数据源时 底层 IList ? 我认为有一个特定的属性用于此目的 但不记得它实际上是什么。
例如:
public void BindToGrid(IList<CustomClass> list)
{
_bindingSource.DataSource = list;
dataGridView1.DataSource = _bindingSource.DataSource;
}
类型绑定应该是这样的
class CustomClass
{
bool _selected = false;
//[DisplayOrder(0)]
public bool Selected
{
get { return _selected; }
set { _selected = value; }
}
string _name;
//[DisplayOrder(2)]
public string Name
{
get { return _name; }
set { _name = value; }
}
string _value;
//[DisplayOrder(1)]
public string Value
{
get { return _value; }
set { _value = value; }
}
}
编辑: 我想补充一点,我不想将列手动添加到设计器中的列列表中。我想尽可能保持动态。
Is there a way to determine the order of the columns displayed in
a datagridview when binding it to a datasource whitch contains an
underlying IList ?
I thought there was a specific property attribute for this purpose
but can't recall what it actually was.
eg:
public void BindToGrid(IList<CustomClass> list)
{
_bindingSource.DataSource = list;
dataGridView1.DataSource = _bindingSource.DataSource;
}
Type binded should be something like this
class CustomClass
{
bool _selected = false;
//[DisplayOrder(0)]
public bool Selected
{
get { return _selected; }
set { _selected = value; }
}
string _name;
//[DisplayOrder(2)]
public string Name
{
get { return _name; }
set { _name = value; }
}
string _value;
//[DisplayOrder(1)]
public string Value
{
get { return _value; }
set { _value = value; }
}
}
Edit:
I would like to add that I rather not want to add the columns manually to columns list in the designer. I'd like to keep this as dynamic as possible.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 DataGridView 中指定实际的列列表,而不是允许其自动数据绑定。您可以在 Visual Studio 的设计视图中通过选择控件并添加列来执行此操作。确保在每一列中指定应绑定到哪个属性。然后,您可以按照您想要的方式重新排列列以及进行其他自定义。
我认为
DisplayOrder
属性相对较新,并且可能在DataGridView
控件中不受支持。In the
DataGridView
specify an actual list of columns instead of allowing it to auto-databind. You can do this in Design View in Visual Studio by selecting the control and adding the columns. Make sure you specify in each column which property it should bind to. Then you can rearrange the columns any way you want as well as do other customizations.I think that the
DisplayOrder
attribute is relatively new and probably not supported in theDataGridView
control.DataGridView
中列的显示顺序由DataGridViewColumn
-s 的DisplayIndex
属性确定。您必须在网格的列上设置这些属性,才能更改它们的顺序。我也同意 Eilon 的回答:您可以自己创建列列表,而不是自动数据绑定,这样您就可以确定它们的显示顺序。
The display order of the columns in the
DataGridView
is determined by theDisplayIndex
properties of theDataGridViewColumn
-s. You would have to set these properties on the columns of the grid, in order to change their order.I also agree with Eilon's answer: you can create the list of the columns yourself, instead of auto-databinding, and that way you can determine the order in which they will be displayed.
我不确定这是否是 .Net 提供的功能,但如果您只是更改类中属性的顺序,网格将以相同的顺序呈现列。
下面的两个类将按照它们在类中键入的顺序呈现。奇怪的!!
I am not sure whether this is a functionality that .Net Offers, but if you just change the order of your properties in the class, the grid renders the columns in the same order.
The below two classes will render in the order they are typed in the class. Strange!!