Binding CheckBoxList to DataSet Column

D

Dave Hall

I have a DataSet with two columns that I want to bind to a CheckBoxList. One column of the DataSet is a datetime field called HourOfDay. I want just the time portion of this column to become the text. The second column in the DataSet is a Bit column called "Checked". I want it to control if the checkbox for a particular hour of the day is checked or not. The following code displays the time column properly, but none of the checkboxes are checked. How should I bind the checkboxes to the "Checked" column in the DataSet?

Thanks,
Dave

Here's the code I'm using for binding the DataSet to the CheckBoxList:

CheckBoxList1.DataSource = dataSet;
CheckBoxList1.DataTextField = "HourOfDay";

CheckBoxList1.DataTextFormatString = "{0:h:mm tt}";

CheckBoxList1.DataValueField = "Checked";

CheckBoxList1.DataBind();



Here's the resulting display:


8:00 AM
9:00 AM
10:00 AM
11:00 AM
12:00 PM
1:00 PM
2:00 PM
3:00 PM
4:00 PM
5:00 PM


Here's the dataset contents:

Checked HourOfDay
------- -------------------
1 2004-08-09 08:00:00
0 2004-08-09 09:00:00
1 2004-08-09 10:00:00
1 2004-08-09 11:00:00
0 2004-08-09 12:00:00
1 2004-08-09 13:00:00
1 2004-08-09 14:00:00
0 2004-08-09 15:00:00
1 2004-08-09 16:00:00
1 2004-08-09 17:00:00
 
J

Jos

Dave said:
I have a DataSet with two columns that I want to bind to a
CheckBoxList. One column of the DataSet is a datetime field called
HourOfDay. I want just the time portion of this column to become the
text. The second column in the DataSet is a Bit column called
"Checked". I want it to control if the checkbox for a particular hour
of the day is checked or not. The following code displays the time
column properly, but none of the checkboxes are checked. How should
I bind the checkboxes to the "Checked" column in the DataSet?

The DataValueField of a checkboxlist has nothing to do with the checkmarks
on the boxes.
It behaves in the same way as it does for a ListBox or a DropDownList.

To check the boxes, you'll need to loop through the rows of the dataset. For
each row, you should set CheckBoxList1.Items(i).Checked according to the
value in the database.
 
D

Dave Hall

I wsa afraid I'd have to do the work myself instead of just binding the
dataSet to the control. Here's what I ended up doing. It works OK so far.

CheckBoxList1.DataSource = dataSet;
CheckBoxList1.DataTextField = "HourOfDay";
CheckBoxList1.DataTextFormatString = "{0:h:mm tt}";
CheckBoxList1.DataBind();
DataRow[] allRows = dataSet.Tables[0].Select(null, null,
DataViewRowState.CurrentRows);
for(int i=0; i< CheckBoxList1.Items.Count; i++)
{
DataRow currentRow = (DataRow)allRows.GetValue(i);
CheckBoxList1.Items.Selected = (bool)currentRow["Checked"];
}

Thanks,
Dave
 

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

Members online

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top