2011-07-30 82 views
0

IM發送從一種形式值以另一種形式,然後要在DGV顯示,添加行編程到無限的datagridview

即時試圖這一點,有在執行過程中沒有錯誤,BT它並不顯示在DGV數據.. PLZ HEPL我

 lineItemsDGV.Rows.Add(); 

      int RowIndex = lineItemsDGV.RowCount - 1; 
      DataGridViewRow NewRow =lineItemsDGV.Rows[RowIndex]; 
      NewRow.Cells[0].Value = item.Product_id; 
      NewRow.Cells[1].Value = item.product_name; 
      NewRow.Cells[2].Value = item.specification; 
     NewRow.Cells[3].Value = item.unit_price; 
      NewRow.Cells[4].Value = item.unit_price * 1; 

問候

回答

1

這是我會怎麼做:

DataRow rowToAdd= myTable.NewRow(); 

rowToAdd["ProductId"] = item.Product_id; 
rowToAdd["ProductName"] = item.product_name; 
rowToAdd["Specification"] = item.specification; 
rowToAdd["UnitPrice"] = item.unit_price; 

myTable.Add(rowToAdd); 

並將數據表綁定到gridview。

3

你很近。

你可以做的是使用你的調用返回值DataGridViewRowCollection.Rows.Add()方法,這是剛剛添加的行的索引值。

你的代碼改成這樣:

int RowIndex = lineItemsDGV.Rows.Add(); 
DataGridViewRow NewRow = lineItemsDGV.Rows[RowIndex]; 
NewRow.Cells[0].Value = item.Product_id; 
NewRow.Cells[1].Value = item.product_name; 
NewRow.Cells[2].Value = item.specification; 
NewRow.Cells[3].Value = item.unit_price; 
NewRow.Cells[4].Value = item.unit_price * 1;