繁体   English   中英

在设置datagridview的数据源之后添加单元格值

[英]Add cell value after set datasource of datagridview

DataGridview从数据库获得DataSource之后,我尝试以两种方式添加TaxDiscount列:设计和代码。 但是当我运行这段代码时:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM