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;
}
}
}
}