Click here to Skip to main content
15,891,424 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how can i copy selected column data and row from one datagridview to another in c# using checkedlistbox.

there are have two datagridview, they are; datagridview1 and datagridview2.
then display sql data in datagridview1. and add the "select" column with checkboxes into datagridview1.
thereafter add "copy" button and checkedlistbox for select columns that we need to copy/pass data to datagridview2.

chekAll = checkedlistbox (if (chekAll.Checked == true))
when all items selected in checkedlistbox. all selected data copy from datagridview1(dgOne) to datagridview2(dgTwo). this code is ok....

but the problem is in second code (else if (chekAll.Checked == false))*****
problem is when select the rows and columns using checkedlistbox1 from datagriview1(dgOne) and click the "Copy" button The selected data in the columns are recorded row by row in one column.

"""when I select some of the items in checkedlistbox(chekAll) how can I copy or pass selected rows and relevent columns values from dgOne to dgTwo?????? """

this is the error message when running the second code
"input array is longer than the number of columns in this table"

What I have tried:

C++

if (chekAll.Checked == true)
                {
                    foreach (DataGridViewRow item in dgOne.Rows)
                    {
                        if ((bool)item.Cells[0].Value == true)
                        {

                            DataTable dt = new DataTable();
                            dt.Columns.Add("Serial No:");
                            dt.Columns.Add("Register No:");
                            dt.Columns.Add("Acedemic Year");
                            dt.Columns.Add("Full Name");
                            dt.Columns.Add("Name with Initial");
                            dt.Columns.Add("Course");
                            dt.Columns.Add("Address");
                            dt.Columns.Add("Telephpne No:");
                            dt.Columns.Add("Province");
                            dt.Columns.Add("District");
                            dt.Columns.Add("AGA Division");
                            dt.Columns.Add("Base of Recruitment");

                            foreach (DataGridViewRow row in dgOne.Rows)
                            {
                                bool isSelected = Convert.ToBoolean(row.Cells["Column0"].Value);
                                if (isSelected)
                                {
                                    dt.Rows.Add(row.Cells[1].Value, row.Cells[2].Value, row.Cells[3].Value, row.Cells[4].Value, row.Cells[5].Value, row.Cells[6].Value, row.Cells[7].Value, row.Cells[8].Value, row.Cells[9].Value, row.Cells[10].Value, row.Cells[11].Value, row.Cells[12].Value);
                                }
                                dgTwo.DataSource = dt;
                            }


                        }
}


else if (chekAll.Checked == false)
                {

                    foreach (DataGridViewRow item in dgOne.Rows)
                    {
                        if ((bool)item.Cells[0].Value == true)
                        {

                            DataTable dt = new DataTable();
                            dgTwo.DataSource = null;
                            foreach (string s in checkedListBox1.CheckedItems)
                            {
                                dt.Columns.Add(s);

                            }

                            foreach (DataGridViewRow row in dgOne.Rows)
                            {
                                bool isSelected = Convert.ToBoolean(row.Cells["Column0"].Value);
                                if (isSelected)
                                {
                                    dt.Rows.Add(row.Cells[1].Value, row.Cells[2].Value, row.Cells[3].Value, row.Cells[4].Value, row.Cells[5].Value, row.Cells[6].Value, row.Cells[7].Value, row.Cells[8].Value, row.Cells[9].Value, row.Cells[10].Value, row.Cells[11].Value, row.Cells[12].Value);
                                }
                                dgTwo.DataSource = dt;
                            }


                        }
                    }
}
Posted
Updated 22-Jul-20 0:14am
v6
Comments
Garth J Lancaster 22-Jul-20 3:46am    
You don't say what is 'wrong' with your code / what it is doing (or not), or even if it compiles - might be an idea to use Improve question and update it ..
Member 14895792 22-Jul-20 5:37am    
I improved the question

You are using the same DataTable for both DataGridViews so they will both show all the rows, including the duplicates that you are adding. You should use the DataGridViewRow.Clone Method (System.Windows.Forms) | Microsoft Docs[^] to create new DataGridViewRow objects, and add them to dgTwo. Alternatively, create a new DataTable and use that as the source for dgTwo.
 
Share this answer
 
The biggest issue I see with your code is where you have two things in 'inner loops' (and too many loops) that probably, given a slightly better design, shouldnt be there eg
dgTwo.DataSource = dt;
I would take that line 'out of there', and create a new function that returns that datatable, THEN updates the 2nd datatable, so
DataTable dt = getRowsToCopy();
dgTwo.DataSource = dt;
Similarly, this
if (chekAll.Checked == true)
{
  foreach (DataGridViewRow item in dgOne.Rows)
  {
    if ((bool)item.Cells[0].Value == true)
    {
      DataTable dt = new DataTable();
      ...
would be better 'outside' the loops so maybe
[Modifiers] DataTable getRowsToCopy()
{
  DataTable dt = new DataTable();
  dt.Columns.Add("Serial No:");
  dt.Columns.Add("Register No:");
  dt.Columns.Add("Acedemic Year");
  dt.Columns.Add("Full Name");
  dt.Columns.Add("Name with Initial");
  dt.Columns.Add("Course");
  dt.Columns.Add("Address");
  dt.Columns.Add("Telephpne No:");
  dt.Columns.Add("Province");
  dt.Columns.Add("District");
  dt.Columns.Add("AGA Division");
  dt.Columns.Add("Base of Recruitment");

  if (chekAll.Checked == true)
  {
    foreach (DataGridViewRow item in dgOne.Rows)
    {
      if ((bool)item.Cells[0].Value == true) 
      {
        bool isSelected = Convert.ToBoolean(row.Cells["Column0"].Value);
        if (isSelected)
        {
          dt.Rows.Add(row.Cells[1].Value, row.Cells[2].Value, row.Cells[3].Value, row.Cells[4].Value, row.Cells[5].Value, row.Cells[6].Value, row.Cells[7].Value, row.Cells[8].Value, row.Cells[9].Value, row.Cells[10].Value, row.Cells[11].Value, row.Cells[12].Value);
        }
      }
    }
  }
  return dt;
}


Disclaimer : untested, just quickly re-arranged
 
Share this answer
 
v2

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