0

I have a grid and what I am trying to do is when edit button is clicked of any row, the data of that row should be passed to textboxes. But I am unable to do so. I have used RowCommand but still unable to fetch the data.

asp code

<div class="form-group">
                            <label>FULL NAME</label>
                            <input type="text" class="form-control" runat="server" id="Name" autocomplete="off" />
                        </div>
                        <div class="form-group">
                            <label>GENDER</label><span class="required">*</span>
                            <select class="form-control" id="selectGender" runat="server" style="height: 34px;">
                                <option value="0">MALE</option>
                                <option value="1">FEMALE</option>
                            </select>
                        </div>

<div class="col-md-12">
                        <asp:GridView ID="famGrid" Visible="false"
                            runat="server" OnRowCommand="famGrid_RowCommand">
                            <Columns>
                                <asp:TemplateField HeaderText="S No." ItemStyle-Width="3%">
                                    <ItemTemplate>
                                        <%#Container.DataItemIndex+1 %>
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:BoundField
                                    HeaderText="FULL NAME" DataField="Name">
                                    <ItemStyle VerticalAlign="Top" />
                                </asp:BoundField>
                                <asp:BoundField
                                    HeaderText="GENDER" DataField="selectGender">
                                    <ItemStyle VerticalAlign="Top" />
                                </asp:BoundField>                                
                                <asp:TemplateField HeaderText="EDIT">
                                        <ItemTemplate>
                                            <asp:Button Text="EDIT" ID="btnEdit" CommandName="ChangeStatus" formnovalidate="formnovalidate" runat="server" />
                                        </ItemTemplate>
                                    </asp:TemplateField>

                            </Columns>
                        </asp:GridView>

code behind

protected void famGrid_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            try
            {
                int selectedRowIndex = -1;
                if (e.CommandName == "ChangeStatus")
                {
                    DataTable dt = ViewState["FamilyTable"] as DataTable;                    
                    GridViewRow gvr = famGrid.SelectedRow;
                    lblRow.Text = gvr.Cells[1].Text;
                    Name.Value = gvr.Cells[2].Text;
                    selectGender.Items[selectGender.SelectedIndex].Text = gvr.Cells[3].Text;
                }
            }
            catch (Exception)
            {

                throw;
            }
        }

I am getting error null reference error.

user4221591
  • 1,795
  • 5
  • 26
  • 50
  • In which line the error thrown? Perhaps you should look this first: [How to solve `NullReferenceException` issue](https://stackoverflow.com/q/4660142). – Tetsuya Yamamoto Apr 26 '18 at 00:55

2 Answers2

0

I've used SelectedIndexChanged. It is working.

protected void famGrid_SelectedIndexChanged(object sender, EventArgs e)
        {
            GridViewRow gvr = famGrid.SelectedRow;
            lblRow.Text = gvr.Cells[1].Text;
            Name.Value = gvr.Cells[2].Text;
            selectGender.Items[selectGender.SelectedIndex].Text = gvr.Cells[3].Text;
        }
user4221591
  • 1,795
  • 5
  • 26
  • 50
0

Please let us know i which line you are getting exception.

Gagan Deep
  • 1,430
  • 7
  • 13