Click here to Skip to main content
15,890,375 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
i want to get ID When i m clicking on Edit button in gridveiw table. but i faced a problem when i m doing this ... i m using GridViewUpdateEventArgs event. its do not return the ID.

{System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
at System.Collections.ArrayList.get_Item(Int32 index)
at System.Web.UI.WebControls.DataKeyArray.get_Item(Int32 index)
at Doze.gvw_RowUpdating(Object sender, GridViewUpdateEventArgs e) }

Above Exception give in this line to me.

Int32 id = Int32.Parse(GridView.DataKeys[e.RowIndex].Value.ToString());

can anyone tell me What mistake i have Done ?
Posted
Comments
Shemeer NS 3-Jul-12 9:19am    
Do you have multiple Data Key Names in Gridview? can you provide some more details about the code

If you have single key in Data Key
DataKeyNames="empId"
then use
C#
int empId= Convert.ToInt32(GridView1.DataKeys[row.RowIndex].Value);


If you have two keye in Data Key
DataKeyNames="empId,depId"
then use
C#
int EmployeeID = Convert.ToInt32(GridView1.DataKeys[row.RowIndex].Values[0]);


As per your comment I have added this below part....

Why you have mentioned EditIndex in the HTML itself?no issue just to confirm...

Please find the code below and check you are doing the same....

HTML
--------
XML
<asp:GridView ID="gvw" runat="server" AutoGenerateColumns="False" CssClass="basix"
    Width="408px" CellPadding="4" ForeColor="#333333" GridLines="None" Height="127px"
    OnSelectedIndexChanged="gvwExample_SelectedIndexChanged" OnRowDeleting="gvw_RowDeleting1"
    OnRowUpdating="gvw_RowUpdating" OnRowCancelingEdit="gvw_RowCancelingEdit"
    OnRowEditing="gvw_RowEditing" DataKeyNames="ID">
    <Columns>
        <asp:BoundField DataField="ID" HeaderText="ID" AccessibleHeaderText="ID" />
        <asp:BoundField DataField="MedDoze" HeaderText="DOZE" AccessibleHeaderText="DOZE" />
        <asp:CommandField HeaderText="Delete" ShowDeleteButton="True" />
        <asp:CommandField ShowEditButton="True" />
    </Columns>
    <PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
    <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
    <HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
</asp:GridView>


Code Behind
-----------
C#
protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            fillGrid();
        }
    }
    protected void gvw_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        gvw.EditIndex = -1;
        fillGrid();
    }
    protected void gvw_RowDeleting1(object sender, GridViewDeleteEventArgs e)
    {
        // do you code here
    }
    protected void gvw_RowEditing(object sender, GridViewEditEventArgs e)
    {
        gvw.EditIndex = e.NewEditIndex;
        fillGrid();

    }
    protected void gvw_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        int myID = Convert.ToInt32(gvw.DataKeys[e.RowIndex].Value);
        Response.Write(myID); // this will print the Id on the screen
        //do you update logic here
    }
    protected void gvwExample_SelectedIndexChanged(object sender, EventArgs e)
    {
        // do you code here
    }



mark it as solution if it answer your question
 
Share this answer
 
v2
Comments
miss_sumaira 3-Jul-12 11:26am    
my solution is
DataKeyNames="ID"

Int32 ID = Int32.Parse(GridView.DataKeys[e.RowIndex].Value.ToString());
miss_sumaira 4-Jul-12 12:55pm    
<asp:GridView ID="gvw" runat="server" AutoGenerateColumns="False"
CssClass="basix" Width="408px" CellPadding="4" ForeColor="#333333"
GridLines="None" Height="127px"
onselectedindexchanged="gvwExample_SelectedIndexChanged" onrowdeleting="gvw_RowDeleting1"

onrowupdating="gvw_RowUpdating" EditIndex="0"
onrowcancelingedit="gvw_RowCancelingEdit" onrowediting="gvw_RowEditing"
DataKeyNames="ID" >


<asp:BoundField DataField="ID" HeaderText="ID" AccessibleHeaderText="ID" />
<asp:BoundField DataField="MedDoze" HeaderText="DOZE"
AccessibleHeaderText="DOZE" />
<asp:CommandField HeaderText="Delete" ShowDeleteButton="True" />
<asp:CommandField ShowEditButton="True" />


<PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />




Event code

GridViewRow row = (GridViewRow)gvw.Rows[e.RowIndex];
Int32 ID = Int32.Parse(gvw.DataKeys[e.RowIndex].Value.ToString());
string DozeName = ((TextBox)(row.Cells[1].Controls[0])).Text;
TextBox txtDozename = new TextBox();
txtDozename.Text = DozeName;
this.Controls.Add(txtDozename);
string Query = " update dbo.AddDoze set Meddoze= N'" txtDozename.Text + "' where ID= " + ID + " ";
DBAccess.Execute(Query);
Shemeer NS 3-Jul-12 11:30am    
Please share the HTML for grid and the specified event...for a detailed check
Shemeer NS 4-Jul-12 9:32am    
I have updated the solution for you with detailed code...please check....
miss_sumaira 4-Jul-12 13:01pm    
yeah i implemented all these handler also ..
thank you so much for replaying...
There may be a mistake:
First check that you have provided the field that you are accessing in DataKeyNames property in that gridview control.
 
Share this answer
 
Comments
miss_sumaira 3-Jul-12 8:37am    
I m not using DataKeyName property for Assessing....
 
Share this answer
 
Comments
miss_sumaira 3-Jul-12 8:46am    
any other solution ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900