数据源不反映排序的DataGridView [英] Datasource not reflecting sorting in DataGridView

查看:203
本文介绍了数据源不反映排序的DataGridView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我绑定一个数据表数据源 A DataGridView的。我启用了 SortMode 属性自动所有的DataGridViewColumn 。但是,当我在DataGridView的排序排序变化不会反映在底层数据源是DataTable中。


例如:

  DataTable的表=新的DataTable(); 
table.Columns.Add(姓名);
table.Columns.Add(时代的typeof(INT));
table.Rows.Add(亚历克斯,27);
table.Rows.Add(杰克,65);
table.Rows.Add(条例,22);
dataGridView1.DataSource =表;





现在,如果我是让所选择的行象下面这样它会总是在0指数收益率的行DataTable中的甚至整理徐家,27


$ b $之后b

 的DataRow NEWROW = table.NewRow(); 
newRow.ItemArray = table.Rows [dataGridView1.Rows.IndexOf(dataGridView1.SelectedRows [0])] ItemArray.Clone()作为对象[]。



有人可以请建议我应该怎么去了解这种情况?


< DIV CLASS =h2_lin>解决方案

所有的数据表的对象首先是从 DataGridView的即使你把它绑定到数据源,也就是说,如果你改变你的 DataGridView的也不会影响到你的数据表。而在你的情况下,要在 DataGridView的分拣到数据表中得到体现。所以出于这个原因,你需要每有订货/排序的变化在 DataGridView的的时间来捕捉事件。你需要因此搭上了这方面的 DataGridView的 ColumnHeaderMouseClick 事件。



另一个重要的事情是,在实现同步的排序都数据表 DataGridView的默认视图 确定时代类具有排序属性的方法。所以,我们要做的是每次 DataGridView的更改排序明智的,我们将排序数据表



首先,我们需要做一个DataTable对象全球,这样我们就可以在任何地方以后访问的。

 的DataTable表; 



其次,我们需要初始化的事件侦听器 ColumnHeaderMouseClick 并为我们的实际目的,我们将在表格构造函数中设置它。

 的InitializeComponent(); 
dataGridView1.ColumnHeaderMouseClick + =新DataGridViewCellMouseEventHandler(dataGridView1_ColumnHeaderMouseClick);



然后,我们有这个空的鼠标事件处理程序:

 无效dataGridView1_ColumnHeaderMouseClick(对象发件人,DataGridViewCellMouseEventArgs E)
{

}

如果上面的方法不会自动出来了,只要复制并在你的代码粘贴。



和为了说明起见,我们将添加数据表窗体加载过程中在这种情况下我会使用你的自己的代码:

 表=新的DataTable(); 
table.Columns.Add(姓名);
table.Columns.Add(时代的typeof(INT));
table.Rows.Add(亚历克斯,27);
table.Rows.Add(杰克,65);
table.Rows.Add(条例,22);
dataGridView1.DataSource =表;

和然后最后我们会把一些代码在 ColumnHeaderMouseClick 事件:

 无效dataGridView1_ColumnHeaderMouseClick(对象发件人,DataGridViewCellMouseEventArgs E)
{
如果(dataGridView1 .SortOrder.ToString()==降序)//检查排序降序
{
table.DefaultView.Sort = dataGridView1.SortedColumn.Name +DESC; //获取排序的列名,并将其按降序排列
}
,否则
{
table.DefaultView.Sort = dataGridView1.SortedColumn.Name +ASC排序; //否则按升序
}
表= table.DefaultView.ToTable(排序); //排序视图转换为数据表,然后分配到表对象。
}

您现在可以使用表对象和分类按照排序顺序的 DataGridView的



只是为了确认我的要求,我们会在您指定的形式一个按钮按钮1 并单击时,它会显示第一行和排序的列值,如:

 私人无效的button1_Click(对象发件人,EventArgs五)
{
字符串sortedValue = dataGridView1.SortedColumn.Name ==名称:table.Rows [0] [0]的ToString()? table.Rows [0] [1]的ToString();
MessageBox.Show(sortedValue);
}


I am binding a DataTable as the DataSource of a DataGridView. I enabled the SortMode property to Automatic for all DataGridViewColumn. But when I am sorting on DataGridView the Sort change is not being reflected in the underlying DataSource which is the DataTable.
For example:

DataTable table = new DataTable();
table.Columns.Add("Name");
table.Columns.Add("Age", typeof(int));
table.Rows.Add("Alex", 27);
table.Rows.Add("Jack", 65);
table.Rows.Add("Bill", 22);
dataGridView1.DataSource = table;


Now if I was to get the selected row like below it will always return the row at 0 Index in the DataTable even after sorting which is Alex, 27.

DataRow newRow = table.NewRow();
newRow.ItemArray = table.Rows[dataGridView1.Rows.IndexOf(dataGridView1.SelectedRows[0])].ItemArray.Clone() as object[];

Can someone please suggest how I should go about this situation?

解决方案

First of all the DataTable object is detached from DataGridView even if you bind it to DataSource, that is, if you change your DataGridView it will not affect your DataTable. And in your case you want the sorting in DataGridView to be reflected in DataTable. And so for that reason you need to catch an event every time there is changes in ordering/sorting in DataGridView. You need to catch therefore for that regard the ColumnHeaderMouseClick event of the DataGridView.

Another important thing is, in achieving a synchronize sorting of both DataTable and DataGridView is the DefaultView method of Datable class that has the Sort property. So, what we are going to do is every time the DataGridView is changed sorting-wise we will sort the DataTable also.

First of all we need to make a DataTable object global so that we could access it anywhere later on.

DataTable table;

Secondly, we need to initialize an event listener for ColumnHeaderMouseClick and for our practical purpose we will set it at Form constructor.

InitializeComponent();
dataGridView1.ColumnHeaderMouseClick += new DataGridViewCellMouseEventHandler(dataGridView1_ColumnHeaderMouseClick);

We then have this empty Mouse event handler:

 void dataGridView1_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
 {            

 }

And if the above method doesn't automatically come out, just copy and paste in your code.

And for the sake of illustration we will add the DataTable during the Form Load and in this case I am going to use your own code:

table = new DataTable();
table.Columns.Add("Name");
table.Columns.Add("Age", typeof(int));
table.Rows.Add("Alex", 27);
table.Rows.Add("Jack", 65);
table.Rows.Add("Bill", 22);
dataGridView1.DataSource = table;

And then finally we will put some code in the ColumnHeaderMouseClick event:

  void dataGridView1_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
  {            
      if (dataGridView1.SortOrder.ToString() == "Descending") // Check if sorting is Descending
      {
          table.DefaultView.Sort = dataGridView1.SortedColumn.Name + " DESC"; // Get Sorted Column name and sort it in Descending order
      }
      else
      {
        table.DefaultView.Sort = dataGridView1.SortedColumn.Name + " ASC";  // Otherwise sort it in Ascending order
      }
      table = table.DefaultView.ToTable(); // The Sorted View converted to DataTable and then assigned to table object.
  }

You could now use table object and sorted according to the sorting order of the DataGridView.

Just to confirm my claim, we will make a button in your form named button1 and when clicked it will show the first row and the sorted column value, like:

  private void button1_Click(object sender, EventArgs e)
  {
    String sortedValue = dataGridView1.SortedColumn.Name == "Name" : table.Rows[0][0].ToString() ? table.Rows[0][1].ToString();
     MessageBox.Show(sortedValue);
  }

这篇关于数据源不反映排序的DataGridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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