0

I have wird problem with gridview and select checkbox for one row.

enter image description here

I get correct Text in Label, but if i select checkbox from first row:

enter image description here

I get: Room not pick

BUTTON CLICK

protected void bookButton_Click(object sender, EventArgs e) {

        foreach (GridViewRow row in GridView1.Rows)
        {


            var chk = (HtmlInputCheckBox) row.FindControl("checkboxID");
            int id_room = Convert.ToInt32(row.Cells[4].Text);
            if (chk.Checked)
            {
                Label1.Text = id_room.ToString();

                /* 
                String CS = ConfigurationManager.ConnectionStrings["DBConnection"].ConnectionString;
                 using (SqlConnection con = new SqlConnection(CS))
                 {
                        //STORED PROCEDURE CALL
                 }

            }

            else
            {

                Label1.Text ="Room not pick";
            }
                */
        }
    }

AND GRIDVIEW

                            <asp:GridView ID="GridView1" runat="server" CssClass="table border-0 table-hover" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" GridLines="None" BorderWidth="0px">

                <Columns>
                    <asp:BoundField DataField="Number" HeaderText="Number" SortExpression="Number" />
                    <asp:BoundField DataField="Picture" HeaderText="Picture" SortExpression="Picture" />
                    <asp:BoundField DataField="Price" HeaderText="Price" SortExpression="Price" />
                     <asp:BoundField DataField="ID"  HeaderText="IDP" SortExpression="ID" />
                    <asp:TemplateField>

                        <ItemTemplate>
                            <input type="checkbox" CssClass="custom-checkbox" ID="checkboxID" runat="server"  />
                        </ItemTemplate>

                        <ItemStyle HorizontalAlign="Center" />
                    </asp:TemplateField>

                </Columns>
            </asp:GridView>
2
  • show me you selected event or checkbox checked event Commented Jun 14, 2018 at 8:39
  • @ArunPratap I don't have. I check if chechbox is selected here: var chk = (HtmlInputCheckBox) row.FindControl("checkboxID"); int id_room = Convert.ToInt32(row.Cells[4].Text); if (chk.Checked) Commented Jun 14, 2018 at 8:44

2 Answers 2

0

Index is wrong. Its the 4 column. As its a zero based index, you should use 3

int id_room = Convert.ToInt32(row.Cells[3].Text);

A cleaner way will be to use ItemTemplate

<asp:TemplateField HeaderText="IDP" SortExpression="ID">
    <ItemTemplate>
        <asp:Label ID="roomID" runat="server" />
    </ItemTemplate>
</asp:TemplateField>

And access it like

foreach(GridViewRow row in GridView1.Rows) {
    var chk = (HtmlInputCheckBox)row.FindControl("checkboxID");
    var selectedRoomID = (Label)row.FindControl("roomID");
    if (chk.Checked) {
        Label1.Text = selectedRoomID.Text;
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

In gridview I have 5 columns: ROOMNUMBER | PICTURE | PRICE | CHECKBOX | ID_ROOM, so ID_ROOM is 4. column I think or not?
The checkbox comes after room_id. so checkbox is the 5th column and room_id is the 4th. So the zero based index will be 3
I change row.Cells[4].Text to row.Cells[3].Text and not change other code. It's still not working :(
0

Ok, thanks @naveen! I change the code like this:

 foreach (GridViewRow row in GridView1.Rows)
        {
            var chk = (HtmlInputCheckBox)row.FindControl("checkboxID");
            var selectedRoomID = (Label)row.FindControl("Label2");

            if (chk.Checked && chk != null)
            {

                Label1.Text = selectedRoomID.Text;
            }
            else
               {
              Label1.Text = "error";
                }

and change ID_ROOM col to ItemTemplates.

Comments

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.