Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
have two datagridview they are; dgOne and dgTwo,
sql data display in dgOne.
and add checkedlistbox to choose column what we need to copy.
when select rows and select column from checkedlistbox.
problem is cels and rows copy correctly but rows and columns are empty dgOne values not display on dgTwo. below is the code that I tried.

What I have tried:

else if (chekAll.Checked == false)
                {

                    try
                    {
                        List<string> list = new List<string>();
                        
                        foreach (var item in checkedListBox1.CheckedItems)
                        {
                            list.Add(item.ToString());
                        }

                        DataTable table = new DataTable();

                        for (int i = 0; i < list.Count; i++)
                        {
                            table.Columns.Add(list[i].ToString(), typeof(string));
                        }

                        foreach (DataGridViewRow row in dgOne.Rows)
                        {
                            bool isSelected = Convert.ToBoolean(row.Cells["Column0"].Value);

                            if (isSelected)
                            {

                                DataRow rowitem = table.NewRow();

                                for (int i = 0; i < list.Count; i++)
                                {
                                    int count;
                                    if ("Serial No" == list[i]) {
                                        count = 1;
                                    }
                                    else if ("Register No" == list[i])
                                    {
                                        count = 2;
                                    }
                                    else if ("Acedemic Year" == list[i])
                                    {
                                        count = 3;
                                    }
                                    else if ("Full Name" == list[i])
                                    {
                                        count = 4;
                                    }
                                    else if ("Name with Initial" == list[i])
                                    {
                                        count = 5;
                                    }

                                    else if ("Course" == list[i])
                                    {
                                        count = 6;
                                    }

                                    else if ("Address" == list[i])
                                    {
                                        count = 7;
                                    }

                                    else if ("Telephpne No" == list[i])
                                    {
                                        count = 8;
                                    }

                                    else if ("Province" == list[i])
                                    {
                                        count = 9;
                                    }

                                    else if ("District" == list[i])
                                    {
                                        count = 10;
                                    }

                                    else if ("AGA Division" == list[i])
                                    {
                                        count = 11;
                                    }

                                    else if ("Base of Recruitment" == list[i])
                                    {
                                        count = 12;
                                    }

                                }
                                table.Rows.Add(rowitem);
                            }
                            dgTwo.DataSource = table;
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message + ex.ToString());
                    }


                }
Posted
Updated 23-Jul-20 21:09pm

Here, have a look at this previously answered query: How can I copy selected column data and row from one datagridview to another in C# using checkedlistbox.[^]

BTW, for learning, do have a look at this: Copy datagridview row into another datagridview[^]
 
Share this answer
 
v2
Comments
Maciej Los 24-Jul-20 4:02am    
5ed!
Sandeep Mewara 24-Jul-20 4:47am    
:thumbsup:
else if (chekAll.Checked == false)
                {

                    try
                    {
                        List<string> list = new List<string>();
                        
                        foreach (var item in checkedListBox1.CheckedItems)
                        {
                            list.Add(item.ToString());
                        }

                        DataTable table = new DataTable();

                        for (int i = 0; i < list.Count; i++)
                        {
                            table.Columns.Add(list[i].ToString(), typeof(string));
                        }

                        foreach(DataGridViewRow row in dgOne.Rows)
                        {
                            bool isSelected = Convert.ToBoolean(row.Cells["Column0"].Value);

                            if (isSelected)
                            {

                                DataRow rowitem = table.NewRow();
                                int count = 0;


                                for (int i = 0; i < list.Count; i++)
                                {
                                    //rowitem[list[i]] = row.Cells[list[i]].ToString();

                                    if ("Serial No" == list[i])
                                    {
                                        count = 1;
                                    }
                                    else if ("Register No" == list[i])
                                    {
                                        count = 2;
                                    }
                                    else if ("Exam No" == list[i])
                                    {
                                        count = 3;
                                    }
                                    else if ("Acedemic Year" == list[i])
                                    {
                                        count = 4;
                                    }
                                    else if ("Full Name" == list[i])
                                    {
                                        count = 5;
                                    }
                                    else if ("Name with Initial" == list[i])
                                    {
                                        count = 6;
                                    }

                                    else if ("Course" == list[i])
                                    {
                                        count = 7;
                                    }

                                    else if ("Address" == list[i])
                                    {
                                        count = 8;
                                    }

                                    else if ("Telephpne No" == list[i])
                                    {
                                        count = 9;
                                    }

                                    else if ("Province" == list[i])
                                    {
                                        count = 10;
                                    }

                                    else if ("District" == list[i])
                                    {
                                        count = 11;
                                    }

                                    else if ("AGA Division" == list[i])
                                    {
                                        count = 12;
                                    }

                                    else
                                    {
                                        count = 13;
                                    }
                                    rowitem[i] = row.Cells[count].Value.ToString();
                                }
                                table.Rows.Add(rowitem);
                            }
                            dgTwo.DataSource = table;
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message + ex.ToString());
                    }


                }
 
Share this answer
 
Comments
Maciej Los 24-Jul-20 4:03am    
Is this an answer?
Member 14895792 24-Jul-20 4:06am    
yes its working

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