c# - row in index value in grid always taking from zero onwards..how to get row index value properly -


whenever executing grid value of row id taking 0 delete , edit operations not performing well.

any 1 please me. in advance

protected void grd_view_rowcommand(object sender, system.web.ui.webcontrols.gridviewcommandeventargs e) {     if (e.commandname == "edit")     {         int rowid = 1;          rowid = convert.toint32(e.commandargument);         session["rowid"]= rowid;         datatable dt = new datatable();         sqlconnection con = new sqlconnection(mystr);         sqlcommand cmd = new sqlcommand("select * customerprofmain customercode='" +rowid+ "'", con);         sqldataadapter sda = new sqldataadapter(cmd);         con.open();         dataset ds = new dataset();         sda.fill(ds);         dt=ds.tables[0];         textbox1.text = dt.rows[0]["customername"].tostring();         textbox2.text=dt.rows[0]["address"].tostring();         textbox3.text=dt.rows[0]["tellno"].tostring();         textbox4.text=dt.rows[0]["faxno"].tostring();         textbox5.text=dt.rows[0]["email"].tostring();         button1.text = "update";     }     if (e.commandname == "delete")     {         int rowid = convert.toint32(e.commandargument);         session["rowid"] = rowid;         // datatable dt = new datatable();         sqlconnection con = new sqlconnection(mystr);         sqlcommand cmd = new sqlcommand("delete customerprofmain customercode='"+rowid+"' ", con);         sqldataadapter sda = new sqldataadapter(cmd);         con.open();         cmd.executenonquery();         con.close();         //dataset ds = new dataset();         //sda.fill(ds);         //dt = ds.tables[0];     } } 

you can use commandsource property. please try

gridviewrow gvr = (gridviewrow)((control)e.commandsource).namingcontainer;  int rowindex    = gvr.rowindex; 

you row index in question

 int index = convert.toint32(e.commandargument); 

if using <asp:commandfield showeditbutton="true" /> or <asp:buttonfield buttontype="button" text="save" commandname="edit" />

but best approach using commandargument have set aspx:

commandargument='<%# ((gridviewrow) container).rowindex %>'  

then rowindex follows:

int rowindex = int.parse(e.commandargument.tostring());  

please refer here or here know commandargument in gridview


Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -