如何对DataGrid或DataSet进行排序并在C#中绑定它

时间:2014-05-03 23:02:58

标签: c# sorting datagrid dataset

我在同一表单上有文本字段控件和数据表格,并且都链接到数据库。基本上,文本字段和表表示来自数据库的相同数据。该表用于快速导航和用于更新该数据的文本字段。当用户单击表中的任何行时,该行中的数据将填充文本字段并可以进行修改。

//Bind the textbox control to the database table column
TextControl.DataBindings.Add(new System.Windows.Forms.Binding("Text", accessDataSet, comboTables.Text + "." + accessDataSet.Tables[comboTables.Text].Columns[i]));

//Finally add the controls to the form
this.Controls.Add(TextControl);
this.Controls.Add(LabelControl);

//arrange controls on the form
arrange_Controls(controlTop, accessDataTable.Columns.Count);

//database view in the grid
dGrid.SetDataBinding(accessDataSet, comboTables.Text);

排序表后出现问题。当选择排序表上的行时,它不再填充表单上的文本字段。

// Sorting LOGIC. NOTE: I don’t know how to sort my accessDataSet so I do the 
// following
DataView dv = new DataView(accessDataSet.Tables[comboTables.Text]);
dv.Sort = "ID DESC";

// view the grid
dGrid.DataSource = dv;
dGrid.Refresh();

我理解我在DataSet上执行数据绑定时正在对DataSource进行排序,但我不知道如何对DataSet进行排序。

如何对表进行排序并将其与TextBoxes链接?谢谢。

0 个答案:

没有答案