Reputation: 47
I have two datagridviews in one form which are having some similar colums When the user clicks a button, the selectedrow's data of 2nd and the 3rd column of 2nd datagridview should go to the 1st and the 2nd column of the 1st datagridview. I've retrieved the data using
string strppno = dataGridView2.SelectedCells[1].Value.ToString();
string strpartno = dataGridView2.SelectedCells[2].Value.ToString();
and i can paste in the second datagridview using
dataGridView1.Rows[0].Cells[1].Value = strppno;
dataGridView1.Rows[0].Cells[2].Value = strpartno;
how ever i cannot figure out how to loop it so that it can be used multiple times or or multiple selected values.
Can someone please help me out. I thank you in advance for any suggestions.
first attempt.
for (int index = 0; index < dataGridView2.Rows.Count; index++)
{
int editIndex = dataGridView1.Rows.Add();
dataGridView1.Rows[editIndex].Cells[0].Value = dataGridView2.Rows[index].Cells[1].Value;
dataGridView1.Rows[editIndex].Cells[1].Value = dataGridView2.Rows[index].Cells[2].Value;
}
this bring out all the contents from the datagridview 2. I just want the selected one.
Second attempt.
string strppno = dataGridView2.SelectedCells[1].Value.ToString();
string strpartno = dataGridView2.SelectedCells[2].Value.ToString();
for (int i = dataGridView2.SelectedRows.Count; i < dataGridView2.SelectedRows.Count + 1; i++)
{
dataGridView1.Rows[i].Cells[i].Value = strppno;
dataGridView1.Rows[i].Cells[i + 1].Value = strpartno;
}
dataGridView1.Rows.Add(dataGridView2.SelectedRows.Count);
}
this kinda bring things that i want but in the wrong cell plus the loop keeps on running. :( I kept fiddling around with the 2nd loop but can't get the expected results.. :(
Upvotes: 1
Views: 2789
Reputation: 47
Ok So i got it... the problem happens when you add a new row to the dataGridView. It does not add after the existing data but adds before it. hence there is occurs an exception. Solution is below just in case anyone else needs it. :)
int count =0; // declare in public class
private void button3_Click(object sender, EventArgs e)
{
if (dataGridView2.SelectedRows.Count > 0)
{
dataGridView1.Rows.Add(dataGridView2.SelectedRows.Count); // add rows before entering data inside the new dataGridView.
}
foreach (DataGridViewRow row in dataGridView2.SelectedRows)
{
{
string value1 = row.Cells[0].Value.ToString();
string value2 = row.Cells[1].Value.ToString();
dataGridView1.Rows[count].Cells[0].Value = value1;
dataGridView1.Rows[count].Cells[1].Value = value2;
}
}
count =count + 1;
dataGridView2.ClearSelection(); // I used this because i have to work with 4 dataGridViews
}
Upvotes: 0
Reputation:
something like this will be useful to you:
private void dataGridView_SelectionChanged(object sender, EventArgs e) {
foreach (DataGridViewRow row in dataGridView.SelectedRows) {
string value1 = row.Cells[0].Value.ToString();
string value2 = row.Cells[1].Value.ToString();
//...
//also set the second datagrid from here too
}
}
So, in your example, you'll want to be using just the 2nd and 3rd Cell Value, and assigning them to your other datagrid :)
As a side note, to make your variables more readable/etc, you should get used to using camelCase,
i.e.
your string strpartno
should become string strPartNo
as well as naming dataGridViews properly/etc
from your comment, I have realised your insertInto code needs to be amended slightly:
Currently, your placing them:
dataGridView1.Rows[0].Cells[1].Value = strppno;
dataGridView1.Rows[0].Cells[2].Value = strpartno;
^
|
this will only place it into index 0 of the datagrid
So, what i suggest is to first
int count = ... //count number of rows in SecondDatagrid
Then do:
dataGridView1.Rows[count++].Cells[1].Value = strppno;
dataGridView1.Rows[count++].Cells[2].Value = strpartno;
^
|
this will only place it into next
available row of the datagrid
that way, you're still inserting into the first two columns, but not overwriting the values currently stored.
Upvotes: 1