2
Answers

copy data from one datagridview to another if strings match

Photo of Ghamai Khuram

Ghamai Khuram

6y
1.7k
1
I am importing data from excel to Datagridview1 and Datagridview2 which has columns of Group number, Member name
I want to copy the Group number from dataGridview2 to Datagridview1 if Member name is matching non case sensitive ignore characters like .,- and ect. something like .contains will be great
below are the codes that i already tried and after error/failed i converted them to comment
foreach (DataGridViewRow kk in datagridcorrect.Rows)
{
foreach (DataGridViewRow cell in Memnum.Rows)
{
//string corct;
//string memn;
//corct = kk.Cells[0].Value.ToString();
// memn = cell.Cells[0].Value.ToString();
// corct = Convert.ToString(kk.Cells[0].Value);
// memn = Convert.ToString(cell.Cells[0].Value);
//bool b;
// b = kk.Cells[0].Value.ToString().Contains(cell.Cells.ToString());
// if(mem.Equals(correct))
//if (object.Equals(kk.Cells[1].Value, cell.Cells[1].Value))
// if(b == true)
{
kk.Cells[0].Value = cell.Cells[0].Value;
}
}
}
I really worked alot on this any help will be highly appreciated!
thanks

Answers (2)

2
Photo of Ravishankar N
142 13.5k 1.8m 6y
Hi Ghamai Khuram
 
Copy Data Grid view rows to another u can use this code below, 
  1. private DataGridView CopyDataGridView(DataGridView dgv_org)  
  2. {  
  3.     DataGridView dgv_copy = new DataGridView();  
  4.     try  
  5.     {  
  6.         if (dgv_copy.Columns.Count == 0)  
  7.         {  
  8.             foreach (DataGridViewColumn dgvc in dgv_org.Columns)  
  9.             {  
  10.                 dgv_copy.Columns.Add(dgvc.Clone() as DataGridViewColumn);  
  11.             }  
  12.         }  
  13.   
  14.         DataGridViewRow row = new DataGridViewRow();  
  15.   
  16.         for (int i = 0; i < dgv_org.Rows.Count; i++)  
  17.         {  
  18.             row = (DataGridViewRow)dgv_org.Rows[i].Clone();  
  19.             int intColIndex = 0;  
  20.             foreach (DataGridViewCell cell in dgv_org.Rows[i].Cells)  
  21.             {  
  22.                 row.Cells[intColIndex].Value = cell.Value;  
  23.                 intColIndex++;  
  24.             }  
  25.             dgv_copy.Rows.Add(row);  
  26.         }  
  27.         dgv_copy.AllowUserToAddRows = false;  
  28.         dgv_copy.Refresh();  
  29.   
  30.     }  
  31.     catch (Exception ex)  
  32.     {  
  33.         cf.ShowExceptionErrorMsg("Copy DataGridViw", ex);  
  34.     }  
  35.     return dgv_copy;  
  36. }  
In order to copy selected rows   
  1. private DataGridView CopyDataGridView(DataGridView dgv_org)  
  2.        {  
  3.            DataGridView dgv_copy = new DataGridView();  
  4.            try  
  5.            {  
  6.                if (dgv_copy.Columns.Count == 0)  
  7.                {  
  8.                    foreach (DataGridViewColumn dgvc in dgv_org.Columns)  
  9.                    {  
  10.                        dgv_copy.Columns.Add(dgvc.Clone() as DataGridViewColumn);  
  11.                    }  
  12.                }  
  13.   
  14.                DataGridViewRow row = new DataGridViewRow();  
  15.   
  16.                for (int i = 0; i < dgv_org.SelectedRows.Count; i++)  
  17.                {  
  18.                    row = (DataGridViewRow)dgv_org.SelectedRows[i].Clone();  
  19.                    int intColIndex = 0;  
  20.                    foreach (DataGridViewCell cell in dgv_org.SelectedRows[i].Cells)  
  21.                    {  
  22.                        row.Cells[intColIndex].Value = cell.Value;  
  23.                        intColIndex++;  
  24.                    }  
  25.                    dgv_copy.Rows.Add(row);  
  26.                }  
  27.                dgv_copy.AllowUserToAddRows = false;  
  28.                dgv_copy.Refresh();  
  29.   
  30.            }  
  31.            catch (Exception ex)  
  32.            {  
  33.                System.Console.WriteLine(ex.ToString());  
  34.   
  35.            }  
  36.            return dgv_copy;  
  37.        }  
I Hope it will help you 
Accepted
0
Photo of Vincent Maverick Durano
124 14.9k 4.2m 6y
I Have not used DataGridView before but I hope this short discussion may helps: https://stackoverflow.com/questions/6336239/copy-datagridviews-rows-into-another-datagridview