Hello,
I am using this code for display items in datagridview using datatable. but it's showing error:-
Rows cannot be programmatically added to the DataGridView's rows collection when the control is data-bound.
for (int i = 0; i < dt.Rows.Count; i++)
{
dgvDisplayTiles.Rows.Add(); // error
dgvDisplayTiles.Rows[i].Cells[0].Value = dt.Rows[i][0].ToString();
dgvDisplayTiles.Rows[i].Cells[1].Value = dt.Rows[i][1].ToString();
dgvDisplayTiles.Rows[i].Cells[2].Value = dt.Rows[i][2].ToString();
Bitmap img = new Bitmap(Application.StartupPath + "\\" + dt.Rows[i][3].ToString());
dgvDisplayTiles.Rows[i].Cells[3].Value = img;
try
{
Bitmap img3 = new Bitmap(Application.StartupPath + "\\" + dt.Rows[i][4].ToString());
dgvDisplayTiles.Rows[i].Cells[4].Value = img3;
}
catch (Exception ex)
{
Bitmap img3 = new Bitmap(Application.StartupPath + "\\Library\\NoTiles.png");
dgvDisplayTiles.Rows[i].Cells[4].Value = img3;
}
}
dgvDisplayTiles.AutoGenerateColumns = false;
dgvDisplayTiles.DataSource = dt;
Please help me.
Thanks in Advance.
Ankit Agarwal
Software Engineer
I am using this code for display items in datagridview using datatable. but it's showing error:-
Rows cannot be programmatically added to the DataGridView's rows collection when the control is data-bound.
for (int i = 0; i < dt.Rows.Count; i++)
{
dgvDisplayTiles.Rows.Add(); // error
dgvDisplayTiles.Rows[i].Cells[0].Value = dt.Rows[i][0].ToString();
dgvDisplayTiles.Rows[i].Cells[1].Value = dt.Rows[i][1].ToString();
dgvDisplayTiles.Rows[i].Cells[2].Value = dt.Rows[i][2].ToString();
Bitmap img = new Bitmap(Application.StartupPath + "\\" + dt.Rows[i][3].ToString());
dgvDisplayTiles.Rows[i].Cells[3].Value = img;
try
{
Bitmap img3 = new Bitmap(Application.StartupPath + "\\" + dt.Rows[i][4].ToString());
dgvDisplayTiles.Rows[i].Cells[4].Value = img3;
}
catch (Exception ex)
{
Bitmap img3 = new Bitmap(Application.StartupPath + "\\Library\\NoTiles.png");
dgvDisplayTiles.Rows[i].Cells[4].Value = img3;
}
}
dgvDisplayTiles.AutoGenerateColumns = false;
dgvDisplayTiles.DataSource = dt;
Please help me.
Thanks in Advance.
Ankit Agarwal
Software Engineer