Popluting a gridview check boxes

Spike
Spike used Ask the Experts™
on
I've added a gridview to a webpage and can populate it with the list of items displaying checkboxes alongside each item. When the data is loded, I also want to either select, unselect the assciated check box dependant on data assoiated with the list item.

I'm populating it currently by:
SqlDataAdapter ad = new SqlDataAdapter("SELECT
            ID, thingsName FROM things",
            myConnection);

    DataSet ds = new DataSet();

    ad.Fill(ds);

    GridView1.DataSource = ds;

    GridView1.DataBind();

The gridview is created as:
<asp:GridView ID="GridView1" runat="server"
        AutoGenerateColumns="false">
    <Columns>
    <asp:TemplateField>
    <HeaderTemplate>
    <input type="checkbox" id="chkAll" name="chkAll"
        onclick="Check(this)" />
    </HeaderTemplate>

    <ItemTemplate>
    <asp:CheckBox ID="chkSelect" runat="server" />
    </ItemTemplate>

    </asp:TemplateField>
    <asp:TemplateField>
    <ItemTemplate>

    <asp:Label ID="lblthingsName" runat="server"
        Text = '<%# Eval("thingsName ") %>' />
    </ItemTemplate>
    </asp:TemplateField>

    </Columns>
</asp:GridView>


Many thanks in advance.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Umar Topia.Net Full Stack Developer
Commented:
You have two options

Either do it in ASPX only
or you can handle the RowDataBOund Event and do that in Code Behind
SpikeSoftware Developer

Author

Commented:
Do you have an example how you'd do it in the RowDataBound event?
Cheers
Top Expert 2015
Commented:
Here's the basic setup for RowDataBound in a Gridview to do what you require.
Protected Sub GridView1_RowDataBound(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim chkBox As CheckBox = CType(e.Row.FindControl("chkSelect"), CheckBox)
            Dim lbl As Label = CType(e.Row.FindControl("lblThingsName"), Label)
            If lbl.Text.Contains("some value") Then  'This can be whatever condition you require.
                chkBox.Checked = True
            End If
        End If
    End Sub

Open in new window

Top Expert 2015
Commented:
C sharp equivalent.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                CheckBox chkBox = (CheckBox)e.Row.FindControl("chkSelect");
                Label lbl = (Label)e.Row.FindControl("lblThingsName");
                if (lbl.Text.Contains("some value"))
                {   //This can be whatever condition you require.
                    chkBox.Checked = true;
                }
            }
        }

Open in new window

SpikeSoftware Developer

Author

Commented:
I'd actuall y solved it in the .aspx file using:

            <asp:GridView ID="GridView1" runat="server" BackColor="White"
                       BorderColor="#DEDFDE" BorderStyle="None"
                       BorderWidth="1px" CellPadding="4"
                       ForeColor="Black" GridLines="Vertical"
                       AutoGenerateColumns="false"
                       style="z-index: 103; left: 0px; position: relative; top: -2px"
                        Width="320px" ShowHeaderWhenEmpty="True">
                <Columns>
                    <asp:TemplateField>
                        <HeaderTemplate>
                            <input type="checkbox" id="chkAll" name="chkAll"
                            onclick="Check(this)" />
                        </HeaderTemplate>
                        <ItemTemplate>
                            <asp:CheckBox ID="chkSelect" runat="server"
                            AutoPostBack="false" Checked='<%# Convert.ToBoolean(Eval("isItValid")) %>'/>
                        </ItemTemplate>
                    </asp:TemplateField>

         
                    <asp:BoundField DataField="ID" HeaderText="Things ID" />      
                    <asp:BoundField DataField="thingsName" HeaderText="Things Name" />

                </Columns>
            </asp:GridView>

Works well, for what I need.

I thought i'd leave the post as it may be useful to others.
Cheers
SpikeSoftware Developer

Author

Commented:
My solution works well, as well!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial