Darth Sucuk
Darth Sucuk

Reputation: 254

How to add rows to datagridview while datagridview is bind

I have a datagridview and I filled with data.

 DataTable table = new DataTable();
 dataGridView1.DataSource = table;

 con = new SqlDataAdapter("SELECT * FROM TABLE "'", con);
 ds = new System.Data.DataSet();
 con .Fill(ds, "TABLE");

My problem is I have to add rows manually like the code below but it is just add one row.But what I need foreach's count row.

foreach (var a in names.Split(new char[] { ';' }))
{
    DataRow newRow = table.NewRow();
    table.Rows.Add(newRow);
    dataGridView2.Rows[i + 1].Cells[3].Value = a.ToString();
    i = i +1;
}

Upvotes: 1

Views: 130

Answers (1)

Nuisance
Nuisance

Reputation: 455

Try to use

DataTable dataTable = (DataTable)dataGridView2.DataSource;
DataRow drToAdd = dataTable.NewRow();
drToAdd[3] = a.ToString();
dataTable.Rows.Add(drToAdd);

Upvotes: 2

Related Questions