adamo ski
adamo ski

Reputation: 17

GridView - select first row doesn't work

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>

Upvotes: 0

Views: 1245

Answers (2)

adamo ski
adamo ski

Reputation: 17

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.

Upvotes: 0

codeandcloud
codeandcloud

Reputation: 55248

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;
    }
}

Upvotes: 0

Related Questions