user2971598
user2971598

Reputation: 11

Add check box to gridview

I've a grid view with three columns Employee name Employee details and Employee age. I want to add a check box at each row and after selection of each check box i want to fire a insert query associated to that employee. Can you tell me how to add this dynamic functionality to the grid view.

also if we use <%# EVAL %> i don't know how to implement it with checkbox.

Upvotes: 0

Views: 5535

Answers (3)

Hasan Sajedi
Hasan Sajedi

Reputation: 1

you can use TemplateFields for the GridView:

<asp:GridView ID="GridView1" runat="server">
<Columns>
<asp:TemplateField >
<ItemTemplate>
<asp:CheckBox ID="myCheckBox" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

and in code behind use below code:

ClusterName = GV.Rows(1).Cells(2).FindControl("myLabelinTheGridViewTemplateField")

Upvotes: 0

Samiey Mehdi
Samiey Mehdi

Reputation: 9424

ASPX:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
            DataSourceID="SqlDataSource1">
            <Columns>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:CheckBox ID="CheckBox1" runat="server" />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="name" HeaderText="name" SortExpression="name" />
                <asp:BoundField DataField="details" HeaderText="details" 
                    SortExpression="details" />
                <asp:BoundField DataField="age" HeaderText="age" SortExpression="age" />
            </Columns>
        </asp:GridView>
        <br />
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="OK" />
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:connApps %>" 
            SelectCommand="SELECT [name], [details], [age] FROM [tblA]">
        </asp:SqlDataSource>
        <br />
        <asp:Label ID="Label1" runat="server"></asp:Label>

Code behind:

protected void Button1_Click(object sender, EventArgs e)
{
    Label1.Text = "Selected item name:<br>";
    foreach (GridViewRow  item in GridView1.Rows)
    {
        CheckBox chk = (CheckBox)item.FindControl("CheckBox1");
        if (chk != null)
        {
            if (chk.Checked)
            {
                Label1.Text += item.Cells[1].Text + "<br>";
            }
        }
    }
}

Output:

enter image description here

Upvotes: 1

zey
zey

Reputation: 6103

Here is an example ,

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
BackColor="White" BorderColor="#336699" BorderStyle="Solid" BorderWidth="1px"  
CellPadding="0" CellSpacing="0" DataKeyNames="CategoryID" Font-Size="10"
Font-Names="Arial" GridLines="Vertical" Width="40%">

        <Columns>            
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:CheckBox ID="chkStatus" runat="server"
                        AutoPostBack="true" OnCheckedChanged="chkStatus_OnCheckedChanged"
                        Checked='<%# Convert.ToBoolean(Eval("Approved")) %>'
                        Text='<%# Eval("Approved").ToString().Equals("True") ? " Approved " : " Not Approved " %>' />
                </ItemTemplate>                   
            </asp:TemplateField>

            <asp:BoundField DataField="CategoryID" HeaderText="CategoryID" />                   
            <asp:BoundField DataField="CategoryName" HeaderText="CategoryName"  />
        </Columns>

<HeaderStyle BackColor="#336699" ForeColor="White" Height="20" />

For more information , check Here !

Upvotes: 0

Related Questions