0

In the below code I have a datagrid, in which I have 2 columns userid and documentid and a button. I want to get that userid of particular row, how can I do this?

    <asp:DataGrid Width="100%" ID="DocumentUsergrid" 
                       PagerStyle-Mode="NumericPages" PageSize="10" PagerStyle-Visible="true" 
                            AllowPaging="true" AllowSorting="true"  
                        AlternatingRowStyle-CssClass="alt">
                        <AlternatingItemStyle BackColor="White" />
     <asp:TemplateColumn HeaderText="DocumentID" ItemStyle-Width="150px" Visible="false" >                             
                                 <ItemTemplate>                                              
                                  <asp:Label ID="lblOutputProductID_i"  runat="server"  Text='<%#Eval("DocumentID") %>' > </asp:Label>
                                 </ItemTemplate>                                             
                              </asp:TemplateColumn> 

     <asp:TemplateColumn HeaderText="UserID" ItemStyle-Width="150px" Visible="false" >                             
                                 <ItemTemplate>                                              
                                   <asp:Label ID="lblSupportProductID_i"  runat="server"  Text='<%#Eval("UserID") %>' > </asp:Label>
                                 </ItemTemplate>                                             
                              </asp:TemplateColumn> 
    <asp:TemplateColumn HeaderText="Insert" ItemStyle-Width="150px">
                                  <ItemTemplate>                               
                               <asp:Button ID="btnSubmit" runat ="server" OnClick="btnSubmit_Click" /> 
                               </ItemTemplate>  
 </Columns>                       

                  <HeaderStyle Font-Bold="True" ForeColor="White" />
                   <ItemStyle BackColor="#EFF3FB"/>

                </asp:DataGrid>

protected void btnSubmit_Click(object sender, EventArgs e)
{
    try
    {
        Dictionary<string, string> OutputProductVal = new Dictionary<string, string>();
        OutputProductVal.Add("UserID", );//To get userid of particular row
        OutputProductVal.Add("DocumentID", ddlDocumentName.SelectedValue.ToString());
        DocumentServiceClient oProduct = new DocumentServiceClient();
        oProduct.InsertUserDocumentMap(OutputProductVal);
    }
}
Ramon Snir
  • 7,520
  • 3
  • 43
  • 61

2 Answers2

0

You can find the user id label for the row in the grid view.

foreach(DataGridItem item in DocumentUsergrid.Items)
{
    Label l = item.FindControl("lblSupportProductID_i") as Label;
    string userId = l.Text;
}
Christos
  • 53,228
  • 8
  • 76
  • 108
Kiran Hegde
  • 3,651
  • 1
  • 16
  • 14
0

Use a commandField column like that:

In the ASPX

<asp:CommandField ShowSelectButton="True" />

In the code-behind

Handles the DocumentUsergrid.SelectedIndexChanged event to do what you want.

 protected void DocumentUsergrid_SelectedIndexChanged(object sender, EventArgs e)
 {
 }

In the event code, you can retrieve the data like that:

row = YOUR_GRID.Rows(e.RowIndex);

For more informations about the selectIndexChanged event, see http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.selectedindexchanged(v=vs.110).aspx

Nicolas Henrard
  • 843
  • 8
  • 19