添加行与现有列的DataGridView [英] Adding rows to datagridview with existing columns

查看:106
本文介绍了添加行与现有列的DataGridView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 DataGridView的几个创建的列。我已经添加一些行,它们会正确显示;然而,当我点击一个单元格,内容将消失。

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.

我在做什么错了?

代码如下:

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!

推荐答案

只是为了延长这个问题,这里还有另一种方式来一行添加到 DataGridView的,特别是如果列始终是相同的

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:

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:

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.

这是我发现将数据放入一个 DataGridView的无约束力的反对电网<$最快的方法C $ C>数据源。结合网格将实际的时间显著量加速这一过程,如果你在网格中有500多个行,我强烈建议,而不是绑定手工填充它。

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.

绑定并还拿出奖金,你可以保持对象机智,铁如果要选定的行进行操作,就可以做到这一点是在DataGridView绑定:

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:

if(gridViewParts.CurrentRow != null)
{
    SaleItem item = (SalteItem)(gridViewParts.CurrentRow.DataBoundItem);
    // You can use item here without problems.
}



这是建议你的类当中去约束国家执行 System.ComponentModel.INotifyPropertyChanged 接口,这使得它能够告诉改变电网。

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.

这篇关于添加行与现有列的DataGridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆