简体   繁体   中英

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;
}

Try to use

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM