我DataGridView
我通过DataTable
添加项目
此项目从XML文件和此XML文件中读取,
那些未排序且我想要排序的项目是在添加到我的DataGridView
private void UpdateDataGdirView(List<Vendor> list)
{
DataTable dt = new DataTable();
dt.Columns.Add("Column1", System.Type.GetType("System.Boolean"));
dt.Columns.Add("Column2", typeof(string));
dt.Columns.Add("Column3", System.Type.GetType("System.Boolean"));
dt.Columns.Add("Column4", System.Type.GetType("System.Boolean"));
dt.Columns.Add("Column5", System.Type.GetType("System.Boolean"));
dt.Columns.Add("Column6", System.Type.GetType("System.Boolean"));
DataRow dr;
foreach (Vendor vendor in list)
{
dr = dt.NewRow();
dr["Column1"] = vendor.IsVendorChecked;
dr["Column2"] = vendor.Number;
dr["Column3"] = vendor.Name;
dr["Column4"] = vendor.Size;
dr["Column5"] = vendor.Path;
dr["Column6"] = vendor.Path2;
dt.Rows.Add(dr);
}
dataGridView1.AllowUserToAddRows = false;
dataGridView1.AllowUserToDeleteRows = false;
dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
this.Invoke((MethodInvoker)delegate { dataGridView1.DataSource = dt; });
}
答案 0 :(得分:2)
答案 1 :(得分:1)
我认为您应该使用DataView。喜欢 :
DataTable orders = dataSet.Tables["SalesOrderHeader"];
EnumerableRowCollection<DataRow> query =
from order in orders.AsEnumerable()
orderby order.Field<decimal>("TotalDue")
select order;
DataView view = query.AsDataView();
bindingSource1.DataSource = view;
请点击此链接:Query on DataView
OR
您可以使用如下:
DataTable orders = dataSet.Tables["SalesOrderHeader"];
DataView dv = new DataView(orders);
dv.Sort = "TotalDue";
dataGridView1.DataSource = dv;