Click here to Skip to main content
15,943,477 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I used the following code to fill my datagridview from database
DataSet ds = new DataSet();
            OleDbDataAdapter da = new OleDbDataAdapter("select ID, [number], [date], name, place, placeID, numbertype, [note], [time] FROM CallsRegistray ORDER BY [date] DESC ", tool.getconnection());
            //OleDbDataAdapter da = new OleDbDataAdapter(, tool.getconnection());
            BindingSource bds = new BindingSource(ds, ds.Tables[0].TableName);
            dataGridView1.DataSource = bds;

I tried to use the following code to add row
dataGridView1.Rows.Add("test", "test");

but there is an error occur :
Rows cannot be programmatically added to the DataGridView's rows collection when the control is data-bound

how can I resolve this problem
best regards

using (OleDbConnection connection = new OleDbConnection(@ExtraConnection))

          OleDbCommand command = new OleDbCommand("SELECT * FROM "+txtSheet.Text, connection);
              command.CommandTimeout = 20;
              OleDbDataAdapter SDA = new OleDbDataAdapter(command);
              OleDbCommandBuilder SCB = new OleDbCommandBuilder(SDA);
              DataSet m_DataSet = new DataSet();
                foreach (DataTable table in m_DataSet.Tables)
                  foreach (DataRow row in table.Rows)

Share this answer
DataTable data = new DataTable();
        public StudentBatchSave()

  "StudentCode", Type.GetType("System.Int64"));
  "StudentName", Type.GetType("System.String"));
  "StudentFamily", Type.GetType("System.String"));
            this.dataGridView2.DataSource = data;
Share this answer
To make long story short, you need to create a datatable to which you should populate existing data first then add new rows and then set that data table as datasource for that gridview.
Share this answer

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