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

CheckBox in DataGrid

P: n/a
Hello all

is it possible to add a checkbox in a DataGrid for
Boolean Data?

Thanks in advance
Nov 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
yes!
here's how to add checkbox items to a datagrid in asp.net using c#
e.g. datagrid html code:

.....(html and other bits)
<asp:datagrid id="DgUserPermissions" style="Z-INDEX: 101; LEFT: 44px;
POSITION: absolute; TOP: 152px" runat="server" AutoGenerateColumns="False"
Height="75px" Width="598px" DataKeyField="Project_User_ID">
<AlternatingItemStyle BackColor="LightGray"></AlternatingItemStyle>
<HeaderStyle BackColor="Gray"></HeaderStyle>
<Columns>
<asp:EditCommandColumn ButtonType="PushButton" UpdateText="Update"
CancelText="Cancel" EditText="Edit"></asp:EditCommandColumn>
.....(some other columns)
<asp:TemplateColumn HeaderText="Disabled">
<ItemStyle HorizontalAlign="Center"></ItemStyle>
<ItemTemplate>
<asp:CheckBox id="Disabled" runat="server" Checked='<%#
DataBinder.Eval(Container.DataItem, "<NAME OF DATASET COL GOES HERE!!>")
%>'>
</asp:CheckBox>
</ItemTemplate>
</asp:TemplateColumn>
......(some more columns and html)
which is bound to a dataset (in my case dynamically not at design time)
during the pageload -> if (!page.ispostback) etc.
the dataset column is a bit column in this case

the databinder.eval appears (from what i can tell) to convert the 'bit'
value to something that the asp:checkbox can cope with
if you know what datatype is returned you can put a load of stuff in with
formatting and so on
(note the special syntax "<%#=" this is only executed when the 'databind'
method is called)

and to get the values out?
assuming that you are using the update/edit/cancel button stuff provided, in
your 'update' event
something like this:
(on the codebehind page)
private void DgUserPermissions_Update(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)

put
CheckBox cb = (CheckBox)e.Item.Cells[2].FindControl("Disabled");
Boolean Disabled = cb.Checked;

2 means second column in the grid in this case, the "(checkbox)e...." syntax
is casting the control into a checkbox (which it is, seems silly to me)
you can then access the value
/*
i have had problems where by the edit/cancel buttons were being added but
the hander was missing
(found inside the unexpanded) "Web Form Designer generated code" section
so you have to add in something like (again on the code behind page)
this.DgUserPermissions.CancelCommand += new
System.Web.UI.WebControls.DataGridCommandEventHand ler(this.DgUserPermissions
_Cancel);
this.DgUserPermissions.EditCommand += new
System.Web.UI.WebControls.DataGridCommandEventHand ler(this.DgUserPermissions
_Edit);
this.DgUserPermissions.UpdateCommand += new
System.Web.UI.WebControls.DataGridCommandEventHand ler(this.DgUserPermissions
_Update);
you can put whatever you like in with template columns drop downs, special
formatting and so on
if you don't like c# then make the effort to learn it
i was a vb6 programmer of 5 years and have made the switch (eventually!)

[there are some in depth articles about the datagrid on the 4guysfromrolla
website]

"Sebi" <an*******@discussions.microsoft.com> wrote in message
news:0a****************************@phx.gbl...
Hello all

is it possible to add a checkbox in a DataGrid for
Boolean Data?

Thanks in advance

Nov 17 '05 #2

P: n/a
Thanks

It all worked out!

I'm trying to handle the change of the CheckBox directly.

Don't know yet how to find out which row I'm on but I
hope I'll find out!

I AM C# developer (came from c++) - Thank you!

Sebi
-----Original Message-----
yes!
here's how to add checkbox items to a datagrid in asp.net using c#e.g. datagrid html code:

.....(html and other bits)
<asp:datagrid id="DgUserPermissions" style="Z-INDEX: 101; LEFT: 44px;POSITION: absolute; TOP: 152px" runat="server" AutoGenerateColumns="False"Height="75px" Width="598px" DataKeyField="Project_User_ID"> <AlternatingItemStyle BackColor="LightGray"></AlternatingItemStyle> <HeaderStyle BackColor="Gray"></HeaderStyle>
<Columns>
<asp:EditCommandColumn ButtonType="PushButton" UpdateText="Update"CancelText="Cancel" EditText="Edit"></asp:EditCommandColumn>.....(some other columns)
<asp:TemplateColumn HeaderText="Disabled">
<ItemStyle HorizontalAlign="Center"></ItemStyle>
<ItemTemplate>
<asp:CheckBox id="Disabled" runat="server" Checked='<%#DataBinder.Eval(Container.DataItem, "<NAME OF DATASET COL GOES HERE!!>")%>'>
</asp:CheckBox>
</ItemTemplate>
</asp:TemplateColumn>
......(some more columns and html)
which is bound to a dataset (in my case dynamically not at design time)during the pageload -> if (!page.ispostback) etc.
the dataset column is a bit column in this case

the databinder.eval appears (from what i can tell) to convert the 'bit'value to something that the asp:checkbox can cope with
if you know what datatype is returned you can put a load of stuff in withformatting and so on
(note the special syntax "<%#=" this is only executed when the 'databind'method is called)

and to get the values out?
assuming that you are using the update/edit/cancel button stuff provided, inyour 'update' event
something like this:
(on the codebehind page)
private void DgUserPermissions_Update(object source,
System.Web.UI.WebControls.DataGridCommandEventArg s e)

put
CheckBox cb = (CheckBox)e.Item.Cells[2].FindControl ("Disabled"); Boolean Disabled = cb.Checked;

2 means second column in the grid in this case, the "(checkbox)e...." syntaxis casting the control into a checkbox (which it is, seems silly to me)you can then access the value
/*
i have had problems where by the edit/cancel buttons were being added butthe hander was missing
(found inside the unexpanded) "Web Form Designer generated code" sectionso you have to add in something like (again on the code behind page) this.DgUserPermissions.CancelCommand += new
System.Web.UI.WebControls.DataGridCommandEventHan dler (this.DgUserPermissions_Cancel);
this.DgUserPermissions.EditCommand += new
System.Web.UI.WebControls.DataGridCommandEventHan dler (this.DgUserPermissions_Edit);
this.DgUserPermissions.UpdateCommand += new
System.Web.UI.WebControls.DataGridCommandEventHan dler (this.DgUserPermissions_Update);
you can put whatever you like in with template columns drop downs, specialformatting and so on
if you don't like c# then make the effort to learn it
i was a vb6 programmer of 5 years and have made the switch (eventually!)
[there are some in depth articles about the datagrid on the 4guysfromrollawebsite]

"Sebi" <an*******@discussions.microsoft.com> wrote in messagenews:0a****************************@phx.gbl...
Hello all

is it possible to add a checkbox in a DataGrid for
Boolean Data?

Thanks in advance

.

Nov 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.