簡體   English   中英

使用其他表單(C#)中的數據更新datagridview

[英]Update datagridview with data from another form (C#)

我在嘗試找出如何在C#中更新datagridview時遇到問題。 我有兩種形式(Form1:使用datagridview / Form2:使用文本框和“保存”按鈕。)

我使用datagridview上的doubleclick函數打開Form2,並顯示所選行的詳細信息。

    private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
    {
        if (dataGridView1.CurrentRow != null)
        {
            WCustomer row = dataGridView1.CurrentRow.DataBoundItem as WCustomer;

            CustomerDetail c1 = new CustomerDetail(_Proxy, row.CustomerID);
            c1.CompanyName = dataGridView1.CurrentRow.Cells[1].Value.ToString();
            c1.ContactName = dataGridView1.CurrentRow.Cells[2].Value.ToString();
            c1.ContactTitle = dataGridView1.CurrentRow.Cells[3].Value.ToString();
            c1.Address = dataGridView1.CurrentRow.Cells[4].Value.ToString();
            c1.City = dataGridView1.CurrentRow.Cells[5].Value.ToString();
            c1.Region = dataGridView1.CurrentRow.Cells[6].Value.ToString();
            c1.PostalCode = dataGridView1.CurrentRow.Cells[7].Value.ToString();
            c1.Country = dataGridView1.CurrentRow.Cells[8].Value.ToString();
            c1.Phone = dataGridView1.CurrentRow.Cells[9].Value.ToString();
            c1.Fax = dataGridView1.CurrentRow.Cells[10].Value.ToString();
            c1.passDgvValueToCustomerDetail();
            c1.Show();
        }
        else
        {
            MessageBox.Show("No selected row!");
        }
    }

要將數據從gridview導入到第二種形式的文本框中,我使用了以下代碼:

    public void passDgvValueToCustomerDetail()
    {
        txtCompanyName.Text = CompanyName;
        txtContactName.Text = ContactName;
        txtContactTitle.Text = ContactTitle;
        txtAddress.Text = Address;
        txtCity.Text = City;
        txtRegion.Text = Region;
        txtPostalCode.Text = PostalCode;
        txtCountry.Text = Country;
        txtPhone.Text = Phone;
        txtFax.Text = Fax;
    }

現在,如何通過單擊“保存”后用在第二個表單中更改的地址替換它來更新datagridview中的地址?

已經感謝您的回答。

如果從數據庫表填充了datagridview,那么您必須更新該表並將其綁定到datagridview一次,再單擊“保存”。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM