c# - Adding a Command Column to DataGridView -


i have c# datagridview columns. want add action column last cell in each row.

there should buttons or icons view,delete,update in each row.

here few options can think of:

  • by far simplest add 1 column each button. (recommended!)
  • of create custom cell type, maybe panel subclass buttons want. involves implementing idatagridvieweditingcontrol iterface @ least dozen fields , methods , 2 custom classes derived datagridviewcolumn , datagridviewcell many more things do. in short huge amount of work! sophisticated editing control maybe worth it. few buttons not! (not recommended!)
  • or fake buttons little owner-drawing magic in cellpainting event. see below..!
  • or add 1 sopisticated control outside datagridview acts on current row. usual way!

here fun little example, owner-draws 4 'sidu' commands in 4th column of dataggridview dgv:

enter image description here

private void form1_load(object sender, eventargs e) {     dgv.rows.add(12);     for( int = 0; i< dgv.rows.count; i++)     {         dgv[0, i].value = i;         dgv[1, i].value = r.next(1000);         dgv[2, i].value = rights[r.next(rights.count)];         dgv[3, i].readonly = true;     } }  list<string> rights = new list<string>    { "sidu", "sid-", "si-u", "s-du", "si--", "s--u", "s-d-", "s---" }; dictionary<char, string> rightstexts = new dictionary<char, string>    { { 's', "select" }, { 'i', "insert" }, { 'd', "delete" }, { 'u', "update" } }; random r = new random(1);   private void dgv_cellpainting(object sender, datagridviewcellpaintingeventargs e) {     if (e.columnindex == 3 && e.rowindex >= 0)     {         string r = dgv[2,e.rowindex].value.tostring();         stringformat format = new stringformat()         { linealignment = stringalignment.center,  alignment = stringalignment.center};          int w = e.cellbounds.width / 4;         int y = e.cellbounds.y + 1;         int h = e.cellbounds.height - 2;          e.paintbackground(e.cellbounds, false);          (int = 0; < 4; i++)         {            int x = e.cellbounds.x + * w;            rectangle rect = new rectangle(x, y, w, h);            controlpaint.drawbutton(e.graphics, rect, buttonstate.normal);            if (rightstexts.containskey(r[i]))                 e.graphics.drawstring(rightstexts[r[i]], dgv.font,                                      systembrushes.windowtext, rect ,format );         }         e.handled = true;     } }  private void dgv_cellmouseclick(object sender, datagridviewcellmouseeventargs e) {     if (e.columnindex == 3 && e.rowindex >= 0)     {         datagridviewcell cell = dgv[e.columnindex, e.rowindex];         int w = cell.size.width;         int buttonindex = e.x * 4 / w;         text = rightstexts.elementat(buttonindex).value;     } } 

the drawing stuff thrown rather q&d, invest lot more care fine-tune it..

i have chosen display rights in visible cell demostration. production action cell's value obvious place.


Comments

Popular posts from this blog

facebook - android ACTION_SEND to share with specific application only -

python - Creating a new virtualenv gives a permissions error -

javascript - cocos2d-js draw circle not instantly -