c# - DatagridView does not refresh after i have added deleted or inserted new items from its linkcolumns -


what happens after perform 1 operation datagridview updates , reflects operation performed.but cannot perform more operation after first operation.its if goes in read mode or links stop work.am new datagridviews appreciated.

my code display datagridview.

public void displaydatagridview()     {          con.open();         sqldataadapter da = new sqldataadapter("select * reg", con);         datatable dt = new datatable();         da.fill(dt);         datagridview1.datasource = dt;         con.close();     } 

my code add 3 columns insert delete , edit.

private void form1_load(object sender, eventargs e)     {         displaydatagridview();          datagridviewlinkcolumn editlink = new datagridviewlinkcolumn();         editlink.usecolumntextforlinkvalue = true;         editlink.headertext = "edit";         editlink.linkbehavior = linkbehavior.systemdefault;         editlink.text = "edit";         datagridview1.columns.add(editlink);      } 

and delete code.

 private void datagridview1_cellcontentclick(object sender, datagridviewcelleventargs e)     {         if (e.columnindex == 6)//delete         {             string id = convert.tostring(datagridview1.rows[e.rowindex].cells["id"].value);             con.open();             sqlcommand cmd = new sqlcommand("delete reg id=" + id + "", con);              cmd.executenonquery();             con.close();              displaydatagridview();             //datagridview1.refresh();         } 

i able perform 1 operation , cannot else.also after first operation no other operation reflected in database.

your "delete" code block not execute because columnindex of editlink not 6 anymore

every time when datagridview updated new datasource,
generated columns removed , new columns generated
(if datagridview.autogenearatecolumns = true - assume in case)

editlink not removed because wasn't generated datagridview, added manually. after new datasource setted columnindex of editlink column became 0.

for workaround use column name when create column in load event handler

editlink.name = "columndelete"; 

then in cellcontentclick event handler compare indexes using name

if (e.columnindex == this.datagridview1.columns["columndelete"].index) {     //delete code } 

i prefer using column names instead of indexes. names can sure column, after changing order of columns in datagridview


Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - Bypass Geo Redirect for specific directories -

php - .htaccess mod_rewrite for dynamic url which has domain names -