2009-11-02 77 views
0

我的DataGridView綁定到與插入表單相同的BindingSource,並且Name列被排序。插入完成後,groupBindingSource.Current不會返回新插入的DataRowView,而是按排序順序排列最後一行,從而使Update不起作用。插入帶有排序DataGridView的行

 FormGroup formGroup = new FormGroup(); 
     formGroup .Source = groupBindingSource; 
     formGroup .setMode(FormGroup.Mode.Insert); 
     if (formGroup .ShowDialog() == DialogResult.OK) 
     { 
      DataRowView drv = (DataRowView)groupBindingSource.Current; 
      grupoTableAdapter.Update(drv.Row); 
     } 
+0

我認爲這將來自這裏:http://stackoverflow.com/questions/1664537/select-newly-added-row-datagridview-and-bindingsource我重新提出了這個問題。現在更清楚了。 – 2009-11-03 02:59:31

回答

0
DataRowView drv = (DataRowView)source.AddNew(); 
    grupoTableAdapter.Update(drv.Row); 
    grupoBindingSource.Position = grupoBindingSource.Find("ID", drv.Row.ItemArray[0]); 

它使綁定到相應選擇新添加的行即使任何列進行排序出的DataGridView。

0

在插入和啓用它之前,您可以禁用排序嗎?

是這樣的:

//... 
//disable sorting 
if (formGroup .ShowDialog() == DialogResult.OK) 
{ 
    DataRowView drv = (DataRowView)groupBindingSource.Current; 
    grupoTableAdapter.Update(drv.Row); 
} 
//enable sorting