C# 使用现有列向 datagridview 添加行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/148854/
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
Adding rows to datagridview with existing columns
提问by Nacho
I have a DataGridView
with several created columns. I've add some rows and they get displayed correctly; however, when I click on a cell, the content disappears.
我有DataGridView
几个创建的列。我添加了一些行,它们显示正确;但是,当我单击一个单元格时,内容消失了。
What am I doing wrong?
我究竟做错了什么?
The code is as follows:
代码如下:
foreach (SaleItem item in this.Invoice.SaleItems)
{
DataGridViewRow row = new DataGridViewRow();
gridViewParts.Rows.Add(row);
DataGridViewCell cellQuantity = new DataGridViewTextBoxCell();
cellQuantity.Value = item.Quantity;
row.Cells["colQuantity"] = cellQuantity;
DataGridViewCell cellDescription = new DataGridViewTextBoxCell();
cellDescription.Value = item.Part.Description;
row.Cells["colDescription"] = cellDescription;
DataGridViewCell cellCost = new DataGridViewTextBoxCell();
cellCost.Value = item.Price;
row.Cells["colUnitCost1"] = cellCost;
DataGridViewCell cellTotal = new DataGridViewTextBoxCell();
cellTotal.Value = item.Quantity * item.Price;
row.Cells["colTotal"] = cellTotal;
DataGridViewCell cellPartNumber = new DataGridViewTextBoxCell();
cellPartNumber.Value = item.Part.Number;
row.Cells["colPartNumber"] = cellPartNumber;
}
Thanks!
谢谢!
回答by Nacho
Edit: oops! made a mistake on the second line of code. - fixed it.
编辑:哎呀!在第二行代码上犯了一个错误。- 修复。
Sometimes, I hate defining the datasource property.
有时,我讨厌定义数据源属性。
I think that whenever you create and set a new row for "row", for some weird reason,the old value get disposed. try not using an instance to hold the rows you create :
我认为,每当您为“行”创建和设置新行时,出于某种奇怪的原因,旧值都会被处理掉。尽量不要使用实例来保存您创建的行:
int i;
i = gridViewParts.Rows.Add( new DataGridViewRow());
DataGridViewCell cellQuantity = new DataGridViewTextBoxCell();
cellQuantity.Value = item.Quantity;
gridViewParts.Rows[i].Cells["colQuantity"] = cellQuantity;
It seems like cells work fine with the cell instances. I have no idea why it is different for rows though. More testings may be required...
似乎单元格与单元格实例一起工作正常。我不知道为什么行不同。可能需要更多的测试...
回答by Bobby
Just to extend this question, there's also another way to add a row to a DataGridView
, especially if the columns are always the same:
只是为了扩展这个问题,还有另一种向 a 添加行的方法DataGridView
,特别是如果列始终相同:
object[] buffer = new object[5];
List<DataGridViewRow> rows = new List<DataGridViewRow>();
foreach (SaleItem item in this.Invoice.SaleItems)
{
buffer[0] = item.Quantity;
buffer[1] = item.Part.Description;
buffer[2] = item.Price;
buffer[3] = item.Quantity * item.Price;
buffer[4] = item.Part.Number;
rows.Add(new DataGridViewRow());
rows[rows.Count - 1].CreateCells(gridViewParts, buffer);
}
gridViewParts.Rows.AddRange(rows.ToArray());
Or if you like ParamArrays:
或者,如果您喜欢 ParamArrays:
List<DataGridViewRow> rows = new List<DataGridViewRow>();
foreach (SaleItem item in this.Invoice.SaleItems)
{
rows.Add(new DataGridViewRow());
rows[rows.Count - 1].CreateCells(gridViewParts,
item.Quantity,
item.Part.Description,
item.Price,
item.Quantity * item.Price,
item.Part.Number
);
}
gridViewParts.Rows.AddRange(rows.ToArray());
The values in the buffer need to be in the same order as the columns (including hidden ones) obviously.
显然,缓冲区中的值需要与列(包括隐藏的)的顺序相同。
This is the fastest way I found to get data into a DataGridView
without binding the grid against a DataSource
. Binding the grid will actually speed it up by a significant amount of time, and if you have more then 500 rows in a grid, I strongly recommend to bind it instead of filling it by hand.
这是我发现将数据放入 aDataGridView
而不将网格绑定到 a 的最快方法DataSource
。绑定网格实际上会显着加快速度,如果网格中有超过 500 行,我强烈建议绑定它而不是手动填充它。
Binding does also come with the bonus that you can keep the Object in tact, f.e. if you want to operate on the selected row, you can do this is the DatagridView is bound:
绑定也有一个好处,你可以保持对象的完整,如果你想对选定的行进行操作,你可以这样做是绑定了 DatagridView:
if(gridViewParts.CurrentRow != null)
{
SaleItem item = (SalteItem)(gridViewParts.CurrentRow.DataBoundItem);
// You can use item here without problems.
}
It is advised that your classes which get bound do implement the System.ComponentModel.INotifyPropertyChanged
interface, which allows it to tell the grid about changes.
建议您绑定的类确实实现了System.ComponentModel.INotifyPropertyChanged
接口,这允许它告诉网格有关更改的信息。