winforms - C# DataGridView edit cell value -
winforms - C# DataGridView edit cell value -
all. i've been googling question hr , still can't understand how works. have datagridview command on form, has 3 columns + 1 buttoncolumn, add together lines this:
dg.rows.add(param1, param2, param3);
the text button set this:
datagridviewbuttoncolumn bc = (datagridviewbuttoncolumn)dg.columns["buttoncolumn"]; bc.text = "action"; bc.usecolumntextforbuttonvalue = true;
now, want alter specific button's text, 1 time user clicked on it, to, let's "done". tried this:
private void datagridview1_cellclick(object sender, datagridviewcelleventargs e) { if (articles.rows[e.rowindex].cells[e.columnindex].gettype() == typeof(datagridviewbuttoncell)) { datagridviewbuttoncell cell = (datagridviewbuttoncell)articles.rows[e.rowindex].cells[e.columnindex]; articles.currentcell = cell; articles.editmode = datagridvieweditmode.editprogrammatically; articles.beginedit(false); cell.value = "done"; articles.endedit(); } }
and doesn't work. i've tried few answers similar questions here, on stackoverflow, doesn't work well. forgive me if overlooked something. kind explain me how this, , why doesn't work?
update:
private void datagridview1_cellclick(object sender, datagridviewcelleventargs e) { if (articles.rows[e.rowindex].cells[e.columnindex].gettype() == typeof(datagridviewbuttoncell)) { articles.editmode = datagridvieweditmode.editprogrammatically; articles.rows[e.rowindex].cells[e.columnindex].value = "done"; } }
update2:
private void datagridview1_cellclick(object sender, datagridviewcelleventargs e) { if (articles.rows[e.rowindex].cells[e.columnindex].gettype() == typeof(datagridviewbuttoncell)) { articles.editmode = datagridvieweditmode.editprogrammatically; articles.readonly = false; articles.rows[e.rowindex].readonly = false; articles.rows[e.rowindex].cells[e.columnindex].readonly = false; articles.currentcell = articles.rows[e.rowindex].cells[e.columnindex]; articles.beginedit(true); if (articles.rows[e.rowindex].cells[e.columnindex].isineditmode) { //it's false here articles.rows[e.rowindex].cells[e.columnindex].value = "done"; } articles.endedit(); } }
i can't alter value manually in debugger, it's set old value. problem seems sepcific datagridviewbuttoncell, because cells of other types alter fine.
you need alter using cell.value
articles.rows[e.rowindex].cells[e.columnindex].value = "done";
changing cells value changes if add together cell datagridview. way can rid of using cell this.
private void datagridview1_cellclick(object sender, datagridviewcelleventargs e) { if (articles.columns[e.columnindex].gettype() == typeof(datagridviewbuttoncolumn)) { articles.editmode = datagridvieweditmode.editprogrammatically; articles.rows[e.rowindex].cells[e.columnindex].readonly = false; articles.rows[e.rowindex].cells[e.columnindex].value = "done"; } }
i edited out rest don't believe needed doing.
c# winforms datagridview
Comments
Post a Comment