c# datagridview add rows code example
Example 1: dynamically add rows to datagridview c#
DataTable dt = new DataTable();
// first add your columns
for (int i = 0; i < count; i++)
{
dt.Columns.Add(fromFileFields[i]);
}
// and then add your rows
for (int i = 0; i < count; i++)
{
var row = dt.NewRow();
// Set values for columns with row[i] = xy
dt.Rows.Add(row);
}
datagridview.DataSource = dt;
Example 2: add rows to datagridview vb.net
Me.DataGridView1.Rows.Add(TextBox1.Text)
If DataGridView1.SelectedRows.Count > 0 Then
For i As Integer = DataGridView1.SelectedRows.Count - 1 To 0 Step -1
DataGridView1.Rows.RemoveAt(DataGridView1.SelectedRows(i).Index)
Next
Else
MsgBox("No row(s) have been selected")
End If