ognale88
ognale88

Reputation: 169

How to Set the Checkbox value inside GridView?

How to set the CheckBox value, which is located inside a Gridview ?

  <asp:GridView ID="gviewPermission" runat="server" 
        onrowdatabound="gviewPermission_RowDataBound" 
        onrowupdated="gviewPermission_RowUpdated" 
        onrowupdating="gviewPermission_RowUpdating">
        <Columns>
            <asp:TemplateField HeaderText="Allow" ItemStyle-HorizontalAlign="Center">
                <ItemTemplate>
                    <asp:CheckBox ID="Check_Allow" runat="server" />
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Deny" ItemStyle-HorizontalAlign="Center">
                <ItemTemplate>
                    <asp:CheckBox ID="Check_Deny" runat="server" />
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

The check box value has to set based upon some condition....

Upvotes: 0

Views: 18367

Answers (4)

Shridhar
Shridhar

Reputation: 2468

  <ItemTemplate>
            <asp:CheckBox runat="server" checked='<%# bool.Parse(Eval("check").ToString()) %>'  ID="chkselet" />
  </ItemTemplate>

check value must be true or false

Upvotes: 0

RAJESH KUMAR
RAJESH KUMAR

Reputation: 517

If the value of column is boolean then. Try the below code

<ItemTemplate>
  <center>
      <asp:CheckBox ID="chkSelect" Checked='<%#Convert.ToBoolean(Eval("isChecked"))%>' runat="server"></asp:CheckBox></center>
 </ItemTemplate>

Where "isChecked" is the column name.

Upvotes: 3

Magnus
Magnus

Reputation: 46947

In the gviewPermission_RowDataBound function do:

if(e.Row.RowType == DataControlRowType.DataRow)
   ((CheckBox)e.Row.FindControl("Check_Allow")).Checked = SomeCondition;

Or if the condition is coming directly from the datasource you can do:

 <ItemTemplate>
    <asp:CheckBox ID="Check_Allow" runat="server" 
                  Checked='<%# Eval("ConditionFromDs") %>' />
 </ItemTemplate>

Upvotes: 6

ElGavilan
ElGavilan

Reputation: 6904

the CheckBox control has an attribute called Checked that works similarly to the html counterpart attribute. So set this attribute either in the aspx markup:

<asp:CheckBox ID="Check_Allow" runat="server" Checked='<%= someCondition == true %>' />

or in your code-behind.

Upvotes: 0

Related Questions