现在我有数据表..dt..里面有ID..和Name
如何绑定..checkedListBox1????
还有,就是选中了如何.得到选中名字的ID???
在线..= .............

解决方案 »

  1.   

    绑定: checkedListBox1.DisplayMember = "Name";
    checkedListBox1.ValueMember = "ID";
    checkedListBox1.DataSource = dt;
    获取选中ID:
    private void checkedListBox1_SelectedIndexChanged(object sender, System.EventArgs e)
    {
    MessageBox.Show(checkedListBox1.SelectedValue.ToString());
    }
      

  2.   

    checkedListBox1.DataSource=ds.Tables[0];
    checkedListBox1.ValueMember="intSectionID";
    checkedListBox1.DisplayMember="txtShortDesc".ToString(); for (int i = 0;i<chlSections.CheckedItems.Count;i++)
    {
         aa+=chlSections.CheckedItems[i].ToStrin()+";";
    }
      

  3.   

    绑定:
    checkedListBox1.DataSource=ds.Tables[0];
    checkedListBox1.DataValueField = "id";
    checkedListBox1.DataTextField = "Name";
    checkedListBox1.DataBind();
    得到选中名字的ID如楼上所说..
      

  4.   

    刚才写的有点错误,更新::checkedListBox1.DataSource=ds.Tables[0];
    checkedListBox1.ValueMember="intSectionID";
    checkedListBox1.DisplayMember="txtShortDesc".ToString();for (int i = 0;i<checkedListBox1.CheckedItems.Count;i++)
    {
          DataRowView dv = ((System.Data.DataRowView)checkedListBox1.CheckedItems[i]);
          aa+=dv["字段"].ToString()+";"
    }
      

  5.   

    CheckBoxList1.DataSource = dt;
    CheckBoxList1.DataTextField = "name";
    CheckBoxList1.DataValueField = "id";
    CheckBoxList1.DataBind();
      

  6.   

    没有说错,的确是在WinForm下,也许你会说IDE的提示里面没有,不过我测试过,的确可以这样使用,而且我贴出的代码就是我测试的代码。CheckedListBox 是从ListControl派生而来的。DisplayMember ValueMember 这两个成员自然也就继承下来了,IDE中的智能提示没有,我只能认为是一个bug,如果你看到《Windows Form程序设计》一书,里面也提到了IDE存在的另一个bug,你就不会奇怪了。
      

  7.   

    checkedListBox1 没有这个DataSource属性啊。。
      

  8.   

    checkedListBox1.DataSource=ds.Tables[0];
    checkedListBox1.DataValueField = "id";
    checkedListBox1.DataTextField = "Name";
    checkedListBox1.DataBind();
    for (int i = 0;i<checkedListBox1.CheckedItems.Count;i++)
    {
         aa+=checkedListBox1.CheckedItems[i].ToStrin()+";";
    }
      

  9.   

    第一步:
    public Form1()
    {
      …………… 
      this.checkedListBox1.SetItemCheckState (0, System.Windows.Forms.CheckState.Indeterminate);//设置第一个item(索引是0)的状态为“不确定”
    }第二步:
    private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
    {
       if (e.CurrentValue == CheckState.Indeterminate)//如果对像的当前状态为“不确定”,则设置对象的新状态为“不确定”
       {
           e.NewValue = CheckState.Indeterminate;
       }
    ……………
    }第三步:
    在你要对items进行操作时,用个if语句,选择item的状态为非“不确定”的进行操作,这样,对状态为“不确定”的item的操作就被屏蔽掉了。回答完毕。
    一个完整的示例程序:using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;namespace WindowsApplication1
    {
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;
    using System.IO ; public class Form1 : System.Windows.Forms.Form
    {
    private System.Windows.Forms.CheckedListBox checkedListBox1;
    private System.Windows.Forms.TextBox textBox1;
    private System.Windows.Forms.Button button1;
    private System.Windows.Forms.Button button2;
    private System.Windows.Forms.ListBox listBox1;
    private System.Windows.Forms.Button button3;
    private System.Windows.Forms.Button button4;
    private System.ComponentModel.Container components;
          
    public Form1()
    {
    InitializeComponent(); // Sets up the initial objects in the CheckedListBox.
    string[] myFruit = {"Apples", "Oranges","Tomato"};
    checkedListBox1.Items.AddRange(myFruit); // Changes the selection mode from double-click to single click.
    checkedListBox1.CheckOnClick = true;
    } protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if (components != null) 
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    } private void InitializeComponent()
    {
    this.textBox1 = new System.Windows.Forms.TextBox();
    this.checkedListBox1 = new System.Windows.Forms.CheckedListBox();
    this.listBox1 = new System.Windows.Forms.ListBox();
    this.button1 = new System.Windows.Forms.Button();
    this.button2 = new System.Windows.Forms.Button();
    this.button3 = new System.Windows.Forms.Button();
    this.button4 = new System.Windows.Forms.Button();
    this.SuspendLayout();
    // 
    // textBox1
    // 
    this.textBox1.Location = new System.Drawing.Point(173, 69);
    this.textBox1.Name = "textBox1";
    this.textBox1.Size = new System.Drawing.Size(153, 21);
    this.textBox1.TabIndex = 1;
    this.textBox1.Text = "";
    this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
    // 
    // checkedListBox1
    // 
    this.checkedListBox1.Location = new System.Drawing.Point(19, 69);
    this.checkedListBox1.Name = "checkedListBox1";
    this.checkedListBox1.Size = new System.Drawing.Size(144, 180);
    this.checkedListBox1.TabIndex = 0;
    this.checkedListBox1.ThreeDCheckBoxes = true;
    this.checkedListBox1.ItemCheck += new System.Windows.Forms.ItemCheckEventHandler(this.checkedListBox1_ItemCheck);
    // 
    // listBox1
    // 
    this.listBox1.ItemHeight = 12;
    this.listBox1.Location = new System.Drawing.Point(490, 69);
    this.listBox1.Name = "listBox1";
    this.listBox1.Size = new System.Drawing.Size(153, 196);
    this.listBox1.TabIndex = 3;
    // 
    // button1
    // 
    this.button1.Enabled = false;
    this.button1.Location = new System.Drawing.Point(173, 112);
    this.button1.Name = "button1";
    this.button1.Size = new System.Drawing.Size(125, 34);
    this.button1.TabIndex = 2;
    this.button1.Text = "Add Fruit";
    this.button1.Click += new System.EventHandler(this.button1_Click);
    // 
    // button2
    // 
    this.button2.Enabled = false;
    this.button2.Location = new System.Drawing.Point(346, 69);
    this.button2.Name = "button2";
    this.button2.Size = new System.Drawing.Size(124, 34);
    this.button2.TabIndex = 2;
    this.button2.Text = "Show Order";
    this.button2.Click += new System.EventHandler(this.button2_Click);
    // 
    // button3
    // 
    this.button3.Enabled = false;
    this.button3.Location = new System.Drawing.Point(346, 112);
    this.button3.Name = "button3";
    this.button3.Size = new System.Drawing.Size(124, 34);
    this.button3.TabIndex = 2;
    this.button3.Text = "Save Order";
    this.button3.Click += new System.EventHandler(this.button3_Click);
    // 
    // button4
    // 
    this.button4.Location = new System.Drawing.Point(200, 184);
    this.button4.Name = "button4";
    this.button4.TabIndex = 4;
    this.button4.Text = "button4";
    this.button4.Click += new System.EventHandler(this.button4_Click);
    // 
    // Form1
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.ClientSize = new System.Drawing.Size(675, 294);
    this.Controls.Add(this.button4);
    this.Controls.Add(this.listBox1);
    this.Controls.Add(this.button3);
    this.Controls.Add(this.button2);
    this.Controls.Add(this.button1);
    this.Controls.Add(this.textBox1);
    this.Controls.Add(this.checkedListBox1);
    this.Name = "Form1";
    this.Text = "Fruit Order";
    this.ResumeLayout(false); } [STAThread]
    public static void Main(string[] args) 
    {
    Application.Run(new Form1());
    } // Adds the string if the text box has data in it.
    private void button1_Click(object sender, System.EventArgs e)
    {
    if(textBox1.Text != "")
    {
    if(checkedListBox1.CheckedItems.Contains(textBox1.Text)== false)
    checkedListBox1.Items.Add(textBox1.Text,CheckState.Checked);
    textBox1.Text = "";
    } }
    // Activates or deactivates the Add button.
    private void textBox1_TextChanged(object sender, System.EventArgs e)
    {
    if (textBox1.Text == "")
    {
    button1.Enabled = false;
    }
    else
    {
    button1.Enabled = true;
    }
                
    } // Moves the checked items from the CheckedListBox to the listBox.
    private void button2_Click(object sender, System.EventArgs e)
    {
    listBox1.Items.Clear();
    button3.Enabled=false;
    for (int i=0; i< checkedListBox1.CheckedItems.Count;i++)
    {
    if (this.checkedListBox1.GetItemCheckState(i) != System.Windows.Forms.CheckState.Indeterminate)
    {
    listBox1.Items.Add(checkedListBox1.CheckedItems[i]); }
    }
    if (listBox1.Items.Count>0)
    button3.Enabled=true;
             
    }
    // Activates the move button if there are checked items.
    private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
    {
    if (e.CurrentValue == CheckState.Indeterminate)
    {
    e.NewValue = CheckState.Indeterminate;
    }
    if(e.NewValue==CheckState.Unchecked)
    {
    if(checkedListBox1.CheckedItems.Count==1)
    {
    button2.Enabled = false;
    }
    }
    else
    {
    button2.Enabled = true;
    }
    } // Saves the items to a file.
    private void button3_Click(object sender, System.EventArgs e)
    {   
    // Insert code to save a file.
    listBox1.Items.Clear();
    IEnumerator myEnumerator;
    myEnumerator = checkedListBox1.CheckedIndices.GetEnumerator();
    int y;
    while (myEnumerator.MoveNext() != false)
    {
    y =(int) myEnumerator.Current;
    checkedListBox1.SetItemChecked(y, false);
    }
    button3.Enabled = false ;
    } private void button4_Click(object sender, System.EventArgs e)
    {
    this.checkedListBox1.SetItemCheckState (0, System.Windows.Forms.CheckState.Indeterminate);
    }
    }
    }这里是加了个button4的按钮。
      

  10.   

    绑定数据的用for 循环取值 然后 用Items.Add();
    实现了 不过不太厚道 只是刚刚实现功能而已