[英]How to add a new row to datagridview programmatically
if add row to DataTable
如果向
DataTable
添加行
DataRow row = datatable1.NewRow();
row["column2"]="column2";
row["column6"]="column6";
datatable1.Rows.Add(row);
How about DataGridView
?? DataGridView
怎么样??
You can do:你可以做:
DataGridViewRow row = (DataGridViewRow)yourDataGridView.Rows[0].Clone();
row.Cells[0].Value = "XYZ";
row.Cells[1].Value = 50.2;
yourDataGridView.Rows.Add(row);
or:要么:
DataGridViewRow row = (DataGridViewRow)yourDataGridView.Rows[0].Clone();
row.Cells["Column2"].Value = "XYZ";
row.Cells["Column6"].Value = 50.2;
yourDataGridView.Rows.Add(row);
Another way:其它的办法:
this.dataGridView1.Rows.Add("five", "six", "seven","eight");
this.dataGridView1.Rows.Insert(0, "one", "two", "three", "four");
From: http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.rows.aspx来自: http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.rows.aspx
Like this:像这样:
var index = dgv.Rows.Add();
dgv.Rows[index].Cells["Column1"].Value = "Column1";
dgv.Rows[index].Cells["Column2"].Value = 5.6;
//....
Lets say you have a datagridview that is not bound to a dataset and you want to programmatically populate new rows...假设您有一个未绑定到数据集的数据网格视图,并且您想以编程方式填充新行...
Here's how you do it.这是你如何做的。
// Create a new row first as it will include the columns you've created at design-time.
int rowId = dataGridView1.Rows.Add();
// Grab the new row!
DataGridViewRow row = dataGridView1.Rows[rowId];
// Add the data
row.Cells["Column1"].Value = "Value1";
row.Cells["Column2"].Value = "Value2";
// And that's it! Quick and painless... :o)
Like this:像这样:
dataGridView1.Columns[0].Name = "column2";
dataGridView1.Columns[1].Name = "column6";
string[] row1 = new string[] { "column2 value", "column6 value" };
dataGridView1.Rows.Add(row1);
Or you need to set there values individually use the propery .Rows()
, like this:或者您需要使用属性
.Rows()
单独设置值,如下所示:
dataGridView1.Rows[1].Cells[0].Value = "cell value";
Adding a new row in a DGV with no rows with Add() raises SelectionChanged event before you can insert any data (or bind an object in Tag property).使用Add()在没有行的 DGV 中添加新行会引发SelectionChanged事件,然后才能插入任何数据(或在 Tag 属性中绑定 object)。
Create a clone row from RowTemplate is safer imho:从RowTemplate创建克隆行更安全恕我直言:
//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);
This is how I add a row if the dgrview is empty: (myDataGridView has two columns in my example)如果 dgrview 为空,这就是我添加一行的方式:(myDataGridView 在我的示例中有两列)
DataGridViewRow row = new DataGridViewRow();
row.CreateCells(myDataGridView);
row.Cells[0].Value = "some value";
row.Cells[1].Value = "next columns value";
myDataGridView.Rows.Add(row);
According to docs: "CreateCells() clears the existing cells and sets their template according to the supplied DataGridView template".根据文档:“CreateCells() 清除现有单元格并根据提供的 DataGridView 模板设置它们的模板”。
If the grid is bound against a DataSet / table its better to use a BindingSource like如果网格绑定到数据集/表,最好使用 BindingSource 之类的
var bindingSource = new BindingSource();
bindingSource.DataSource = dataTable;
grid.DataSource = bindingSource;
//Add data to dataTable and then call
bindingSource.ResetBindings(false)
here is another way to do such这是另一种方法
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
dataGridView1.ColumnCount = 3;
dataGridView1.Columns[0].Name = "Name";
dataGridView1.Columns[1].Name = "Age";
dataGridView1.Columns[2].Name = "City";
dataGridView1.Rows.Add("kathir", "25", "salem");
dataGridView1.Rows.Add("vino", "24", "attur");
dataGridView1.Rows.Add("maruthi", "26", "dharmapuri");
dataGridView1.Rows.Add("arun", "27", "chennai");
}
If you need to manipulate anything aside from the Cell Value string such as adding a Tag, try this:如果您需要操作单元格值字符串以外的任何内容,例如添加标签,请尝试以下操作:
DataGridViewRow newRow = (DataGridViewRow)mappingDataGridView.RowTemplate.Clone();
newRow.CreateCells(mappingDataGridView);
newRow.Cells[0].Value = mapping.Key;
newRow.Cells[1].Value = ((BusinessObject)mapping.Value).Name;
newRow.Cells[1].Tag = mapping.Value;
mappingDataGridView.Rows.Add(newRow);
If you are binding a List如果您正在绑定一个列表
List<Student> student = new List<Student>();
dataGridView1.DataSource = student.ToList();
student .Add(new Student());
//Reset the Datasource
dataGridView1.DataSource = null;
dataGridView1.DataSource = student;
If you are binding DataTable如果您正在绑定 DataTable
DataTable table = new DataTable();
DataRow newRow = table.NewRow();
// Add the row to the rows collection.
table.Rows.Add(newRow);
yourDGV.Rows.Add(column1,column2...columnx); //add a row to a dataGridview
yourDGV.Rows[rowindex].Cells[Cell/Columnindex].value = yourvalue; //edit the value
you can also create a new row and then add it to the DataGridView like this:您还可以创建一个新行,然后将其添加到 DataGridView,如下所示:
DataGridViewRow row = new DataGridViewRow();
row.Cells[Cell/Columnindex].Value = yourvalue;
yourDGV.Rows.Add(row);
If anyone wanted to Add DataTable as a source of gridview then--如果有人想将 DataTable 添加为 gridview 的来源,那么——
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("column1"));
dt.Columns.Add(new DataColumn("column2"));
DataRow dr = dt.NewRow();
dr[0] = "column1 Value";
dr[1] = "column2 Value";
dt.Rows.Add(dr);
dataGridView1.DataSource = dt;
An example of copy row from dataGridView and added a new row in The same dataGridView:从 dataGridView 复制行并在同一个 dataGridView 中添加新行的示例:
DataTable Dt = new DataTable();
Dt.Columns.Add("Column1");
Dt.Columns.Add("Column2");
DataRow dr = Dt.NewRow();
DataGridViewRow dgvR = (DataGridViewRow)dataGridView1.CurrentRow;
dr[0] = dgvR.Cells[0].Value;
dr[1] = dgvR.Cells[1].Value;
Dt.Rows.Add(dR);
dataGridView1.DataSource = Dt;
Consider a Windows Application and using Button Click Event put this code in it.考虑一个 Windows 应用程序并使用按钮单击事件将此代码放入其中。
dataGridView1.Rows
.Add(new object[] { textBox1.Text, textBox2.Text, textBox3.Text });
//Add a list of BBDD
var item = myEntities.getList().ToList();
//Insert a new object of type in a position of the list
item.Insert(0,(new Model.getList_Result { id = 0, name = "Coca Cola" }));
//List assigned to DataGridView
dgList.DataSource = item;
If you´ve already defined a DataSource
, You can get the DataGridView
´s DataSource
and cast it as a Datatable
.如果您已经定义了一个
DataSource
,您可以获取DataGridView
的DataSource
并将其转换为Datatable
。
Then add a new DataRow
and set the Fields Values.然后添加一个新的数据
DataRow
并设置字段值。
Add the new row to the DataTable
and Accept the changes.将新行添加到
DataTable
并接受更改。
In C# it would be something like this..在 C# 中它会是这样的..
DataTable dataTable = (DataTable)dataGridView.DataSource;
DataRow drToAdd = dataTable.NewRow();
drToAdd["Field1"] = "Value1";
drToAdd["Field2"] = "Value2";
dataTable.Rows.Add(drToAdd);
dataTable.AcceptChanges();
//header
dataGridView1.RowCount = 50;
dataGridView1.Rows[0].HeaderCell.Value = "Product_ID0";
//add row by cell
dataGridView1.Rows[1].Cells[0].Value = "cell value";
I think the cleanest way to do is invoking the DataGridView and using a lambda expression.我认为最干净的方法是调用 DataGridView 并使用 lambda 表达式。 Simple one liner while also keeping the code thread safe:
简单的一个衬里,同时还保持代码线程安全:
MyDataGridView.Invoke((MethodInvoker)(() => MyDataGridView.Rows.Add(param1, param2, paramX)));
I have found this useful more than once when the DataGrid is bound to a table.当 DataGrid 绑定到表时,我发现这不止一次有用。
DataTable dt = (DataTable)dgvData.DataSource;
DataRow row = dt.NewRow();
foreach (var something in something)
{
row["ColumnName"] = something ;
}
dt.Rows.Add(row);
dgvData.DataSource = dt;
string[] splited = t.Split('>');
int index = dgv_customers.Rows.Add(new DataGridViewRow());
dgv_customers.Rows[index].Cells["cust_id"].Value=splited.WhichIsType("id;");
But be aware, WhichIsType
is the extension method I created.但请注意,
WhichIsType
是我创建的扩展方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.