C# 构建DataGridViewRow并添加到DataTable
我试图通过编程从两个不同的数据源构建一个数据网格。我有一个列表和一个数据网格。问题不在于我的数据处理,而在于DataGridViewRow对象的值。这是我的密码:C# 构建DataGridViewRow并添加到DataTable,c#,.net,winforms,datagridview,datagridviewrow,C#,.net,Winforms,Datagridview,Datagridviewrow,我试图通过编程从两个不同的数据源构建一个数据网格。我有一个列表和一个数据网格。问题不在于我的数据处理,而在于DataGridViewRow对象的值。这是我的密码: protected void buildGrid() { dgResults.Columns.Add( "sku", "SKU" ); dgResults.Columns.Add( "itemID", "Item ID" ); dgResults.Columns.Add(
protected void buildGrid()
{
dgResults.Columns.Add( "sku", "SKU" );
dgResults.Columns.Add( "itemID", "Item ID" );
dgResults.Columns.Add( "productName", "Product Name" );
dgResults.Columns.Add( "eBayQty", "eBay Qty" );
dgResults.Columns.Add( "stockQty", "Stock Qty" );
dgResults.Columns.Add( "difference", "Difference" );
//Add the eBayItem data to the table
foreach ( string[] eBayItem in ebayItems )
{
string SKU = eBayItem[1].ToString();
int eBayQty = Convert.ToInt32(eBayItem[2]);
string ProductName = "";
int stockQty = 0;
int qtyDifference = 0;
DataRow[] rows = dbData.Select( "sku ='" + SKU + "'" );
if (rows.Length == 1) {
stockQty = Convert.ToInt32( rows[0]["quantity"] );
ProductName = rows[0]["ProductName"].ToString();
}
qtyDifference = stockQty - eBayQty;
DataGridViewRow dgvr = new DataGridViewRow();
dgvr.SetValues( SKU, eBayItem[0].ToString(), ProductName, eBayQty, stockQty, qtyDifference );
if ( qtyDifference != 0 || eBayQty > stockQty )
{
dgvr.DefaultCellStyle.BackColor = System.Drawing.Color.Red;
dgvr.DefaultCellStyle.ForeColor = System.Drawing.Color.White;
}
else if ( stockQty > eBayQty )
dgvr.DefaultCellStyle.BackColor = System.Drawing.Color.LightGoldenrodYellow;
else
dgvr.DefaultCellStyle.BackColor = System.Drawing.Color.GreenYellow;
dgResults.Rows.Add(dgvr);
}
}
行正在添加到DataGrid中,并且它们被适当地着色,但是行中的每个单元格都不包含数据?我最后得到的只是几个设置了背景属性的空行
有人有什么想法吗
提前感谢。尝试在DataGridViewRow上使用BeginEdit和EndEdit以下是我对类似操作的解决方案的看法 出于演示目的,我构建了一个EbayItem类:-
public class EbayItem
{
public EbayItem()
{
}
public EbayItem(string sku, int id, string product, int ebayqty, int stockqty)
{
SKU = sku;
ID = id;
ProductName = product;
ebayQty = ebayqty;
stockQty = stockqty;
}
public string SKU { get; set; }
public int ID { get; set; }
public string ProductName { get; set; }
public int ebayQty { get; set; }
public int stockQty { get; set; }
public int difference
{
get { return stockQty - ebayQty; }
set { difference = value; }
}
}
在Windows窗体(Form1_Load)中,我创建了一些测试条目,并将它们添加到列表对象中。最后一个条目看起来要买2枚手榴弹,但库存为零:-
List<EbayItem> Inventory = new List<EbayItem>();
Inventory.Add(new EbayItem("SKU1", 1, "Ski-Mask", 1, 10));
Inventory.Add(new EbayItem("SKU2", 2, "Shotgun", 1, 10));
Inventory.Add(new EbayItem("SKU3", 3, "Rounds", 5, 10));
Inventory.Add(new EbayItem("SKU4", 4, "Grenade", 2, 0));
接下来,为我的DGV分配数据源,将其设置为绑定对象。这意味着对列表对象所做的任何更改都会自动传递到DGV:-
dataGridView1.DataSource = bs;
现在剩下的就是使用DGV的CellFormatting事件句柄。我使用此选项突出显示库存差异为零或更小的任何行:-
private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
int colIndex = e.ColumnIndex;
int rowIndex = e.RowIndex;
if (rowIndex >= 0 && colIndex >= 0)
{
DataGridViewRow theRow = dataGridView1.Rows[rowIndex];
int difference = (int)theRow.Cells[5].Value;
if (difference <= 0)
{
theRow.DefaultCellStyle.BackColor = System.Drawing.Color.Red;
theRow.DefaultCellStyle.ForeColor = System.Drawing.Color.White;
}
}
}
private void dataGridView1\u单元格格式(对象发送方,DataGridViewCellFormattingEventArgs e)
{
int colIndex=e.ColumnIndex;
int rowIndex=e.rowIndex;
如果(行索引>=0&&colIndex>=0)
{
DataGridViewRow theRow=dataGridView1.Rows[rowIndex];
int差=(int)theRow.Cells[5]值;
如果(差异可能重复“是”,该问题中标记为解决方案的答案正是我所要寻找的。我会尽快发布此问题的答案。如果有机会,我会添加一个更干净的方法。我在DataGridViewRow对象上没有可用的BeginEdit和EndEdit方法。但是在DataGri对象上我有虽然是dView对象,但在尝试之后,它不起作用。Nvm,我找到了解决方案。我会尽快发布它。很棒的解决方案。我非常喜欢数据绑定,而不是手动尝试构建表。
private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
int colIndex = e.ColumnIndex;
int rowIndex = e.RowIndex;
if (rowIndex >= 0 && colIndex >= 0)
{
DataGridViewRow theRow = dataGridView1.Rows[rowIndex];
int difference = (int)theRow.Cells[5].Value;
if (difference <= 0)
{
theRow.DefaultCellStyle.BackColor = System.Drawing.Color.Red;
theRow.DefaultCellStyle.ForeColor = System.Drawing.Color.White;
}
}
}