0

i am trying to pick a value from gridview but i returns empty string.

<asp:GridView ID="GridViewLedger" runat="server" Width="100%" AutoGenerateColumns="False"
                        ShowFooter="True" DataKeyNames="AccountID" OnRowCommand="GridViewLedger_RowCommand"
                        CssClass="table table-hover table-striped table-bordered">
                        <Columns>


                            <asp:TemplateField HeaderText="InvoiceNo" SortExpression="InvoiceNo">
                              <ItemTemplate>
                                <asp:LinkButton ID="btnClickInvoiceNo" runat="server" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" CommandName="InvoiceNo" Text='<%# Eval("InvoiceNo") %>' />
                              </ItemTemplate>
                            </asp:TemplateField>

                        </Columns>
                    </asp:GridView>

I deleted all irrelevant columns here,

protected void GridViewLedger_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        int index = Convert.ToInt32(e.CommandArgument);
        string InvoiceNo = GridViewLedger.Rows[index].Cells[4].Text;
    }
Aamir Shah
  • 93
  • 1
  • 2
  • 14

2 Answers2

2

You can't access a Text of a LinkButton inside an ItemTemplate of a TemplateField by accessing .Text of a rowcell.

What you can do is

LinkButton lbInvoiceNo = GridViewLedger.Rows[index].Cells[4].FindControl("btnClickInvoiceNo");
string invoiceNo = lbInvoiceNo.Text;
CurseStacker
  • 1,079
  • 8
  • 19
0

try..

    if (e.CommandName == "InvoiceNo")
    {
        int index = Convert.ToInt32(e.CommandArgument);
        string InvoiceNo = GridViewLedger.Rows[index].Cells[4].Text;
    }

Try2:

       if (e.CommandName == "InvoiceNo")
       {
        int index = Convert.ToInt32(e.CommandArgument);
        GridViewRow row = GridViewLedger.Rows[index];
        string InvoiceNo=row.Cells[4].Text;
       }
A_Sk
  • 4,532
  • 3
  • 27
  • 51