visual studio 2010 - c# textbox to datagridview -
i want add exact value of textbox
datagridview
problem if add item last item add change. here print screen of sample problem..
1st try
2nd try
this code.
int n = datagridview3.rows.add(); (int j = 0; j < datagridview3.rowcount; j++) { if (datagridview3.rows[j].cells[1].value != null && (textbox4.text == datagridview3.rows[j].cells[4].value.tostring())) { messagebox.show("item on list!"); datagridview3.rows.remove(datagridview3.rows[n]); return; } else { datagridview3.rows[j].cells[1].value = textbox43.text; datagridview3.rows[j].cells[4].value = textbox4.text; datagridview3.rows[j].cells[2].value = datetime.now.toshortdatestring(); datagridview3.rows[j].cells[3].value = datetimepicker3.text; datagridview3.firstdisplayedscrollingrowindex = n; datagridview3.currentcell = datagridview3.rows[n].cells[0]; datagridview3.rows[n].selected = true; } }
you looping on complete array , if not yet on list goes in else part of if. in block assign current entered values row, every single row have.
to fix separated check duplicates , add part more clearly.
do notice if have run through debugger , stepped on each line of code (hitting f10 in visual studio) have spotted bug easily. have @ blog scott guthrie (among others) http://weblogs.asp.net/scottgu/debugging-tips-with-visual-studio-2010
// check if added 1 (int j = 0; j < datagridview3.rowcount; j++) { if (datagridview3.rows[j].cells[1].value != null && (textbox4.text == datagridview3.rows[j].cells[4].value.tostring())) { messagebox.show("item on list!"); return; } } // lets add it! int n = datagridview3.rows.add(); datagridview3.rows[n].cells[1].value = textbox43.text; datagridview3.rows[n].cells[4].value = textbox4.text; datagridview3.rows[n].cells[2].value = datetime.now.toshortdatestring(); datagridview3.rows[n].cells[3].value = datetimepicker3.text; datagridview3.firstdisplayedscrollingrowindex = n; datagridview3.currentcell = datagridview3.rows[n].cells[0]; datagridview3.rows[n].selected = true;
Comments
Post a Comment