Bind checkbox in Datagrid

M

MS Newsgroups

Hi,

I have managed to create a Datagrid with a template column with checkboxes
following Q306227. The dataset i have bound to the datagrid contains 1
column that allows NULL value and I would like to bind the checkbox to be
checked if this value is anything else than DBNULL. I have used to designer
to create the grid and dataset etc, and from what I understand I need to
edit the databinding property of the checkbox in the template to evaluate
the Checked property, but I have not managed to figure out how to create an
expression that evaluates if a coulmn is empty or not.

Any hint would be appreciated

Regards

Niclas
 
K

Ken Cox [Microsoft MVP]

Hi Niclas,

If your checkbox is built inside the .aspx file, you can check the checkbox
for all non-null values like this:

<asp:DataGrid id="DataGrid1" runat="server">
<Columns>
<asp:TemplateColumn HeaderText="Currency Value">
<ItemTemplate>
<asp:CheckBox id="CheckBox1" runat="server" Checked='<%# not
isDBnull(DataBinder.Eval(Container, "DataItem.CurrencyValue"))
%>'></asp:CheckBox>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>

The same thing in the code behind would be like this:

oCheckBox.Checked = Not IsDBNull(container.DataItem("CurrencyValue"))

Is that what you meant?

Ken
MVP [ASP.NET]
 
Joined
Feb 10, 2010
Messages
12
Reaction score
0
Checkbox Column In Datagrid

When we add a customized checkbox column to a datagrid in .net (windows application) , the default property allows to check or uncheck the column using a double click. On the first click it selects the column and on the second click the column is either checked or unchecked.

To change this default property, we need to handle the click event on grid and modify the selected cell value. Here is the sample code to achieve this.

[C#.NET VS 2003 , Code to add checkbox column to grid using table style property]

dgItemDetails.TableStyles.Clear(); // clears the tablestyle (dgItemDetails is the name of grid)
DataGridTableStyle dgt = new DataGridTableStyle();
dgt.MappingName = "ItemDetails";

DataGridBoolColumn dgbCol = new DataGridBoolColumn(); // creates the checkbox column
dgbCol.MappingName = "Select";
dgbCol.HeaderText = "";
dgbCol.Width = 40;
dgbCol.AllowNull = false;
dgbCol.Alignment = HorizontalAlignment.Left;
dgt.GridColumnStyles.Add(dgbCol);

dgItemDetails.TableStyles.Add(dgt); // add new tablestyle to grid

[C#.NET Code Ends]

Hope u this answer is useful.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top