Pramod
Pramod

Reputation: 667

How can I select all data from GridView's current row

How can I select all data from GridViews current row.. I have a column for edit link in GridView. When "Edit" link button is clicked, I want to use that selected row's data. I am trying the following code, but it's returning me with an empty value

protected void gv_RowEditing(object sender, GridViewEditEventArgs e)
{
    gv.EditIndex = -1;
    GridViewRow gvRow = gv.Rows[ e.NewEditIndex];
    string selectedID = gvRow.Cells[3].Text;
}

                 <asp:GridView runat = "server" ID="gvRange0" SkinID="gridView"                AutoGenerateColumns="False"  
                            AllowSorting="True"   OnRowCancelingEdit="gvRange_RowCancelingEdit" OnRowDeleting="gvRange_RowDeleting"
                            OnRowEditing="gvRange_RowEditing" OnRowUpdating="gvRange_RowUpdating" 
                                Width="684px"  OnRowDataBound="gvRange_RowDataBound"  
                            DataMember="DefaultView" OnPageIndexChanged="gvRange_PageIndexChanged" 
                            OnPageIndexChanging="gvRange_PageIndexChanging" OnSorting="gvRange_Sorting" DataKeyNames = "RANGE_ID" 
                            OnSelectedIndexChanged="gvRange_SelectedIndexChanged" Height="65px" >
                              <Columns>
                                  <asp:TemplateField ShowHeader="False">
                                      <ItemTemplate>
                                      <ControlStyle Width="2px" />
                                          <asp:LinkButton ID="lnkDelete0" runat="server" CssClass="lnk" 
                                              CausesValidation="False" CommandName="Delete"
                                              Text="Delete" Visible="false"></asp:LinkButton>
                                          <asp:CheckBox runat="server" ID="chkSelect" CssClass="lbl" Text="" AutoPostBack="False" OnCheckedChanged="chkSelect_CheckedChanged" />
                                      </ItemTemplate>
                                  </asp:TemplateField>
                                  <asp:TemplateField ShowHeader="False">
                                    <ItemTemplate>
                                    <controlStyle width="2px" />
                                      <asp:LinkButton ID="lnkEdit" runat="server" CssClass="lnk" CausesValidation="False" CommandName="Edit"
                                        Text="Edit" ></asp:LinkButton>

                                    </ItemTemplate>
                                    <ItemStyle Width="5px" />
                                  </asp:TemplateField>
                                  <asp:TemplateField HeaderText="Ranges" SortExpression="Sort_Ranges">
                                      <ControlStyle Width="5px" />
                                      <ItemTemplate>
                                      <%#DataBinder.Eval(Container.DataItem,"Min_Age") %>

                                      <%# CheckNull(DataBinder.Eval(Container.DataItem,"Max_Age")) %>
                                      </ItemTemplate>
                                     <%-- <ItemTemplate>--%>
                                          <%--<asp:Label ID="lblStageName" CssClass="lbl" runat="server" Text='<%# Bind("Age_Range") %>' Width="1px"></asp:Label>--%>
                                    <%--  </ItemTemplate>--%>
                                  </asp:TemplateField>
                                  <asp:TemplateField HeaderText="Range ID">
                                  <ItemTemplate><%#DataBinder.Eval(Container.DataItem,"RANGE_ID") %></ItemTemplate>
                                  </asp:TemplateField>
                              </Columns>                        
                          </asp:GridView>

There are 4 columns in the GridView. One contains check box, second is link button for edit, third databound with some value, fourth is the column which I want to use to get some values from database(that is primary key there) and this column is hidden.

Upvotes: 1

Views: 2172

Answers (2)

Jignesh Rajput
Jignesh Rajput

Reputation: 3558

sometime in gridview cell create child controls. you can try this code. may be solve it.

 protected void gv_RowEditing(object sender, GridViewEditEventArgs e)
 {
   gv.EditIndex = -1;

   GridViewRow gvRow= (GridViewRow)(((Button)e.CommandSource).NamingContainer);


  foreach (TableCell Tc in gvRow.Cells)
            {
               //if you are not getting value than find childcontrol of TabelCell.
                string sss = c.Text;
                foreach (Control ctl in Tc.Controls)
                {

                    //Child controls
                    Label lb = ctl as Label;
                    string s = lb.Text;
                    sb.Append(s + ',');
                }
            }
}

Upvotes: 1

Denys Wessels
Denys Wessels

Reputation: 17039

I've noticed that you say you need to access the 4th column but you're using gvRow.Cells[3].Text;

The indexing in the Cell object is done from 1, so if you need to access the 4th row in the grid view try this:

string selectedID = gvRow.Cells[4].Text;

EDIT:

Could you please confirm two things for me

1)When you click on lnkEdit is the GridView1_RowEditing event being raised?

2)If yes, is the e.NewEditIndex value always showing up as '0'?
Attempt clicking the edit link on different rows, is the result always '0'?

Upvotes: 0

Related Questions