datagridview 中的某些列是只读的,但我需要更改单元格值

问题描述 投票:0回答:1

我有一个datagridview,它的数据源来自组合了三个不同表的数据表。 在此 datagridview 中,我将几列设置为 readonly = true; 如果我收到电报消息,我需要更新这些单元格的值(readonly = true) 有时,它会显示错误:NullReferenceException 我不确定这是因为我更新了只读或非只读的单元格值 以下是我的代码,请帮助我

private void UpdateTrialSeqValue(string strProduct, string strBuy, string strSell,  string strBuy1Qty, string strSell1Qty)
    {
        if (!this.IsDisposed && dgvWSettingAll != null)
        {
            if (this.InvokeRequired)
            {
                BeginInvoke(new MethodInvoker(() => UpdateTrialSeqValue(strProduct, strBuy, strSell, strBuy1Qty, strSell1Qty)));
                return;
            }

            lock (Update_Flag)
            {
                foreach (DataGridViewRow row in dgvSettingAll.Rows)
                {
                    if(row.Index >= 0)
                    {
                        string product = Convert.ToString(row.Cells["Colproduct"].Value);
                        if (product == strProduct)
                        {
                            row.Cells["ColBuyQty"].Value = strBuy1Qty;
                            row.Cells["ColBuyPrice"].Value = strBuy;
                            row.Cells["ColSellPrice"].Value = strSell;
                            row.Cells["ColSellQty"].Value = strSell1Qty;
                        }
                    }
                }
            }
        }
    }
c# winforms datagridview
1个回答
0
投票

您遇到的 NullReferenceException 可能是由于访问只读单元格造成的。为了避免这种异常,您可以在更新单元格的值之前检查单元格是否为只读:

private void UpdateTrialSeqValue(string strProduct, string strBuy, string 
strSell, string strBuy1Qty, string strSell1Qty)
{
if (!this.IsDisposed && dgvWSettingAll != null)
{
    if (this.InvokeRequired)
    {
        BeginInvoke(new MethodInvoker(() => UpdateTrialSeqValue(strProduct, 
strBuy, strSell, strBuy1Qty, strSell1Qty)));
        return;
    }

    lock (Update_Flag)
    {
        foreach (DataGridViewRow row in dgvSettingAll.Rows)
        {
            if (row.Index >= 0)
            {
                string product = 
 Convert.ToString(row.Cells["Colproduct"].Value);
                if (product == strProduct)
                {
                    DataGridViewCell buyQtyCell = row.Cells["ColBuyQty"];
                    DataGridViewCell buyPriceCell = row.Cells["ColBuyPrice"];
                    DataGridViewCell sellQtyCell = row.Cells["ColSellQty"];
                    DataGridViewCell sellPriceCell = 
row.Cells["ColSellPrice"];

                    if (!buyQtyCell.ReadOnly)
                    {
                        buyQtyCell.Value = strBuy1Qty;
                    }

                    if (!buyPriceCell.ReadOnly)
                    {
                        buyPriceCell.Value = strBuy;
                    }

                    if (!sellQtyCell.ReadOnly)
                    {
                        sellQtyCell.Value = strSell1Qty;
                    }

                    if (!sellPriceCell.ReadOnly)
                    {
                        sellPriceCell.Value = strSell;
                    }
                }
            }
        }
     }
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.