C#中的绑定控件 [英] Binding Controls in c#
问题描述
当我使用C#.net将DataGridView
中的选定记录值绑定到WinForm控件时
通过此代码:
When I bind a selected record value from DataGridView
to WinForm Controls using C#.net
by this code:
Textbox.text = DataGridView.Rows[e.RowIndex].Cells[0].Value.ToString() // Here Cells[0] for 1st column
当我对列进行排序时,行的顺序会更改,并且此代码使用
的RowIndex
数据网格.
When I sort columns the order of rows changes and this code uses RowIndex of
DataGrid. How solve this problem?
推荐答案
你好,
有一个集合,例如List<test> MyList</test>
并将其绑定到DataGrid
如果对列进行排序,则列表将保持未排序状态.
Hello,
Have a collection, for exampleList<test> MyList</test>
And bind it toDataGrid
If you sort the columns, the List will stay unsorted.
Textbox.text = MyList[0].ToString();
如果要修改DataGrid(例如,删除记录):
(
您必须修改MyList
而不是它.然后将其重新绑定到DataGrid并将可能的排序应用于DataGrid
)
或
(
实施INotifyPropertyChanged
.看一下: INotifyPropertyChanged接口
)
If you want to modify the DataGrid, (for example Deleting a record):
(
you must modify MyList
instead of it. Then ReBind it to the DataGrid and apply the possible sorting to DataGrid
)
OR
(
Implement the INotifyPropertyChanged
. Look at this: INotifyPropertyChanged Interface
)
这篇关于C#中的绑定控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!