Click here to Skip to main content
15,563,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I want to transform row data from datagridview1 to datagrid 2 when buttonassign click.It means hide the row from datagrid view 1 and add it to datagridvitew2.I used dataset to bind data for 2 grid views.

code :
C#
private void btnassign_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < dataGridView1.Rows.Count; i++)
        {

            dataGridView2.Rows.Add(dataGridView1.Rows[i]);
        }

    }


error i got:
" Rows cannot be programmatically added to the DataGridView's rows collection when the control is data-bound"
Posted
Updated 9-Oct-11 18:23pm
v2

1 solution

Add the row to your data source instead..
 
Share this answer
 
Comments
hasithakaru 11-Oct-11 7:56am    
yeah.....i did like so.but it couldn't work.Tbe whole code is like below

public partial class frmCashBeniToSalGrade : Form
{
UI ui = new UI();
BLLCashBeniToSalGrade bll = new BLLCashBeniToSalGrade();

public frmCashBeniToSalGrade()
{
InitializeComponent();
}

private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{

}

private void frmCashBeniToSalGrade_Load(object sender, EventArgs e)
{
try
{
DataSet ds = new DataSet();
ds = bll.Get_Salary_Band();
ui.bindDatasetToCombo(cmbSalGrade, "SALBAN_NAME", "SALBAN_CODE", ds);
cmbSalGrade.Text = "---Select Salary Grade---";

DataSet ds1 = new DataSet();
ds1 = bll.Get_Cash_Benifits();
dataGridView1.DataSource = ds1;
dataGridView1.DataMember = "HS_HR_CASH_BENEFIT";

}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}

private void groupBox1_Enter(object sender, EventArgs e)
{

}

private void cmbSalGrade_SelectedIndexChanged(object sender, EventArgs e)
{
DataSet ds2 = new DataSet();
ds2 = bll.Get_Cash_Benifits_Assigned(cmbSalGrade.SelectedValue.ToString());
dataGridView2.DataSource = ds2;
dataGridView2.DataMember = "HS_HR_CASHBEN_SALARYBAND";

}

private void btnassign_Click(object sender, EventArgs e)
{
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{

dataGridView2.Rows.Add(dataGridView1.Rows[i]);
}

}
}
Espen Harlinn 13-Oct-11 17:28pm    
My 5

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