Click here to Skip to main content
12,627,002 members (36,115 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C#
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



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


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 8-Nov-12 1:40am
Updated 8-Nov-12 1:46am
v2
Comments
Sastry_kunapuli 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

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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
}
  Permalink  
v4

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.161205.3 | Last Updated 14 Jan 2015
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100