2015-10-01 60 views
1

我遇到問題,試圖找出如何更新C#中的datagridview。 我有兩種形式(Form1:使用datagridview/Form2:使用文本框和「保存」按鈕。)從另一個表單(C#)更新datagridview數據

我用datacridview上的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地址點擊「保存」後,我在第二個表單中更改的地址?

感謝您的回答。

回答

1

如果datagridview從數據庫表中填充,那麼當您單擊save時,必須更新表並將其綁定到datagridview一次。

+0

謝謝...我在此期間找到了一個工作解決方案... –

相關問題