2015-07-03 47 views
0

我有一个CheckedListBox在win窗体中获取从下面的代码从数据库中加载。我的问题是,如何获得Checked Value Members的密钥列表?列表价值数据绑定的成员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 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被称为在一个双赢的形式加载事件和包含三列(ID INT,DESCR VARCHAR(50),INT器isChecked)。显示器和value成员设置,然后使用值器isChecked设置在foreach循环

  // 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〔实施例的选中状态,但它返回“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.WebControls,它工作正常。我只需要类似的胜利形式。

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

的“的ToString()”是给错误的结果。尝试值而不是。 – jdweng

+0

值不存在于clbLocations.Items [indexChecked] – bsivel

+0

对象为null。你有没有尝试“IsChecked **”。您的示例数据有两个星号。 – jdweng

回答

0

一些试验和错误后,我想出了一个很好的解决方案。首先加载的复选框列表为前:

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