2

This is my markup:-

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        Width="245px" onrowcommand="GridView1_RowCommand" >
        <Columns>

            <asp:TemplateField>
            <ItemStyle BackColor="#CCCCCC" ForeColor="Black" Width="250px" HorizontalAlign="Center"
                    BorderStyle="None" />
                    <ItemTemplate>
                        <asp:LinkButton ID="userList" runat="server" CommandName="Select" CommandArgument ='<%# Container.DataItemIndex %>' Text='<%# Bind("users") %>'></asp:LinkButton>
                    </ItemTemplate>
            </asp:TemplateField>

        </Columns>        
    </asp:GridView>

And in the codebehind, I am trying to get the current row's text value, but can't seem to get it;

it returns "".

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    int rowValue = Convert.ToInt32(e.CommandArgument.ToString());
    GridView1.SelectedIndex = rowValue;
    string test = GridView1.SelectedRow.Cells[0].Text;
}
Soham Banerjee
  • 465
  • 1
  • 8
  • 18
  • The above code gives `Index was out of range.` Exception at `GridView1.SelectedRow`. Is this the full html markup? – SelvaS May 05 '15 at 07:04

2 Answers2

8

You need to find and acces the control

LinkButton btn = (LinkButton)gvDealerSupportMail.Rows[rowValue].FindControl("userList");
string result = btn.Text;
fubo
  • 44,811
  • 17
  • 103
  • 137
1

you can use CommandSource to get the Command.Text, CommandName and CommandArgument.

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        LinkButton commandSource = e.CommandSource as LinkButton;
        string commandText = commandSource.Text;
        string commandName = commandSource.CommandName;
        int commandArgument = Convert.ToInt32(commandSource.CommandArgument);
    }
SelvaS
  • 2,105
  • 1
  • 22
  • 31