c# - How to add button to any datagridview column's last cell -


i want add button in last cell of columns. not working. how can this?

foreach (datagridviewrow row in datagridview1.rows) {     datagridviewbuttoncell button = (row.cells["1"] datagridviewbuttoncell); } 

edit: don't want add column. want add these button on columns' last cell, not row's.

enter image description here

you have create datagridviewbuttoncell each column want adorn:

datagridviewbuttoncell cell_1 = new datagridviewbuttoncell(); 

then can replace original cells datagridviewbuttoncells:

datagridview1[oneofyourbuttoncolumnindices , buttonrowindex] = cell_1; 

note need create seperate cells each button! each can added once!

you can style cell like:

cell_1.value = "conquer"; cell_1.style.alignment = datagridviewcontentalignment.middlecenter; 

hoever can't register events type datagridviewcell not control , not implement event model.

instead events must handled in datagridview. here example:

list<int> yourbuttoncolumnindices = new list<int> {3,5,7};  private void dgv_cellclick(object sender, datagridviewcelleventargs e) {     datagridviewcell cell = dgv[e.columnindex, e.rowindex];      // check if have hit button cell..     if (yourbuttoncolumnindices.contains(e.columnindex) && cell datagridviewbuttoncell )     {         console.writeline(cell.value.tostring());         // things depend on button:         // if (cell.value == "conquer")  doconquer(..);         // else (cell.value == "command")  docommand(..);         // ..     } } 

you can see can access cell's value normal. displayed button's text.

note cells have tag property , store in there, including delegate!

also note in model buttons' row may change when insert or delete rows. therefore should take care either refer buttonrowindex when add buttons or keep date. didn't event check in click event column index plus cell type should enough. if not, can check on values..

of course can code docommand methods like, adding parameters needed..

btw: regular buttons silver or whatever system has buttons in color scheme. can make them flatstyle buttons , voila, cell's backcolor (and/or forecolor) used:

 cell_1.flatstyle = flatstyle.flat;  cell_1.style.backcolor = color.lightskyblue; 

enter image description here

if have more 2 or 3 such buttons may want create dictionary manage them, maybe this:

dictionary<int, string> buttonlist = new dictionary<int, string>(); buttonlist.add(3, "save operator"); buttonlist.add(5, "save an.1"); buttonlist.add(7, "save an.2"); 

and use create them:

foreach(int in buttonlist.keys)   {      datagridviewbuttoncell cell= new datagridviewbuttoncell();      cell.value = buttonlist[i];      cell.style.alignment = datagridviewcontentalignment.middlecenter;      datagridview1[i, buttonrowindex] = cell;   } 

and check them in cellclick:

if (buttonlist.keys.contains(e.columnindex) && cell datagridviewbuttoncell ) {     if (e.columnindex] == 3) dostuff3()   else if (e.columnindex] == 5) dostuff5()   //.. 

Comments

Popular posts from this blog

javascript - Google App Script ContentService downloadAsFile not working -

javascript - Function overwritting -

php - Find a regex to take part of Email -