[英]Add cell value after set datasource of datagridview
在DataGridview
從數據庫獲得DataSource
之后,我嘗試以兩種方式添加Tax
和Discount
列:設計和代碼。 但是當我運行這段代碼時:
public void LoadGridView()
{
GrdReceiptDetail.AutoGenerateColumns = false;
GrdReceiptDetail.DataSource = SentoDB.usp_viewReceiptDetail(ReceiptId);
foreach (DataGridViewColumn column in GrdReceiptDetail.Columns)
{
if (column.Name != "Check" && column.Name != "Tax" && column.Name != "Discount")
{
GrdReceiptDetail.Columns[column.Index].ReadOnly = true;
}
}
foreach (DataGridViewRow row in GrdReceiptDetail.Rows)
{
if (row.Index != 0)
{
GrdReceiptDetail.Rows[row.Index].Cells["Discount"].Value = "10";
GrdReceiptDetail.Rows[row.Index].Cells["Tax"].Value = "10";
}
}
}
加載表單后,“ Discount
和“ Tax
”兩列的單元格中的值為空白。 我該如何解決? 我認為當我使用DataSource
,可能是DataGridview
阻止我添加列和值。
嘗試這個..
if (row.Index != 0)
{
//replace RowObject with the name of your underlying class that constitutes each
//row
RowObject obj = (RowObject)row.DataBoundItem;
//replace DiscountProperty and TaxProperty with the names of the properties on
//your RowObject that correspond to these values
obj.DiscountProperty = "10";
obj.TaxProperty = "10";
}
希望這對您有用。 這是一個類似問題的鏈接,詳細說明了為什么在使用基礎DataSource修改DataGridView時為什么要以這種方式更新數據。 在數據網格視圖中以編程方式設置單元格值
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.