0

i have a DataGridView on the Form. I select some data from database and load them to datatable and after that i make reference this datatable to grid's datasorurce as below.

string sql = "";
sql = "SELECT id,name,surname,code FROM t_persons";
DataTable dt = new DataTable();
...
adapter.Fill(dt);
grid.DataSource = dt;

and after that i want to add new row to this grid with grid.Rows.Add() method. But every time it gives an error Rows cannot be programmatically added to the DataGridView's rows collection when the control is data-bound.
So whatis the problem and how can i solve it.

2
  • 1
    Solve it by adding a row to the DataTable (isn't the exception self-explanatory?). Commented Aug 23, 2012 at 8:54
  • You can add rows to DataTable instead Commented Aug 23, 2012 at 8:56

2 Answers 2

2

You should add row to the DataTable, not to the DataGridView. That is what the exception is saying. Try:

DataRow newRow = dt.NewRow();
dt.Rows.Add(newRow);
Sign up to request clarification or add additional context in comments.

Comments

1

Please you can add row directly to datatable and it's effect on gridview because it's bind to datatable.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.