I have two CheckedListBoxes that look the same (except for the contents). I load one like so:
private void PopulateReportsListBox()
{
checkedListBoxReports.Items.AddRange(
ReportSchedulerConstsAndUtils.Reports.ToArray<object>());
}
public static List<string> Reports = new List<string>
{
"Produce Usage",
"Delivery Performance",
"Fill Rate by Customer / Location",
"Price Compliance"
};
With that one, I can get the value displayed in the CLB's ItemCheck event like so:
private void checkedListBoxReports_ItemCheck(object sender,
ItemCheckEventArgs iceargs)
{
for (int i = 0; i < checkedListBoxReports.Items.Count; ++i)
{
if (i != iceargs.Index) checkedListBoxReports.SetItemChecked(i,
false);
}
String selectedRpt = checkedListBoxReports.SelectedItem.ToString();
DisableParameterGroupBoxes();
EnableParameterGroupBox(selectedRpt);
}
"selectedRpt" holds the value I expect ("Produce Usage" if the first item is selected, etc.).
However, I load the other CLB like this, from a DB:
private void PopulateUnitsListBox()
{
using (SqlConnection con = new
SqlConnection(ReportSchedulerConstsAndUtils.CPSConnStr))
{
using (SqlCommand cmd = new
SqlCommand(ReportSchedulerConstsAndUtils.SelectUnitsQuery, con))
{
cmd.CommandType = CommandType.Text;
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
DataTable dt = new DataTable();
sda.Fill(dt);
((ListBox)checkedListBoxUnits).DataSource = dt;
((ListBox)checkedListBoxUnits).DisplayMember = "Unit";
((ListBox)checkedListBoxUnits).ValueMember = "Unit";
}
}
}
}
...and I cannot access the display value in its ItemCheck event. I have to use the CLB's Text proprty rather than SelectedItem.ToString(). If I use the latter, I get (for all items), "System.Data.DataRowView"
Why? And are there any "gotchas" I should be aware of when using "Text"? Is it reliable/do I need to Trim() it?