C# .NET - CheckedListBox - Asked By Pavan M on 30-Jan-12 01:19 AM

Sir i started a project. For that i need checkedlistbox, and i have to save multiple items into database, how can i do that pls help me for that.
Jitendra Faye replied to Pavan M on 30-Jan-12 01:21 AM

please check the code snippet below:

//Binding the checkedlist box

this.chkLB.DataSource = ds.Tables[0];
this.chkLB.DisplayMember = "Name";
this.chkLB.ValueMember = "Code"; //based on your requirement.....you can set the value member and displaymember

OR

foreach(DataRow drItem in ds.Tables[0].Rows)
{
//this.chkLB.Items.Add(drItem["Name"]);
this.chkLB.Items.Add(drItem);
}

 

Let me know.

Venkat K replied to Pavan M on 30-Jan-12 01:26 AM
Loop throuch the checkboxes in the checkboxlist and add the result into a string builder and save into database:

protected void CheckBox11_CheckedChanged(object sender, EventArgs e)
{
    // C# Multiple Selection
    foreach (Control control in Panel1.Controls)
    {
      if (control is CheckBox)
      {
      (CheckBox) chkbox = control;
      StringBuilder sb = new StringBuilder();
      sb.Append(chkbox.value);
      }
    }    
  SaveValuetoDB(sb);
}

Write a function SaveValuetoDB which you can use SQLCommand to save the sb to db.

Hope this helps!

Chintan Vaghela replied to Pavan M on 30-Jan-12 01:29 AM

Hello,

  for (int i = 0; i < cbl.Items.Count; i++)

              {

                if (cbl.Items[i].Selected)

                {

                  // Insert Item into Database

                  string itemName = cbl.Items[i].Text;

                  string itemID = cbl.Items[i].Value;

                }

              }

 

Hope this is helpful !

Thanks

 

 

 

 

 

Reena Jain replied to Pavan M on 30-Jan-12 01:35 AM
hi,

try this

    foreach(DataRowView view in myCheckedList.CheckedItems)
    {
        string a=(view[myCheckedList.ValueMember].ToString());
    } 
hope this will help you
Riley K replied to Pavan M on 30-Jan-12 01:42 AM


Hi ,

Just iterate through the items in the checkedlistbox and first check if item is checked then insert


for (int i = 0; i < chkbx.Items.Count; i++)
{
if (chkbx.Items[i].Selected)
  {
  cmd.Parameters.AddWithValue("@CategoryID", chkbx.Items[i].Value);
  cmd.ExecuteNonQuery();
  cmd.Parameters.Clear();
  }
}

Regards
kalpana aparnathi replied to Pavan M on 30-Jan-12 01:04 PM
hi,

Try this code

for (int i = 0; i < CheckBoxList1.Items.Count - 1; i++)
{
String str = "";

if (CheckBoxList1.Items[i].Selected)
{

str = CheckBoxList1.Items[i].Text;
// code for saving data with connection

 }
}