CheckBox in DataGrid

  • Thread starter Thread starter Sebi
  • Start date Start date
S

Sebi

Hello all

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

Thanks in advance
 
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.DataGridCommandEventHandler(this.DgUserPermissions
_Cancel);
this.DgUserPermissions.EditCommand += new
System.Web.UI.WebControls.DataGridCommandEventHandler(this.DgUserPermissions
_Edit);
this.DgUserPermissions.UpdateCommand += new
System.Web.UI.WebControls.DataGridCommandEventHandler(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]
 
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"> said:
<HeaderStyle BackColor="Gray"></HeaderStyle>
<Columns>
<asp:EditCommandColumn ButtonType="PushButton" UpdateText="Update"
CancelText="Cancel"
EditText="Edit"> said:
.....(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.DataGridCommandEventHandler (this.DgUserPermissions
_Cancel);
this.DgUserPermissions.EditCommand += new
System.Web.UI.WebControls.DataGridCommandEventHandler (this.DgUserPermissions
_Edit);
this.DgUserPermissions.UpdateCommand += new
System.Web.UI.WebControls.DataGridCommandEventHandler (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]

Hello all

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

Thanks in advance


.
 
Back
Top