【问题标题】:C# Checkedlistbox data bindingC# Checkedlistbox 数据绑定
【发布时间】:2018-09-07 09:23:17
【问题描述】:

我在 C# 中有一个 checkedlistBox,我是从 sql-server 填写的。 创建新记录时需要插入checkeditems,并且需要更新某条记录之前选中的item。

首先我试图读取特定记录的选定项目,所以我尝试了以下操作:

我将每个项目的值成员与我从 sql 查询中获得的列表进行比较,如果它匹配我检查项目。 所以我需要使用类似 value 选项的东西。

if(checkedListBox1.Items.IndexOf(i).**Value**

        string sql = @"select cs.id from[dbo].[Channel_availableSpecs] cas inner join[dbo].[Channel_specs] cs on cas.ChennelSpec_Id = cs.id
                where cas.Channel_Id =" + val + "order by cs.id";
        SqlCommand cmd = new SqlCommand(sql, conn);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataTable dta = new DataTable();
        da.Fill(dta);
        foreach (DataRow dr in dta.Rows)
        {
            for (int i = 0; i < checkedListBox1.Items.Count; i++)
            {
                 if(checkedListBox1.Items.IndexOf(i).**Value** == dr.ToString()) checkedListBox1.SetItemCheckState(i, CheckState.Checked);
            }
            conn.Close();

        }
    }

填写Checkedlistbox

 public static void FillCheckedListox(CheckedListBox checkedListBox, string query,string displayMember, string valueMember) {

        using (SqlConnection con = new SqlConnection(ConnectionString))
        {
            using (SqlCommand cmd = new SqlCommand(query, con))
            {
                cmd.CommandType = CommandType.Text;
                using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
                {
                    DataTable dt = new DataTable();
                    sda.Fill(dt);
                    ((ListBox)checkedListBox).DataSource = dt;
                    ((ListBox)checkedListBox).DisplayMember = displayMember;
                    ((ListBox)checkedListBox).ValueMember = valueMember;
                }
            }
       }

【问题讨论】:

    标签: c# sql-server checkedlistbox


    【解决方案1】:

    这就是解决办法

    private void selectSpecs(DataTable table)
            {
                while (checkedListBox1.CheckedIndices.Count > 0)
                {
                    checkedListBox1.SetItemChecked(checkedListBox1.CheckedIndices[0], false);
                }
    
            for (int i = 0; i < checkedListBox1.Items.Count; i++)
            {
    
                DataRow r;
                r = ((DataRowView)this.checkedListBox1.Items[i]).Row;
                string val = (r[this.checkedListBox1.ValueMember]).ToString();
                channelsSpecs.Add(Convert.ToInt32(val));
                r = null;
                for (int j = 0; j < table.Rows.Count; j++)
                    foreach (DataRow dataRow in table.Rows)
                        foreach (var item in dataRow.ItemArray) if (val.ToString() == item.ToString()) checkedListBox1.SetItemChecked(i, true);
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多