List value数据绑定CheckedListBox的成员

时间:2015-07-03 20:27:48

标签: c#

我有一个win形式的CheckedListBox,它使用下面的代码从数据库加载。我的问题是如何获得Checked Value Members的密钥列表?

示例数据集如下所示......

/Account/InvalidRole

所以我的目标是使用上面的示例数据用“4,1,2”填充变量StrLocKeys。

ID  Descr      IsChecked
4   East        1
1   Loc Code    1
2   North       1
3   South       0
5   West        0

LoadLocationCheckedListBox在win form load事件中调用,包含三列(ID int,Descr varchar(50),IsChecked int)。设置Display和Value Member,然后使用IsChecked值在foreach循环中设置检查状态

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

我已尝试过下面的MSDN示例,但它返回“System.Data.DataRowView”而不是Value Member。

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

我在ASP.Net中使用以下System.Web.UI.WebControls,它工作正常。我只需要类似的胜利形式。

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

2 个答案:

答案 0 :(得分:0)

此代码可以使用



            DataTable dt = new DataTable();
            dt.Columns.Add("ID", typeof(int));
            dt.Columns.Add("Descr", typeof(string));
            dt.Columns.Add("IsChecked", typeof(int));

            dt.Rows.Add(new object[] {4,"East", 1});
            dt.Rows.Add(new object[] {1,"Loc Code", 1});
            dt.Rows.Add(new object[] {2,"North", 1});
            dt.Rows.Add(new object[] {3,"South", 0});
            dt.Rows.Add(new object[] {5,"West", 0});

            List<int> StrLocKeys = dt.AsEnumerable().Where(x => x.Field<int>("IsChecked") == 1).Select(y => y.Field<int>("ID")).ToList();​
&#13;
&#13;
&#13;

答案 1 :(得分:0)

经过一些反复试验后,我想出了一个很好的解决方案。首先像以前一样加载复选框列表:

<div class="align-content">
  <div class="box"></div>
  <div class="box"></div>
  <div class="box"></div>
  <div class="box"></div>
  <div class="box"></div>
  <div class="box"></div>
</div>

调用SetLocationKeys并传递控件和数据表处理检查相应的复选框:

                // 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);