By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,136 Members | 1,212 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,136 IT Pros & Developers. It's quick & easy.

default value for checkbox in a datagrid.

P: n/a
greetings,
i'm loading a datagrid and my gird has checkbox, i want these checkboxex to
be check based on the value from the db? can someone please show me the code
for it? thanks
Nov 18 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Depending on your data, you might be able to use an IIF statement inline to
indicate whether the checkbox is checked:

<asp:DataGrid id="DataGrid1" runat="server">
<Columns>
<asp:TemplateColumn HeaderText="Boolean Value">
<ItemTemplate>
<asp:CheckBox id=match runat="server" onCheckedChanged="updateStatus"
Checked='<%# DataBinder.Eval(Container, "DataItem.Boolean") %>'
AutoPostBack="True">
</asp:CheckBox>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Boolean Value">
<ItemTemplate>
<asp:CheckBox id="Checkbox1" runat="server"
onCheckedChanged="updateStatus" Checked='<%# IIF(DataBinder.Eval(Container,
"DataItem.Boolean"),"true","false") %>' AutoPostBack="True">
</asp:CheckBox>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Boolean Value">
<ItemTemplate>
<asp:Label id="Label1" runat="server"><%#
IIF(DataBinder.Eval(Container, "DataItem.Boolean"),"Yes","No")
%></asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>

Ken
MVP [ASP.NET]
"Asha" <As**@discussions.microsoft.com> wrote in message
news:7F**********************************@microsof t.com...
greetings,
i'm loading a datagrid and my gird has checkbox, i want these checkboxex
to
be check based on the value from the db? can someone please show me the
code
for it? thanks


Nov 18 '05 #2

P: n/a
Option 1 - Used the Checked attribute of the checkbox.
<ItempTemplate>
<asp:Checkbox RunAt=Server Id="chkActive" Checked='<%# DataBinder.Eval
(Container.DataItem, "<col_name>") %>' />
</ItempTemplate>
Here, checkbox is checked based on the <col_name> column

Option 2 - Use an utility function.
<ItempTemplate>
<asp:Checkbox RunAt=Server Id="chkActive" Checked='<%#
IsActive(DataBinder.Eval (Container.DataItem, "<col_name>")) %>' />
</ItempTemplate>
IsActive is a protected method (can be any name) in the page returning True
or False based on the parameter passed.

Well, the 3rd option is to use ItemDateBound event of the grid.

HTH.

"Asha" <As**@discussions.microsoft.com> wrote in message
news:7F**********************************@microsof t.com...
greetings,
i'm loading a datagrid and my gird has checkbox, i want these checkboxex to
be check based on the value from the db? can someone please show me the code
for it? thanks

Nov 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.