HI all,

I had a gridview in which users are listed in that. In that gridview i had an edit option so that i can edit user details. so my problem is... when i click edit in a row i need the userid present in that row.

Thank you.
here is my asp code.

<asp:GridView ID="GridView1" runat="server" DataKeyNames="LoginId" Width="1100px" 
            AllowSorting="true"  AllowPaging="true" BorderColor="#EED3D3" 
            AutoGenerateColumns="false" onsorting="GridView1_Sorting" 
                OnPageIndexChanging="GridView1_PageIndexChanging">
        <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
        <RowStyle BackColor="#EFF3FB" />
           <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <AlternatingRowStyle BackColor="White" />
            <Columns>
                <asp:TemplateField HeaderText="Edit">
                <ItemTemplate>
                <asp:ImageButton ID="imgbtn" ImageUrl="~/Images/edit-icon.png"  AlternateText="Edit" runat="server" Width="25" Height="25" OnClick="imgbtn_Click"  />
                </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="LoginId" HeaderText="LoginId" SortExpression="LoginId" />
                <asp:BoundField DataField="UserName" HeaderText="UserName" SortExpression="UserName"  />
                <asp:BoundField DataField="Password" HeaderText="Password" SortExpression="Password"  />
                <asp:BoundField DataField="Email" HeaderText="Email" SortExpression="Email"  />
            </Columns>
        </asp:GridView>

Dani AI

Generated

opened a GridView edit flow and suggested reading the id from a cell. That works, but reading from Cells by index is brittle (hidden columns, template fields, column reorder or paging will break the index). A safer, more maintainable approach is to use the GridView's DataKeys or to obtain the GridViewRow from the ImageButton's NamingContainer and then read the DataKey for that row.

Example using the ImageButton's Click handler (keeps the existing OnClick pattern and uses DataKeys):

protected void imgbtn_Click(object sender, ImageClickEventArgs e)
{
    var btn = (ImageButton)sender;
    var row = (GridViewRow)btn.NamingContainer;
    int rowIndex = row.RowIndex;
    int loginId = Convert.ToInt32(GridView1.DataKeys[rowIndex].Value);
    // load editor / redirect / populate form using loginId
}

Alternative: use CommandName/RowCommand so all row commands are centralized (set CommandName and CommandArgument in the template), then read the key from DataKeys:

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "EditUser")
    {
        int rowIndex = Convert.ToInt32(e.CommandArgument);
        int loginId = Convert.ToInt32(GridView1.DataKeys[rowIndex].Value);
        // proceed with edit using loginId
    }
}

Troubleshooting notes and cautions: confirm DataKeyNames is set before DataBind so DataKeys are populated; DataKeys are page-relative (row indices apply to the current page); for composite keys use DataKeys[rowIndex].Values["KeyName"]; avoid relying on cell indices or visible text; when using hidden fields or controls inside templates, use FindControl on the GridViewRow if needed; avoid exposing sensitive IDs in query strings without validation.

In the bound field include useid and hide it, and In the row command event just write the following code..

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            int index = Convert.ToInt32(e.CommandArgument);
            int UserId = 0;
               UserId = Convert.ToInt32(GridView1.Rows[index].Cells[1].Text.Trim());
         }

Cells[1] indicate the column no. in which user id is bounded.
Inform if you finds any problem regarding this
Regards
*Ranj*

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.