Click here to Skip to main content
15,892,839 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i used below coding to store data from listitems to array.From array to datagridview.
when i click button data added successfully into datagridview from array., but if i click again the same data appear two times into datagridview., [two times same data]. how to avoid this

C#
private void button1_Click(object sender, EventArgs e)
      {
          try
          {
              foreach (ListItem li2 in ch11.CheckedItems)
              {
                  total_items[items] = li2.Text;
                  items++;
              }

              foreach (ListItem li2 in ch1.CheckedItems)
              {
                  total_items[items] = li2.Text;
                  items++;
              }

              foreach (ListItem li2 in ch2.CheckedItems)
              {
                  total_items[items] = li2.Text;
                  items++;
              }

              foreach (ListItem li2 in ch3.CheckedItems)
              {
                  total_items[items] = li2.Text;
                  items++;
              }
              foreach (ListItem li2 in ch4.CheckedItems)
              {
                  total_items[items] = li2.Text;
                  items++;
              }
              foreach (ListItem li2 in ch5.CheckedItems)
              {
                  total_items[items] = li2.Text;
                  items++;
              }

              foreach (ListItem li2 in ch8.CheckedItems)
              {
                  total_items[items] = li2.Text;
                  items++;
              }

              foreach (ListItem li2 in ch6.CheckedItems)
              {
                  total_items[items] = li2.Text;
                  items++;
              }

              foreach (ListItem li2 in ch7.CheckedItems)
              {
                  total_items[items] = li2.Text;
                  items++;
              }
              foreach (ListItem li2 in ch9.CheckedItems)
              {
                  total_items[items] = li2.Text;
                  items++;
              }
              foreach (ListItem li2 in ch10.CheckedItems)
              {
                  total_items[items] = li2.Text;
                  items++;
              }

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

              try
              {
                  for (int j = 0; j < items; j++)
                  {
                      dataGridView1.Rows.Add(total_items[j].ToString());
                  }
              }
              catch (Exception err)
              {
                  MessageBox.Show(err.Message);
              }

           }

          catch (SqlException err)
          {
              MessageBox.Show(err.Message);
          }
      }
Posted

1 solution

myGrid.Children.Clear() will remove all child controls nested in the grid. myGrid.RowDefinitions.Clear() will remove all row definitions. myGrid.ColumnDefinitions.Clear() will remove all column definitions.
 
Share this answer
 
Comments
Umapathi K 15-Nov-12 3:04am    
its windows application., unable to get extension of .children and .rowdefinitions and.columndefinitions

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