Gerdy
Gerdy

Reputation: 1

Can't get value from gridview button field

I am facing difficulties when trying to get values from a cell in a gridview. Whenever I ask for the value it goes to NullExcaption, so I asked if the row itself is null, and it is. Here is the ASP code:

<asp:SqlDataSource ID="SqlDataSourceFlats" runat="server" ConnectionString="<%$ ConnectionStrings:EflatsConnectionString %>" SelectCommand="SELECT [Rent], [Address], [FlatId] FROM [Flat_Main]"></asp:SqlDataSource>
                      <asp:GridView ID="GridView1"  onrowcommand="GridView1_RowCommand" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" CellPadding="4" DataKeyNames="FlatId" DataSourceID="SqlDataSourceFlats" ForeColor="#333333" GridLines="None" Width="100%"  >
                          <AlternatingRowStyle BackColor="White" />
                          <Columns>
                              <asp:BoundField DataField="FlatId" HeaderText="FlatId" InsertVisible="False" ReadOnly="True" SortExpression="FlatId" />
                              <asp:BoundField DataField="Rent" HeaderText="Rent" SortExpression="Rent" />
                              <asp:BoundField DataField="Address" HeaderText="Address" SortExpression="Address">
                              <ControlStyle Width="60%" />
                              </asp:BoundField>


                               <asp:ButtonField Text="Add"  CommandName="Apply" ButtonType="Button" />
                              <asp:ButtonField Text="Remove" ButtonType="Button" />
                          </Columns>
                          <EditRowStyle BackColor="#7C6F57" />
                          <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
                          <HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
                          <PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
                          <RowStyle BackColor="#E3EAEB" />
                          <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
                          <SortedAscendingCellStyle BackColor="#F8FAFA" />
                          <SortedAscendingHeaderStyle BackColor="#246B61" />
                          <SortedDescendingCellStyle BackColor="#D4DFE1" />
                          <SortedDescendingHeaderStyle BackColor="#15524A" />
                      </asp:GridView>

And the button code:

 protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "Apply")
    {
        //  string c = GridView1.Rows[e.NewEditIndex].Cells[2].Text;
        //  Response.Write(GridView1.SelectedRow.Cells[2].Text);
             if (GridView1.SelectedRow == null)
            {
                 Response.Write("Gridview is failing me :(");
             }


    //    string celltext = this.GridView1.SelectedRow.Cells[2].Text;
    //    Response.Write(celltext);
    }
}

Basically I want to click the button and get the FlatId value from the selected row.

Upvotes: 0

Views: 1191

Answers (1)

R.C
R.C

Reputation: 10565

The OnRowCommand event seems to have few mistakes.

1.) GridView1.SelectedRow::

you haven't actually selected any row, therefore this will always be null

2.) GridView1.Rows[e.NewEditIndex]::

Again, you are NOT editing any row, so what do you expect the value will be for e.NewEditIndex ?

How to Handle ButtonField events in the GridView control

  1. Set the button's CommandName property to a string that identifies its function, such as "Select" , "Edit" etc...
  2. To determine the index of the record, the RowIndex, use the CommandArgument property of the event argument that is passed to the command event for the data-bound control. The ButtonField class automatically populates the CommandArgument property with the appropriate row-index value.

Below is an example for selecting a record and Handling the OnRowCommand event:

Markup:

<columns>
      <asp:buttonfield buttontype="Button" commandname="Select"
                headertext="Select This Record" text="Select"/>
</columns>

and the OnRowCommand event:

void GridView1_RowCommand(Object sender, GridViewCommandEventArgs e)
  {
    if(e.CommandName=="Select")
    {
      // Convert the row index stored in the CommandArgument
      // property to an Integer.
      int _rowIndex = Convert.ToInt32(e.CommandArgument);    

      GridViewRow selectedRow = CustomersGridView.Rows[_rowIndex];  
      string cellText = selectedRow.Cells[2].Text;  
    }
  }

Upvotes: 1

Related Questions