C# 如何以编程方式向datagridview添加新行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10063770/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 12:19:00  来源:igfitidea点击:

How to add a new row to datagridview programmatically

c#winformsdatagridviewrow

提问by LK Yeung

if add row to DataTable

如果添加行 DataTable

DataRow row = datatable1.NewRow();
row["column2"]="column2";
row["column6"]="column6";
datatable1.Rows.Add(row);

How about DataGridView??

怎么样DataGridView??

采纳答案by Habib

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

回答by Mahmoud Gamal

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

回答by Defkon1

Adding a new row in a DGV with no rows with Add()raises SelectionChangedevent before you can insert any data (or bind an object in Tag property).

在插入任何数据(或在 Tag 属性中绑定对象)之前,使用Add()在没有行的 DGV 中添加新行会引发SelectionChanged事件。

Create a clone row from RowTemplateis 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);

回答by Jizakh

Like this:

像这样:

var index = dgv.Rows.Add();
dgv.Rows[index].Cells["Column1"].Value = "Column1";
dgv.Rows[index].Cells["Column2"].Value = 5.6;
//....

回答by Anders

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)    

回答by silent tone

If you need to manipulate anything aside from the Cell Value string such as adding a Tag, try this:

如果您需要操作除 Cell Value 字符串之外的任何内容,例如添加标签,请尝试以下操作:

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

回答by Sherif Hamdy

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;

回答by Overdrive77

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)

回答by DotNET

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

回答by Rei Salazar

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