如何排序由2列一个DataGridView [英] how to sort a datagridview by 2 columns

查看:88
本文介绍了如何排序由2列一个DataGridView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何排序的两列(升序)一个DataGridView?我有两列:状态

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);



但两年?

But for two?

推荐答案

如果你的 DataGridView的是数据绑定,您可以排序你的数据表视图,并重新绑定到数据表如下图所示:

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
}

或不使用的BindingSource和直接绑定到数据视图

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆