views:

75

answers:

2

Question about the ListBox.DataBinding method. I'm loading a listbox with a DataRows array object and I want to check for each DataRow element a column value if its true/false. If the column value is true, then modify the style for the current listBox.ListItem object. Below is some sample code.:

System.Data.DataRow[] rows = Data.SchoolDetails.Select(filter);
lstBox.DataBinding += new EventHandler(lstBox_DataBinding);
lstBox.DataSource = rows;
lstBox.DataTExtField = "Value";
lstBox.DataValueField = "ValueCode";
lstBox.DataBind();

static void lstBox_DataBinding(object sender, EventArgs e)
{
  ListBox l = (ListBox) sender;
}
+1  A: 

You can't really do that with a ListBox. Maybe you should use ListView, which supports an ItemDataBinding event for each item.

John Saunders
A: 

The best answer is probably the one you included in the comment above.

As an alternative I can just loop through the DataRow array and do it that way, and set the style by doing this: lstBox.Items.Add(new ListItem("").Attributes.CssStyle.Add(HtmlTextWriterStyle.FontWeight, "Bold"));. Thanks for hte help. – Brandon Michael Hunter

That is exactly how I'd do it.

Stephen M. Redd