c# - GridView RowIndex is 0 -
i have gridview on page , click on edit
, displays editable text box when edit value , press update
errors:
the error suggests gridview2.datakeys
null
.
i adding debugging by:
int test = e.rowindex
and gives me value of 0
my code below:
can suggest why im getting:
an exception of type 'system.argumentoutofrangeexception' occurred in mscorlib.dll not handled in user code
additional information: index out of range. must non-negative , less size of collection.
protected void gridview2_rowupdating(object sender, gridviewupdateeventargs e) { int datakeyvalue = convert.toint32(gridview2.datakeys[e.rowindex].value.tostring()); gridviewrow row = (gridviewrow)gridview2.rows[e.rowindex]; label lblid = (label)row.findcontrol("lblid"); textbox gvtxtnextstep = (textbox)row.cells[0].controls[0]; gridview2.editindex = -1; cobj.supportref1 = txtsupportref.text; cobj.nextstep1 = txtnextstep.text; bobj.microticketupdate(cobj); gridview2.databind(); }
you need set datakeynames
property on gridview control relevant id property e.g. <asp:gridview datakeynames="personid"
...
edit
below complete example return valid primary key value in datakeyvalue
variable.you can copy example, is, new web page in web application , test see how works , how data get's bound , hope looking @ example can figure out changes should make code working:
.aspx:
<asp:gridview datakeynames="id" id="gridview1" runat="server" autogeneratecolumns="false" autogenerateeditbutton="true" onrowediting="gridview1_rowediting" onrowupdating="gridview1_rowupdating" onrowcancelingedit="gridview1_rowcancelingedit"> <columns> <asp:boundfield datafield="name" headertext="name" /> <asp:boundfield datafield="creditrequest" headertext="credit request" /> </columns> </asp:gridview>
code behind:
public class persont { public int id { get; set; } public string name { get; set; } public string creditrequest { get; set; } } public partial class aaaa_gridviewrowupdating : system.web.ui.page { protected void page_load(object sender, eventargs e) { if (!page.ispostback) { this.binddata(); } } private void binddata() { var p1 = new persont() { id = 10, name = "person 1", creditrequest = "credit request 1" }; var p2 = new persont() { id = 20, name = "person 2", creditrequest = "credit request 2" }; var list = new list<persont> { p1, p2 }; gridview1.datasource = list; gridview1.databind(); } protected void gridview1_rowediting(object sender, gridviewediteventargs e) { gridview1.editindex = e.neweditindex; binddata(); } protected void gridview1_rowupdating(object sender, gridviewupdateeventargs e) { system.diagnostics.debugger.break(); int index = e.rowindex; int datakeyvalue = convert.toint32(gridview1.datakeys[index].value.tostring()); } protected void gridview1_rowcancelingedit(object sender, gridviewcancelediteventargs e) { gridview1.editindex = -1; binddata(); } }
Comments
Post a Comment