als rij toevoegen aan DataTable
DataRow row = datatable1.NewRow();
row["column2"]="column2";
row["column6"]="column6";
datatable1.Rows.Add(row);
Hoe zit het met DataGridView
?
Dat kun je doen:
DataGridViewRow row = (DataGridViewRow)yourDataGridView.Rows[0].Clone();
row.Cells[0].Value = "XYZ";
row.Cells[1].Value = 50.2;
yourDataGridView.Rows.Add(row);
of:
DataGridViewRow row = (DataGridViewRow)yourDataGridView.Rows[0].Clone();
row.Cells["Column2"].Value = "XYZ";
row.Cells["Column6"].Value = 50.2;
yourDataGridView.Rows.Add(row);
Een andere manier:
this.dataGridView1.Rows.Add("five", "six", "seven","eight");
this.dataGridView1.Rows.Insert(0, "one", "two", "three", "four");
Van: http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.rows.aspx
Zoals dit:
dataGridView1.Columns[0].Name = "column2";
dataGridView1.Columns[1].Name = "column6";
string[] row1 = new string[] { "column2 value", "column6 value" };
dataGridView1.Rows.Add(row1);
Of je moet de waarden individueel instellen met de eigenschap .Rows()
, zoals dit:
dataGridView1.Rows[1].Cells[0].Value = "cell value";
Het toevoegen van een nieuwe rij in een DGV zonder rijen met Add() roept het SelectionChanged event op voordat je gegevens kunt invoegen (of een object kunt binden in Tag property).
Een kloonrij maken van RowTemplate is veiliger imho:
//assuming that you created columns (via code or designer) in myDGV
DataGridViewRow row = (DataGridViewRow) myDGV.RowTemplate.Clone();
row.CreateCells(myDGV, "cell1", "cell2", "cell3");
myDGV.Rows.Add(row);