简体   繁体   English

C#Checkedlistbox数据绑定

[英]C# Checkedlistbox data binding

I have a checkedlistBox in C# that I am filling from sql-server. 我在C#中有一个checkedlistBox ,是从sql-server填充的。 I need to insert the checkeditems when creating a new record and I need to update the previous selected items of a certain record. 创建新记录时,我需要插入检查项,并且需要更新某个记录的先前选择的项目。

First I am trying to read the selected item of a specific record so i tried the following: 首先,我尝试读取特定记录的选定项目,因此我尝试了以下操作:

I compare the value member of every item with the list I am getting from the sql query if it matches I check the item. 我将每个项目的值成员与我从sql查询中获得的列表进行比较(如果它与我检查的项目匹配)。 So I need to use something like the value option. 所以我需要使用类似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();

        }
    }

Fill Checkedlistbox 填写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;
                }
            }
       }

This is the solution 这是解决方案

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM