Click here to Skip to main content
14,641,302 members
Rate this:
Please Sign up or sign in to vote.
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:

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
Rate this:
Please Sign up or sign in to vote.

Solution 1

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.
   
Rate this:
Please Sign up or sign in to vote.

Solution 2

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
   
v2

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




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