简体   繁体   中英

ListBox.DataBinding Method Question

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;
}

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM