如何按 2 列对 datagridview 进行排序 [英] how to sort a datagridview by 2 columns
本文介绍了如何按 2 列对 datagridview 进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何按两列(升序)对 DataGridView 进行排序?我有两列:day
和 status
.
How do I sort a DataGridView by two columns (ascending)? I have two columns: day
and status
.
如果我需要按一列排序,我会这样做:
If I need to sort by one column, I do:
this.dataGridView1.Sort (this.dataGridView1.Columns["day"], ListSortDirection.Ascending);
但是两个人呢?
推荐答案
如果您的 DataGridView
是数据绑定的,您可以对 Datatable
视图进行排序并重新绑定到数据表,如下所示:
If your DataGridView
is databound, you can sort your Datatable
view and rebind to datatable as below:
private DataGridView dataGridView1 = new DataGridView();
private BindingSource bindingSource1 = new BindingSource();
private void Form1_Load(object sender, System.EventArgs e)
{
// Bind the DataGridView to the BindingSource
dataGridView1.DataSource = bindingSource1;
SortDataByMultiColumns(); //Sort the Data
}
private void SortDataByMultiColumns()
{
DataView view = dataTable1.DefaultView;
view.Sort = "day ASC, status DESC";
bindingSource1.DataSource = view; //rebind the data source
}
OR,不使用 bindingsource 直接绑定到 DataView
:
OR, without using bindingsource and binding directly to DataView
:
private void SortDataByMultiColumns()
{
DataView view = ds.Tables[0].DefaultView;
view.Sort = "day ASC, status DESC";
dataGridView1.DataSource = view; //rebind the data source
}
这篇关于如何按 2 列对 datagridview 进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文