3

i am trying to create a dynamic Gridview on a ASP.NET page and i have multiple rows, and added a CheckBox Column.

<body>
    <h1>Alerts</h1>
    <form id="form1" runat="server">
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:KiwiLogConnectionString %>" SelectCommand="SELECT * FROM [Syslogd]
GROUP BY MsgHostname, MsgDate, MsgTime, MsgPriority, MsgText"></asp:SqlDataSource>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="3" DataSourceID="SqlDataSource1" ForeColor="#333333" AllowPaging="True" PageSize="15">
            <AlternatingRowStyle BackColor="White" />
            <Columns>
                <asp:TemplateField >
                <ItemTemplate >
                <asp:CheckBox ID ="Checkbox" runat="server"/>
                </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="MsgDate" HeaderText="Datum" SortExpression="MsgDate" ItemStyle-Wrap="false" >
                    <ItemStyle Wrap="False"></ItemStyle>
                </asp:BoundField>
                <asp:BoundField DataField="MsgTime" HeaderText="Tijd" SortExpression="MsgTime" ItemStyle-Wrap="false" >
                    <ItemStyle Wrap="False"></ItemStyle>
                </asp:BoundField>
                <asp:BoundField DataField="MsgPriority" HeaderText="Priority" SortExpression="MsgPriority" ItemStyle-Wrap="false" >
                    <ItemStyle Wrap="False"></ItemStyle>
                </asp:BoundField>
                <asp:BoundField DataField="MsgHostname" HeaderText="Hostname" SortExpression="MsgHostname" ItemStyle-Wrap="false" >
                    <ItemStyle Wrap="False"></ItemStyle>
                </asp:BoundField>
                <asp:BoundField DataField="MsgText" HeaderText="Message" SortExpression="MsgText" />
            </Columns>
            <EditRowStyle BackColor="#2461BF" />
            <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
            <RowStyle BackColor="#EFF3FB" />
            <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
            <SortedAscendingCellStyle BackColor="#F5F7FB" />
            <SortedAscendingHeaderStyle BackColor="#6D95E1" />
            <SortedDescendingCellStyle BackColor="#E9EBEF" />
            <SortedDescendingHeaderStyle BackColor="#4870BE" />
        </asp:GridView>
        <asp:Button ID="btn_update" runat="server" OnClick="btn_update_Click" Text="Update" />
    </form>
  </body>

If that CheckBox is checked and the "Update" Button is clicked, i want these row(s) to be hidden. How can i manage to do that?

protected void btn_update_Click(object sender, EventArgs e)
{

}

The gridview is build up with the SQL Database, so it has to be dynamic. In foward, many thanks!

2 Answers 2

1

You can try the following:

protected void btn_update_Click(object sender, EventArgs e)
{
    foreach (GridViewRow gvr in GridView1.Rows) 
    {
            if (((CheckBox)gvr.findcontrol("Checkbox")).Checked == true)
            {
                //Do stuff with checked row
                gvr.Visible = false;
            }

    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Haha, no problem. Also, you never declared "Checkbox" in your sample, and the .Checked property is also supposed to be capitalized. You should probably check these things in an IDE before posting, just to be safe.
@TymeJV, this did not work. He doesn't know the .checked property. I am building the webform in Visual Studio 2012, do you have another solution?
@BassieBas1987 I made some edits. Let me know if this helps :)
0

I would use javascript for something like this.

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.