c# - DataGridView and Locating Data -
i using datagridview
keep track of list<myobject>
. populate it, use foreach loop:
foreach (myobject object in mylist) { if (object.status == status.available) { mydatagridview.rows.add(object.name, object.status.tostring()); } }
i use event create new form object in selected row:
void mydatagridview_celldoubleclick(object sender, datagridviewcelleventargs e) { var index = mylist[mydatagridview.currentrow.index]; myform form = new myform(index); }
so works fine until status of item in list changes:
mylist[10].status = status.unavailable;
now when mydatagridview
updated, can no longer use row index open correct form row past 10. i'm @ loss do.
can tell me how open correct form though indices don't match anymore?
edit: mylist
holds characters in game, available hire , aren't. need mydatagridview
populated status available
.
somehow have associate index in list row. 1 way have hidden column in grid maintains mapping between row , correct list index. add hidden column, modify insert code follows:
int = 0; foreach (myobject object in mylist) { if (object.status == status.available) { mydatagridview.rows.add(object.name, object.status.tostring(), i); } i++; } //hide third column mydatagridview.columns[2].visible = false;
then during celldoubleclick event can reference hidden column true index of item in row.
void mydatagridview_celldoubleclick(object sender, datagridviewcelleventargs e) { int listindex = (int)mydatagridview.currentrow.cells[2].value; var index = mylist[listindex]; myform form = new myform(index); }
Comments
Post a Comment