2012-05-02 11 views
0

我有一個datagridview顯示我的數據在列中。 我想要完成的是,在選擇一行並按下編輯按鈕後,將打開一個新窗體並將正確文本框的行分開以更新數據。獲取datagridview行數據到另一個表單上的文本框

DataGridView的行顯示不同類型的數據:姓名,電子郵件,日期等...

什麼想法? 在此先感謝!

+0

你的問題到底是什麼?你有沒有嘗試過一些東西? – asdasdad

回答

2

本網站解釋瞭如何在表單之間發送數據,這將如同在數據網格中選擇正確的單元格一樣簡單,將這些信息發送到正確的文本框中。然後發回他們。 Data between forms

的基礎是創建一個可以利用得到的值的方法,

public string getTextBoxValue() 
{ 
    return TextBox.Text; 
} 

那麼你可以調用該方法的形式之間傳遞數據,

this.Text = myForm2.getTextBoxValue(); 

然而,你將發送單元格的值,並將使textbox.text等於方法的返回 這是理論的基本示例,它試圖讓它適用於你想要的東西要做,如果你不能這樣做就會回來並尋求幫助和錯誤的代碼編輯,但只有在你先試圖自己之後

+0

@sasib想要從數據網格視圖中獲取數據並將其分配給相應的文本框,您正在返回文本框數據 – Sadaf

+0

我還解釋了它的唯一理論如何做到這一點,並且他應該自己用數據網格來嘗試它,男人去釣魚等等 – RhysW

2

你可以創建一個類,比如MyDataCollection,其屬性對應於你的DataGridView列。當你按下編輯按鈕時,創建這個類的一個新實例,填充必要的數據並將它作爲參數傳遞給EditForm的構造函數。

public class MyDataCollection 
{ 
    public string Name; 
    public string Email; 
    // -- 
} 

在您的主要形式有:

void btnEdit_Click(object sender, EventArgs e) 
{ 
    // Create the MyDataCollection instance and fill it with data from the DataGridView 
    MyDataCollection myData = new MyDataCollection(); 
    myData.Name = myDataGridView.CurrentRow.Cells["Name"].Value.ToString(); 
    myData.Email = myDataGridView.CurrentRow.Cells["Email"].Value.ToString(); 
    // -- 

    // Send the MyDataCollection instance to the EditForm 
    formEdit = new formEdit(myData); 
    formEdit.ShowDialog(this); 
} 

和編輯表單應該是這樣的:

public partial class formEdit : Form 
{ 
    // Define a MyDataCollection object to work with in **this** form 
    MyDataCollection myData; 

    public formEdit(MyDataCollection mdc) 
    { 
     InitializeComponent(); 

     // Get the MyDataCollection instance sent as parameter 
     myData = mdc; 
    } 

    private void formEdit_Load(object sender, EventArgs e) 
    { 
     // and use it to show the data 
     textbox1.Text = myData.Name; 
     textbox2.Text = myData.Email; 
     // -- 
    } 
} 

您也可以忘記MyDataCollection類和整個的DataGridViewRow傳遞到formEdit的構造函數。

相關問題