这段代码中发生的情况是,当列表视图中的项目数为 3 时,drv 变量为空。谁能告诉我我做错了什么吗?
private void btn_save_Click(object sender, RoutedEventArgs e)
{
DataRowView drv;
bool valueToCheck;
List<Common.Rule> lst = new List<Common.Rule>();
Common.Rule ru = new Common.Rule();
lst = rr.GetAllRules().ToList();//getting all rules from database
for (int i = 0; i < ltv_rules.Items.Count; i++)
{
drv = ltv_rules.Items.GetItemAt(i) as DataRowView;
valueToCheck = Convert.ToBoolean(drv["IsSet"]);// to get the value of the combobox isSet from the list view
ru = lst[i];//getting the value of the isSet before it was binded to the list view.
if (ru.IsSet != valueToCheck)//comparing the original value (before it was binded) to the value that i get from the listview
{
ru.IsSet = valueToCheck;
bool updated = rr.UpdateRule(ru);
}
}
}