简体   繁体   中英

c# datagridview does not refresh after passing parameters

I am passing parameters from Form 1:Products into Form2:ProductEdit. After edit the parameters in ProductEdit, datagridview in Products does not refresh.

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;
    }  

There are 2 approaches to fixing this.

  1. Instead of passing in strings, pass in the cells. Bind the textboxes to the cells. Then the edits made will show up in the original form as you are directly modifiying it.

  2. When product edit is closing, write the values in the textboxes back to dataGridView.SelectedRows[0].Cells(xxxx)


An example of approach 1: ( untested)

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");
}  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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