Bodi
Bodi

Reputation: 95

How to add new row to datagridview?

I have DataGridView filled with data from datasource (SQL). Now I want to a add new row, but I can't, because new data can't be added to bounded DataGridView...

I was trying to :

dataGridView1.Source = null;
dataGridView1.Rows.Add("1");

but it clears my previous data in table. How to do it, to add new row without deleting previous data?

Upvotes: 9

Views: 36933

Answers (4)

Jim Dagg
Jim Dagg

Reputation: 2032

The short answer is, you don't.

When you set your DataSource to null, you've broken the link between your DataGridView and your data source, so its data won't be persisted. You can't add a row to a bound DataGridView because it's supposed to represent the state of the underlying DataSource; you're effectively asking .net to make your table out of sync with its backing store, defeating the purpose of databinding in the first place.

If you want to add a row to the backing store, you should be adding a row in the DataSource, not in your DataGridView.

Upvotes: 1

Hemal Rathod
Hemal Rathod

Reputation: 93

You just add rows by using add method of rows collection

me.datagridview1.rows.add("first","second","third");

You can add any amount of items with array collection.

Upvotes: 0

casperOne
casperOne

Reputation: 74530

When you set the DataSource property to null, you are essentially removing all data from the DataGridView (since it doesn't know what to bind to anymore).

You have two options here. The first is to update the underlying data source. Let's assume that it's a DataTable. In this case, you'd do something like:

DataTable dt = dataGridView1.Source as DataTable;
dt.Rows.Add(new object[] { ... });

And then the DataGridView will pick up on the changes (note that if you are not binding to something that doesn't implement the INotifyCollectionChanged interface, you'll have to call the ResetBindings method to get the grid to refresh).

The other option is to let the DataGridView manage the rows. You can do this by manually adding each item using the Add method on the DataGridViewRowCollection returned by the Rows property:

foreach (var item in source)
{
    dataGridView1.Rows.Add("1", "2", "3", ...);
}

I wouldn't say the second solution is optimal, but it will work.

Finally, assuming you are binding to a DataTable (or some other materialization of the data from an underlying data source), this doesn't do anything about to updating underlying data source (that would be a separate question).

Upvotes: 8

Andrew
Andrew

Reputation: 7768

maybe you want to do it manually and detailed? Something like this?

        DataSet ds = new DataSet();
        OleDbDataAdapter adapter = null;
        adapter = new OleDbDataAdapter("SELECT * FROM  WHERE", conn);
        adapter.Fill(ds);
        dataGridView1.ColumnCount = 5; //how many columns returns your SQL query? starts with 0

        dataGridView1.Columns[0].Name = "COl-1";
        dataGridView1.Columns[1].Name = "COl-2";
        dataGridView1.Columns[2].Name = "COl-3";
        dataGridView1.Columns[3].Name = "COl-4";
        dataGridView1.Columns[4].Name = "COl-5";

        DataTable dt = ds.Tables[0];

        foreach (DataRow dr in dt.Rows)
        {
            dataGridView1.Rows.Add(
            (dr["COL_HEADER_NAME1"].ToString()),
            (dr["COL_HEADER_NAME2"].ToString()),
            (dr["COL_HEADER_NAME3"].ToString()),
            (dr["COL_HEADER_NAME4"].ToString()),
            (dr["COL_HEADER_NAME5"].ToString()));
        }

Upvotes: 0

Related Questions