Click here to Skip to main content
15,889,462 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello frens,
i have a problem while doing this code.
here i want to select multiple values of first grid but one by one. as soon i select the values from one grid, it should disappear from first grid and seen in second grid, i have this code which shows only one data in second grid as everytime a new datatable is created but cannot get all the selected rows in second grid please help ...
the code i have written is like this...

C#
private void dataGridView1_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
       {
           foreach (DataGridViewRow r in dataGridView1.SelectedRows)
           {
               int stdid = Convert.ToInt32(dataGridView1.CurrentRow.Cells[0].Value.ToString());
               string stdname = dataGridView1.CurrentRow.Cells[1].Value.ToString();

               dataGridView1.Rows.Remove(r);

               //now adding the datas into second datagridview by adding new row each time
               //first creating the column and adding new row each time


               DataTable dt = new DataTable();
               dt.Columns.Add("Student ID");
               dt.Columns.Add("Roll Number");
               dt.Columns.Add("Student Names");
               DataRow dr;
               dr = dt.NewRow();
               int i = 1;
               dr[0] = stdid;
               dr[1] = i;
               dr[2] = stdname;
               dt.Rows.Add(dr);
               i++;
               this.dataGridView2.DataSource = dt;

           }

       }
Posted
Updated 20-Jun-12 6:10am
v2
Comments
[no name] 20-Jun-12 12:12pm    
You are creating a new datatable on each iteration through your foreach.

1 solution

Don't create a new DataTable for the second grid everytime you select a row on the first grid.
in the form load, create your DataTable and columns for the second grid, something like this:

C#
private DataTable SecondDataTable = new DataTable();

private void MainForm_Load(System.Object sender, System.EventArgs e)
{
    SecondDataTable.Columns.Add("Student ID");
    SecondDataTable.Columns.Add("Roll Number");
    SecondDataTable.Columns.Add("Student Names");
}

private void dataGridView1_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
foreach (DataGridViewRow r in dataGridView1.SelectedRows)
{
int stdid = Convert.ToInt32(dataGridView1.CurrentRow.Cells[0].Value.ToString());
string stdname = dataGridView1.CurrentRow.Cells[1].Value.ToString();
 
dataGridView1.Rows.Remove(r);
 
//now adding the datas into second datagridview by adding new row each time
DataRow dr;
dr = SecondDataTable.NewRow();
int i = 1;
dr[0] = stdid;
dr[1] = i;
dr[2] = stdname;
SecondDataTable.Rows.Add(dr);
i++;
this.dataGridView2.DataSource = SecondDataTable;

} 

}
 
Share this answer
 
Comments
bishnuk 20-Jun-12 12:46pm    
at position dr[0] , it says that no columns can be added ... i hope u can correct is Mr Sameer
with thanks
Sameer Alomari 20-Jun-12 15:40pm    
I believe that you redefine the table somewhere in the code, if you did that, then you will loose the columns that you have created in the form load event.
Check the number of the columns in the datatable before creating a new row, if it's not 3 columns, then find where you redefine your datatable and delete that line.

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