2

I have a gridview with checkboxes to select the row. On checking the checkbox I need to get the row values into a string/session. Below is the code

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"                 
    OnRowCancelingEdit="GridView1_RowCancelingEdit"    
    OnRowEditing="GridView1_RowEditing"  OnRowUpdating="GridView1_RowUpdating"  OnRowDeleting="GridView1_OnRowDeleting"  OnPageIndexChanging="GridView1_PageIndexChanging" OnSelectedIndexChanged="GridView1_SelectedIndexChanged"  Width ="1000px" class="grid" AllowPaging="True" PagerSettings-FirstPageText="First" PagerSettings-LastPageText="Last" PageButtonCount="2" PagerSettings-Mode="NumericFirstLast" PageSize="5">
    <PagerSettings Mode="NumericFirstLast" PageButtonCount="2"  FirstPageText="First" LastPageText="Last"/> 
    <Columns>   
       <asp:TemplateField HeaderText="Id">   
            <ItemTemplate>   
               <asp:CheckBox ID="CheckBox3" runat="server" />
            </ItemTemplate>   
        </asp:TemplateField>   
        <asp:TemplateField HeaderText="Connection">   
            <ItemTemplate>   
                <asp:Label ID="lbl_conn" runat="server" Text='<%#Eval("Connection") %>'></asp:Label>   
            </ItemTemplate>   
        </asp:TemplateField>   

                        <asp:TemplateField HeaderText="UserID">   
            <ItemTemplate>   
                <asp:Label ID="lbl_Usrid" runat="server" Text='<%#Eval("UserID") %>'></asp:Label>   
            </ItemTemplate>   
            <EditItemTemplate>   
                <asp:TextBox ID="txt_Usrid" runat="server" Text='<%#Eval("UserID") %>'></asp:TextBox>   
            </EditItemTemplate>   
        </asp:TemplateField> 
        <asp:TemplateField HeaderText="Password">   
            <ItemTemplate>   
                <asp:Label ID="lbl_pwd" runat="server" Text='<%#Eval("Password") %>'></asp:Label>   
            </ItemTemplate>   
            <EditItemTemplate>   
                <asp:TextBox ID="txt_pwd" runat="server" Text='<%#Eval("Password") %>'></asp:TextBox>   
            </EditItemTemplate>   
        </asp:TemplateField> 
        <asp:TemplateField HeaderText="Connection Name">   
            <ItemTemplate>   
                <asp:Label ID="lbl_conName" runat="server" Text='<%#Eval("Connection_Name") %>'></asp:Label>   
            </ItemTemplate>   
            <EditItemTemplate>   
                <asp:TextBox ID="txt_conName" runat="server" Text='<%#Eval("Connection_Name") %>'></asp:TextBox>   
            </EditItemTemplate>   
        </asp:TemplateField>   


           <asp:TemplateField HeaderText="Edit">   
            <ItemTemplate>   
                <asp:Button ID="btn_Edit" runat="server"  Text=" Edit" class=" btnEdit"  CommandName="Edit" />   
            </ItemTemplate>   
            <EditItemTemplate>   
                <asp:Button ID="btn_Update" runat="server" class=" btnEdit" Text="Update" CommandName="Update"/>   
                <asp:Button ID="btn_Cancel" runat="server" class=" btnEdit" Text="Cancel" CommandName="Cancel"/>   
            </EditItemTemplate>   
        </asp:TemplateField>  
       <asp:TemplateField HeaderText="Delete">   
            <ItemTemplate>   
                <asp:Button ID="btn_Delete" runat="server" class=" btnDelete" Text="Delete" CommandName="Delete" OnClientClick="return confirm('Are you sure you want to delete this event?')" />   
            </ItemTemplate>   

        </asp:TemplateField> 
    </Columns>   
</asp:GridView>   

There is a button below the grid. on click of the button I need to get the values.

protected void LinkButton1_Click(object sender, EventArgs e)
{
    foreach (GridViewRow item in GdvTestData.Rows)
    {
        CheckBox chk = (item.FindControl("CheckBox3") as CheckBox);
        if (chk.Checked)
        {
            string conn = item.Cells[1].Text;
        }
    }
}

But am getting null value for string conn = item.Cells[1].Text; where am I going wrong

1 Answer 1

3

Grid contains the different row type like header row , data row and footer row. You need to get content from the data row only then please check the row type first if it is a data row then try to get cell values. GridViewRow.RowType Property

foreach(GridViewRow item in GdvTestData.Rows) {
// check row is datarow
 if (item.RowType == DataControlRowType.DataRow) {
    CheckBox chk = (item.FindControl("CheckBox3") as CheckBox);
    if (chk.Checked) 
    {          
       Label MyLabel = (Label)item.FindControl("lbl_conn");  
       string conn = MyLabel.Text;   
    }
 }
}
Sign up to request clarification or add additional context in comments.

5 Comments

On checking the row type also I am getting null value
And I think it should be if (item.RowType == DataControlRowType.DataRow)
I also need only one checkbox to be selected at a time. Now I am able to select all checkboxes. (It should work like radio button inside gridview)
On checking the checkbox it doesn't fire selectedindexchange event, I want to change the selected row color
I tried with this solution but expression result is _chk = {Text = "" Checked = false} although I checked before.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.