如何在 C# 中将数据源设置为 Datagridview 控件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15811254/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set Datasource to a Datagridview Control in C#
提问by Kiran
This might look really simple, but I am not able to figure out how to do it. I am not an expert with Databinding in C#.
这可能看起来很简单,但我不知道如何去做。我不是 C# 中数据绑定的专家。
I have a list of Class objects(It is a nested Class) which looks something like this :
我有一个类对象列表(它是一个嵌套类),它看起来像这样:
public class IntVector
{
private string customerid;
private string hash_id;
private string client_name;
private string mobile_no;
private string address;
//Table
private List<CustomerInfo> customerinfo;
}
I have a list of IntVector
我有一个清单 IntVector
private List<IntVector> UserData;
Now how to set CustomerInfo
as the Datasource for a DatagridView
Control which is member of the list UserData.
现在如何将控件设置CustomerInfo
为DatagridView
列表 UserData 的成员的数据源。
Thanks
谢谢
回答by Arie
First, you have to expose your customerinfo list somehow (it is now private, so you can't get it from outside your IntVector class).
首先,您必须以某种方式公开您的 customerinfo 列表(它现在是私有的,因此您无法从 IntVector 类之外获取它)。
If it was public:
如果是公开的:
BindingSource bs = new BindingSource();
int indexInUserDataList = 0;
bs.DataSource = UserData[indexInUserDataList].customerinfo;
datagridview.DataSource = bs;
Also, you may want to consider using BindingList instead of List if you want to modify your list programmatically and want those changes to be propagated to the control (here the difference is explained List<T> vs BindingList<T> Advantages/DisAdvantages)
此外,如果您想以编程方式修改列表并希望将这些更改传播到控件,您可能需要考虑使用 BindingList 而不是 List(此处解释了List<T> 与 BindingList<T> 优点/缺点)
What your CustomerInfo class looks like? I assume you want to bind columns of DataGridView to public properties of CustomerInfo class, for exsample:
您的 CustomerInfo 类是什么样的?我假设您想将 DataGridView 的列绑定到 CustomerInfo 类的公共属性,例如:
class CustomerInfo
{
public int Id {get;set;}
public string Name {get;set;}
public string Address {get;set;}
private string somePrivateData;
}
Now if AutoGenerateColumns in your DataGridView is set to true, then 3 columns "Id", "Name" and "Address" will be automatically created in your DataGridView. "somePrivateData" will be ignored.
现在,如果您的 DataGridView 中的 AutoGenerateColumns 设置为 true,那么将在您的 DataGridView 中自动创建 3 列“Id”、“Name”和“Address”。“somePrivateData”将被忽略。
If you want to define collumns yourself, you can do it like this:
如果你想自己定义列,你可以这样做:
// make sure to do it before binding DataGridView control
datagridview.AutoGenerateColumns = false;
DataGridViewTextBoxColumn col1 = new DataGridViewTextBoxColumn();
col1.DataPropertyName = "Name";
col1.HeaderText = "Customer name";
col1.Name = "column_Name";
datagridview.Columns.Add(col1);
DataGridViewTextBoxColumn col2 = new DataGridViewTextBoxColumn();
col2.DataPropertyName = "Address";
col2.HeaderText = "Address";
col2.Name = "column_Address";
datagridview.Columns.Add(col2);
回答by Igor Frank
You need to set the private list of customers as public:
您需要将客户的私人列表设置为公开:
public class IntVector
{
private string customerid;
private string hash_id;
private string client_name;
private string mobile_no;
private string address;
//Table
public List<CustomerInfo> customerinfo;
}
}
private List<IntVector> UserData;
//Populate the UserData list here
And then you can set the datasource to DataGridView like:
然后您可以将数据源设置为 DataGridView,例如:
DataGridView.DataSource = UserData[0].customerinfo;
I hope that helps...
我希望这有帮助...