开发者

Check/uncheck check boxes based on database value

开发者 https://www.devze.com 2023-03-01 12:14 出处:网络
I have a gridview which contains checkboxes and fields in sql server database whic开发者_JAVA技巧h has datatype bit.

I have a gridview which contains checkboxes and fields in sql server database whic开发者_JAVA技巧h has datatype bit.

If the value in database table is set to 1 then the checkbox in the gridview should be checked and disabled otherwise it should be unchecked and enabled.

This should happen at the time of databind. How to achive this task?


<asp:TemplateField>
    <ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" Enabled='<%# Eval("ColumnName") ? false : true %>' Checked='<%# Eval("ColumnName") %>' />
      </ItemTemplate>
  </asp:TemplateField>


You, have to give directly when ever you are declaring in the gridview like the following.

 <ItemTemplate>
    <asp:CheckBox ID="chkAlert1" runat="server" Visible="true" Enabled="false" Checked='<%# DataBinder.Eval(Container,"DataItem.Alert") %>' />
    <asp:CheckBox ID="chkAlert" runat="server" Visible="false" Enabled="true" Checked='<%# DataBinder.Eval(Container,"DataItem.Alert") %>' />
 </ItemTemplate>


You could use a TemplateField and the GridView's RowDataBound event to achieve what you want. Here is a complete example:

ASPX:

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

Codebehind:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If Not IsPostBack Then
        'create some Testdata'
        BindGrid()
    End If
End Sub

Private Sub BindGrid()
    Dim tbl As New DataTable
    Dim rnd As New Random
    tbl.Columns.Add(New DataColumn("MyBitColumn", GetType(Boolean)))
    For i As Int32 = 1 To 10
        Dim row As DataRow = tbl.NewRow
        row("MyBitColumn") = rnd.Next(1, 3) Mod 2 = 0 'get a random boolean'
        tbl.Rows.Add(row)
    Next
    Me.GridView1.DataSource = tbl
    Me.GridView1.DataBind()
End Sub

Private Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
    If e.Row.RowType = DataControlRowType.DataRow Then
        Dim row = DirectCast(e.Row.DataItem, DataRowView).Row
        Dim MyBitColumnValue = DirectCast(row("MyBitColumn"), Boolean)
        Dim ChkMyBitColumn = DirectCast(e.Row.FindControl("ChkMyBitColumn"), CheckBox)
        ChkMyBitColumn.Checked = MyBitColumnValue
        ChkMyBitColumn.Enabled = Not ChkMyBitColumn.Checked
    End If
End Sub


int o = 0;

        foreach (GridViewRow row in GridView1.Rows)
            {

                if (z == ds1.Tables[4].Rows[o]["Name"].ToString())
                {
                    CheckBox chk = (CheckBox)row.FindControl("chkusergroup");
                    chk.Checked = true;
                }
                else
                {
                    CheckBox chk = (CheckBox)row.FindControl("chkusergroup");
                    chk.Checked = false;


                }

            o++;





            }`enter code here`

Gridview1 is the Gridview ID. Z is a string which contains a value. I am checking for one column in every row whether it matches with 'z' and if it matches than set checkbox to checked. O is the counter to iterate in every row.

0

精彩评论

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

关注公众号