我有一个名为CLB_CONTROLLER的checkedListBox,它被加载到页面以显示名称而不是ID:
此信息存储在dbo.CHIPSET
public void loadCategory()
{
CLB_CONTROLLER.DataSource = catg.getCategories();
CLB_CONTROLLER.DisplayMember = "CHIPSET";
CLB_CONTROLLER.ValueMember = "CHIPSET_ID";
}
然后我将数据作为字符串插入数据库。此信息存储在dbo.MYDB
//LOOP THROUGH CONTROLLER CHECKED ITEMS TO INSERT ALL SELECTED
string chip = "";
for (int i = 0; i < CLB_CONTROLLER.CheckedItems.Count; i++)
{
if (chip == "")
{
chip = CLB_CONTROLLER.GetItemText(CLB_CONTROLLER.CheckedItems[i]);
}
else
{
chip += ", " + CLB_CONTROLLER.GetItemText(CLB_CONTROLLER.CheckedItems[i]);
}
}
到目前为止一切都很好......现在我要创建更新功能...当用户从DataGrid
中选择单元格时,我想在页面上自动填充所有textboxes
,CheckBoxes
等。所以我想弄清楚如何使用上面提到的CLB_CONTROLLER
来做到这一点。
更新正在查看dbo.MYDB
和第2列CHIPSET
,它现在是一个字符串(插入上面的代码)。我在下面有这个代码,但当我点击DataGrid
时,CLB_CONTROLLER
//CHECK IF CONTROLLER CONTAINES CONTENT, THEN SELLECT APPROPRIATE CHECK BOX
string con = DGV_AOC.CurrentRow.Cells[2].Value.ToString();
string[] convalues = con.Split(',');
for (int i = 0; i < convalues.Length; i++)
{
convalues[i] = convalues[i].Trim();
}
for (int i = 0; i < CLB_CONTROLLER.Items.Count; i++)
{
CLB_CONTROLLER.SetItemChecked(i, false);//First uncheck the old value!
for (int x = 0; x < convalues.Length; x++)
{
if (CLB_CONTROLLER.Items[i].ToString() == convalues[x].ToString())
{
//Check only if they match!
CLB_CONTROLLER.SetItemChecked(i, true);
}
}
}
非常感谢任何帮助。
答案 0 :(得分:1)
Let's say you have filled the CheckedListBox
with a,b,c,d,e,f
for example as item texts.
Then you can set checked items based on a comma separated list this way:
var itemsToSelect = "a,c,d";
var items = itemsToSelect.Split(',');
for (var i = 0; i < checkedListBox1.Items.Count; i++)
{
var b = items.Contains(checkedListBox1.GetItemText(checkedListBox1.Items[i]));
checkedListBox1.SetItemChecked(i, b);
}
Also you can get checked items in a comma-separated string this way:
var selectedItems = string.Join(",",
checkedListBox1.CheckedItems.Cast<object>()
.Select(x => checkedListBox1.GetItemText(x)));