C# 列出数据绑定CheckedListBox的值成员
我有一个win格式的CheckedListBox,它从数据库中加载,代码如下。我的问题是如何获得校验值成员的键列表 示例数据集如下所示C# 列出数据绑定CheckedListBox的值成员,c#,C#,我有一个win格式的CheckedListBox,它从数据库中加载,代码如下。我的问题是如何获得校验值成员的键列表 示例数据集如下所示 ID Descr IsChecked 4 East 1 1 Loc Code 1 2 North 1 3 South 0 5 West 0 所以我的目标是用上面的示例数据将变量StrLocKeys填充为“4,1,2” public DataTable LoadLocati
ID Descr IsChecked
4 East 1
1 Loc Code 1
2 North 1
3 South 0
5 West 0
所以我的目标是用上面的示例数据将变量StrLocKeys填充为“4,1,2”
public DataTable LoadLocationCheckedListBox(int coid, int userid)
{
string strSql = "plm_admin_location_checkedlistbox_by_user";
SqlCommand Cmd = new SqlCommand(strSql, cnxn);
Cmd.CommandType = CommandType.StoredProcedure;
SqlParameter p1 = new SqlParameter("coid", SqlDbType.Int);
p1.Value = coid;
Cmd.Parameters.Add(p1);
SqlParameter p2 = new SqlParameter("userid", SqlDbType.Int);
p2.Value = userid;
Cmd.Parameters.Add(p2);
SqlDataAdapter Ada = new SqlDataAdapter(Cmd);
DataTable dt = new DataTable();
Ada.Fill(dt);
return dt;
}
LoadLocationCheckedListBox在win form加载事件中被调用,它包含三列(ID int、Descr varchar(50)、IsChecked int)。设置Display和Value成员,然后使用IsChecked值在foreach循环中设置checked状态
// load the locations and then idenfify the checked items
DataTable src_loc = da.LoadLocationCheckedListBox(coid,userid);
clbLocations.DataSource = src_loc;
clbLocations.DisplayMember = "Descr";
clbLocations.ValueMember = "ID";
if (mode == "Edit")
{
foreach (DataRow row in src_loc.Rows)
{
if( row["IsChecked"].ToString() == "1")
{
// check the item in the checkbox list
int i = Convert.ToInt32(row["ID"].ToString());
clbLocations.SetItemChecked(i, true);
}
}
}
我尝试了下面的MSDN exmaple,但它返回的是“System.Data.DataRowView”,而不是值成员
foreach (int indexChecked in clbLocations.CheckedIndices)
{
// The indexChecked variable contains the index of the item.
MessageBox.Show("Index#: " + indexChecked.ToString() + ", is checked. Checked state is:" +
clbLocations.GetItemCheckState(indexChecked).ToString() + ".");
MessageBox.Show(clbLocations.Items[indexChecked].ToString());
//MessageBox.Show(clbLocations)
}
在ASP.Net中,我将以下内容与System.Web.UI.WebControl一起使用,效果良好。我只是需要一些类似的win表单
string GetListBoxSelections(CheckBoxList cb)
{
string rv = string.Empty;
// Iterate through the Items collection of the CheckBoxList
// control and build a string of the selected items.
string c = cbContactTypes.Items.Count.ToString();
for (int i = 0; i < cb.Items.Count; i++)
{
if (cb.Items[i].Selected)
{
// here i need to build a string...
// for example if check box list items 2,5 & 8 are selected then
// i need to buld a string equal to "2,5,8"
// this enables will enable me to bulk insert or update contact types
// and interfaces per contact with the Support.Contact_Save stoed procedure
string Separator = rv.Length > 0 ? "," : "";
rv += Separator + cb.Items[i].Value.ToString();
}
}
return rv;
}
字符串GetListBoxSelections(复选框列表cb)
{
string rv=string.Empty;
//遍历复选框列表的Items集合
//控件并生成选定项的字符串。
字符串c=cbContactTypes.Items.Count.ToString();
对于(int i=0;i0?,“:”;
rv+=分隔符+cb.Items[i].Value.ToString();
}
}
返回rv;
}
此代码将起作用
DataTable dt=newdatatable();
添加(“ID”,typeof(int));
添加(“描述”,类型(字符串));
添加(“已检查”,类型为(int));
添加(新对象[]{4,“东”,1});
Add(新对象[]{1,“Loc Code”,1});
Add(新对象[]{2,“北”,1});
Add(新对象[]{3,“南”,0});
Add(新对象[]{5,“West”,0});
列出StrLocKeys=dt.AsEnumerable()。其中(x=>x.Field(“IsChecked”)==1)。选择(y=>y.Field(“ID”)。ToList();代码>经过反复试验,我想出了一个很好的解决方案。如前所述,首先加载复选框列表:
// load the locations and then idenfify the checked items
DataTable src_loc = da.LoadLocationCheckedListBox(coid,userid);
clbLocations.DataSource = src_loc;
clbLocations.DisplayMember = "Descr";
clbLocations.ValueMember = "ID";
SetLocationKeys(clbLocations,src_loc);
调用SetLocationKeys并传递控件和数据表将处理选中相应复选框的操作:
private void SetLocationKeys(CheckedListBox cb, DataTable dt)
{
int index = 0;
foreach (DataRow r in dt.Rows)
{
if (r.ItemArray[2].ToString() == "1")
{
cb.SetItemChecked(index, true);
}
index++;
}
}
“ToString()”给出了错误的结果。请改为尝试值。clbLocations的值不存在。Items[indexChecked]对象为null。你试过“IsChecked**”吗。示例数据有两个星号。星号是一个类型错误。我试图加粗列标题。