Microsoft Developer
Microsoft Developer

Reputation: 5459

Check/uncheck check boxes based on database value

I have a gridview which contains checkboxes and fields in sql server database which 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?

Upvotes: 0

Views: 5296

Answers (4)

ayush agarwal
ayush agarwal

Reputation: 1

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.

Upvotes: 0

Muhammad Akhtar
Muhammad Akhtar

Reputation: 52241

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

Upvotes: 3

Tim Schmelter
Tim Schmelter

Reputation: 460018

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

Upvotes: 0

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>

Upvotes: 2

Related Questions