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>

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.