开发者

How to make a Gridview Header Check Box

Using VB.Net (Windows Application)

I am using checkbox in the girdview column, now i want to add a checkbox in the header.

If i select the checkbox in the header, all the checkbox in the column should select automatically.

开发者_高级运维

How to do this.

Need VB.Net Code Help


Add the first Checkbox (Handle CheckedChanged event and set AutoPostBack=True) in HeaderTemplate and add second checkbox in ItemTemplate of TemplateField.

Markup

<asp:GridView 
    ID="GridView1" 
    runat="server"
    AutoGenerateColumns="false"
    >
<Columns>
    <asp:TemplateField>
        <HeaderTemplate>
            <asp:CheckBox 
                ID="CheckBox1" 
                runat="server"
                AutoPostBack="true"
                OnCheckedChanged="CheckUncheckAll"
                />
        </HeaderTemplate>
        <ItemTemplate>
            <asp:CheckBox ID="CheckBox2" runat="server" /> 
        </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField>
        <HeaderTemplate>
            Name
        </HeaderTemplate>
        <ItemTemplate>
            <asp:Literal 
                ID="Literal1" 
                runat="server"
                Text='<%#Eval("Name") %>'
                >
            </asp:Literal>
        </ItemTemplate>
    </asp:TemplateField>
</Columns>
</asp:GridView>

Code-Behind

  Public Class Data
        Public Property Name As String
    End Class

    Protected Sub CheckUncheckAll(sender As Object, e As System.EventArgs)
        Dim chk1 As CheckBox
        chk1 = DirectCast(GridView1.HeaderRow.Cells(0).FindControl("CheckBox1"),CheckBox)
        For Each row As GridViewRow In GridView1.Rows
            Dim chk As CheckBox
            chk =DirectCast(row.Cells(0).FindControl("CheckBox2"),CheckBox)
            chk.Checked = chk1.Checked
        Next
    End Sub
    Dim lst As List(Of Data)
    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            lst = New List(Of Data)
            lst.Add(New Data() With {.Name = "A"})
            lst.Add(New Data() With {.Name = "B"})
            lst.Add(New Data() With {.Name = "C"})

            GridView1.DataSource = lst
            GridView1.DataBind()
        End If
    End Sub


Protected Sub chkbSelectAll_CheckedChanged(sender As Object, e As EventArgs)
    Dim chkFlag As Boolean = False
    If chkbSelectAll.Checked Then
        chkFlag = True
    End If
    For Each dr As GridViewRow In gvProducts.Rows
        Dim chk As CheckBox = DirectCast(dr.Cells(0).FindControl("Select"), CheckBox)
        chk.Checked = chkFlag
    Next
End Sub

I'm not sure this code is well-written. I just convert from this C# code (which is working);

protected void chkbSelectAll_CheckedChanged(object sender, EventArgs e)
    {
        bool chkFlag = false;
        if (chkbSelectAll.Checked) chkFlag = true;
        foreach (GridViewRow dr in gvProducts.Rows)
        {
            CheckBox chk = (CheckBox)dr.Cells[0].FindControl("Select");
            chk.Checked = chkFlag;
        }
    }

Algorithm is simple. Define a boolean value for checking checkboxes are selected or not. If checked assign it true. for all gridview rows, find controls with columns name then set them all.


You need to use Header template in order to render a checkbox. And you will need to some javascript to check all the rows when header checked.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜