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

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 14-Nov-12 20:52pm

1 solution

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

Solution 1

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.
  Permalink  
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160621.1 | Last Updated 15 Nov 2012
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