【问题标题】:c# datagridview does not refresh after passing parametersc#datagridview传参后不刷新
【发布时间】:2016-12-23 13:55:31
【问题描述】:

我将参数从 Form 1:Products 传递到 Form2:ProductEdit。在ProductEdit中编辑参数后,Products中的datagridview没有刷新。

private void dataGridView_CellClick(object sender, DataGridViewCellEventArgs e)
    {
        if (e.ColumnIndex == 0 && e.RowIndex >= 0)
        {
            ProductEdit pe = new ProductEdit(dataGridView.SelectedRows[0].Cells[1].Value.ToString(), dataGridView.SelectedRows[0].Cells[2].Value.ToString(), dataGridView.SelectedRows[0].Cells[3].Value.ToString());
            pe.Show(dataGridView);
        }
    }    

public ProductEdit(string PId, string PName, string PPrice)
    {
        InitializeComponent();
        txtPId.Text = PId;
        txtPName.Text = PName;
        txtPPrice.Text = PPrice;
    }  

【问题讨论】:

    标签: c# datagridview refresh


    【解决方案1】:

    有两种方法可以解决此问题。

    1. 不要传入字符串,而是传入单元格。将文本框绑定到单元格。然后,所做的编辑将在您直接修改时以原始形式显示。

    2. 当产品编辑关闭时,将文本框中的值写回dataGridView.SelectedRows[0].Cells(xxxx)


    方法1的一个例子:(未经测试)

    ProductEdit pe = new ProductEdit(dataGridView.SelectedRows[0].Cells[1], dataGridView.SelectedRows[0].Cells[2], dataGridView.SelectedRows[0].Cells[3]);
    
    public ProductEdit(DataGridViewCell PId, DataGridViewCell PName, DataGridViewCell PPrice)
    {
        InitializeComponent();
        txtPId.DataBindings.Add("Text", PId, "Value");
        txtPName.DataBindings.Add("Text", PName, "Value");
        txtPPrice.DataBindings.Add("Text", PPrice, "Value");
    }  
    

    【讨论】:

    • 你能解释一下吗
    • 您的产品编辑表单与您的数据网格视图完全断开,因为您所做的只是将每个单元格的字符串内容的副本传递到表单中。因此,完成编辑后,您需要将编辑后的值写回数据网格。为此,您需要参考数据需要去哪里,因此我建议将 DataGridViewCells 添加到您的构造函数中。
    猜你喜欢
    • 2013-09-04
    • 2015-07-12
    • 1970-01-01
    • 1970-01-01
    • 2010-12-06
    • 1970-01-01
    • 1970-01-01
    • 2016-10-08
    • 2012-04-25
    相关资源
    最近更新 更多