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
C#
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());
            da.Fill(ds);
            BindingSource bds = new BindingSource(ds, ds.Tables[0].TableName);
            dataGridView1.DataSource = bds;


I tried to use the following code to add row
C#
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
Posted

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

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


      }
 
Share this answer
 
C#
DataTable data = new DataTable();
     
        public StudentBatchSave()
        {
            InitializeComponent();

            this.data.Columns.Add("StudentCode", Type.GetType("System.Int64"));
            this.data.Columns.Add("StudentName", Type.GetType("System.String"));
            this.data.Columns.Add("StudentFamily", Type.GetType("System.String"));
         
            this.dataGridView2.DataSource = data;
        }
 
Share this answer
 
v2
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