[英]Unable to set datasource of datagridview from List
I have a datagridview that's getting its data from Access via OleDbDataReader
. 我有一个datagridview,它通过OleDbDataReader
从Access获取数据。
The problem is as follows Reading data into Data table then setting datasource = dt
=> works 问题如下将数据读入数据表然后设置datasource = dt
=>工作
dt.Load(dr);
taking the dt and applying Linq to it => empty GridView 获取dt并将Linq应用于它=>清空GridView
GVMultiple.DataSource = (from myRow in dt.AsEnumerable()
where myRow.Field<string>("State") == "Succeeded"
select myRow)
making the Linq.ToList()
=> shows columns with names "RowError","RowState","Table" and "hasErrors" which aren't my columns 使Linq.ToList()
=>显示名称为“RowError”,“RowState”,“Table”和“hasErrors”的列,这些列不是我的列
GVMultiple.DataSource = (from myRow in dt.AsEnumerable()
where myRow.Field<string>("State") == "Succeeded"
select myRow).ToList()
Reading data into Object
of a custom List then
setting datasource = List
=> empty GridView
将数据读入自定义List then
Object
, List then
设置datasource = List
=> empty GridView
while (dr.Read())
{
UserList.Add(new UserInfo()
{
ID = (int)dr["ID"],
UserName = (string)dr["User Name"]
});
}
GVMultiple.DataSource = UserList
Can someone tell me what is going on? 有人能告诉我发生了什么事吗?
In order to make DataBinding
work you've to use Properties
. 为了使DataBinding
工作,您必须使用Properties
。 Fields
doesn't support DataBinding
. Fields
不支持DataBinding
。
Modify your class as below to make it work. 修改您的课程如下,使其工作。
class UserInfo
{
public int ID {get;set;} //convert fields to property
public string UserName{get;set;}
}
GVMultiple.DataSource = (from myRow in dt.AsEnumerable() where myRow.Field("State") == "Succeeded" select myRow) GVMultiple.DataSource =(来自myRow in dt.AsEnumerable()其中myRow.Field(“State”)==“Succeeded”选择myRow)
making the Linq.ToList() => shows columns with names "RowError","RowState","Table" and "hasErrors" which aren't my columns 使Linq.ToList()=>显示名称为“RowError”,“RowState”,“Table”和“hasErrors”的列,这些列不是我的列
RowError","RowState","Table" and "hasErrors" are properties of the DataRow - which is what is in the list you created. RowError“,”RowState“,”Table“和”hasErrors“是DataRow的属性 - 这是您创建的列表中的内容。
Take a look into the .CopyToDataTable() extension method. 看一下.CopyToDataTable()扩展方法。 http://msdn.microsoft.com/en-us/library/bb396189.aspx http://msdn.microsoft.com/en-us/library/bb396189.aspx
GVMultiple.DataSource = (from myRow in dt.AsEnumerable()
where myRow.Field<string>("State") == "Succeeded"
select myRow).CopyToDataTable();
The reason for that is that the data grid view is not able to recognize the collection of new data as accepted format for display. 原因是数据网格视图无法将新数据集合识别为可接受的显示格式。
When using Linq queries to filter the data and re display it in data grid view you must always return the query result to any data source as data view. 使用Linq查询过滤数据并在数据网格视图中重新显示时,必须始终将查询结果作为数据视图返回到任何数据源。
DataView constructs an index, which significantly increases the performance of operations that can use the index, such as filtering and sorting. DataView构造一个索引,它显着提高了可以使用索引的操作的性能,例如过滤和排序。 The index for a DataView is built both when the DataView is created and when any of the sorting or filtering information is modified. DataView的索引是在创建DataView时以及修改任何排序或过滤信息时构建的。 Creating a DataView and then setting the sorting or filtering information later causes the index to be built at least twice: once when the DataView is created, and again when any of the sort or filter properties are modified. 创建DataView,然后设置排序或过滤信息会导致索引至少构建两次:一次创建DataView时,再次修改任何排序或过滤器属性。
For example: 例如:
DataTable dt = empData.loadEmployee();
BindingSource bs = new BindingSource();
bs.DataSource = dt.AsEnumerable()
.Where(c => c.Field<string>("First Name").ToLower()
.Contains(txtSearch.Text.ToLower())).AsDataView();
dgvEmpManag.DataSource = bs;
In the example I am looking for employee with first name like the search term in the text box txtSearch
when found the data grid view will display filtered results of my search AsDataView()
. 在示例中,我正在寻找具有名字的员工,如文本框txtSearch
的搜索词,当发现数据网格视图将显示我的搜索AsDataView()
过滤结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.