Click here to Skip to main content
15,034,986 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i used panel in the form, inside the panel checkedlistbox will appear dynamically, in the checkedlist box many checkbox will appear., when i click button, what are all the items selected in the checkedlist box should appear in datagridview., i used below coding., but no process happening



C#
string[] total_items = new string[50];
        int items = 0;



C#
private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                foreach (Control p in panel1.Controls)
                {
                    if (p.GetType() == typeof(CheckedListBox))
                    {
                        CheckedListBox getdata = new CheckedListBox();
                       total_items[items] = getdata.SelectedItem.ToString();
                        items++;
                        
                    }
                }


                dataGridView1.Columns.Add("Items", "Choosed Items");


                for (int cnt = 0; cnt < items; cnt++)
                {
                    dataGridView1.Rows.Add(total_items[cnt]);
                }

            }
            catch (SqlException err)
            {
                MessageBox.Show(err.Message);
            }
        }
Posted
Updated 8-Nov-12 0:46am
v2
Comments
S@53K^S 8-Nov-12 9:57am
   
is there any value in the selected item ?and I think you need to iterate through all the checkboxes in the panel not all controls of the panel.and please be clear if you are using ASP.NET or WinForms or WPF.the solution changes for each type

1 solution

Your code won't work like this. total_items should be a list of strings. It should be something like this, play around with it to make things work:

foreach (Control p in panel1.Controls)
{
    if (p.GetType() == typeof(CheckedListBox))
    {
        total_items.Clear();
        CheckedListBox getdata = (CheckedListBox)p;
        foreach (int index in getdata.CheckedIndices)
        {
            total_items.Add(getdata.Items[index].ToString());
        }
    }
}

dataGridView1.Rows.Clear();
dataGridView1.Columns.Add("Items", "Choosed Items");


for (int cnt = 0; cnt < total_items.Count; cnt++)
{
    dataGridView1.Rows.Add(total_items[cnt], true); // or something like that
}
   
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900