2012-01-05 211 views
1

這是我之前的一個跟進問題。你可以找到它here如何自動結束DataGridView單元格編輯模式?

我終於解決了之前的一個問題,當我將按鈕添加到DataGrid的新行中時,整個單元格將處於編輯模式,直到我單擊其他單元格並填充它, /或選項卡直到行的末尾(很明顯,這個不起作用),那麼它將結束編輯模式。 我使用dataGridView.BeginEdit(true);開始編輯模式,以便我可以解析文本框中的值(請參閱我的上一個問題)。因此,如果我插入另一個值並按下按鈕,新值將替換先前插入的舊值,因爲它目前仍處於編輯模式。 我試圖使用,dataGridView.EndEdit(DataGridViewDataErrorContexts.Commit);,cell.DataGridView.EndEdit()cell.DataGridView.EndEdit(DataGridViewDataErrorContexts.Commit);但顯然不會結束編輯模式:( 我想要的是當我按下按鈕時,文本框內的值將被插入到第一個文本框列(這個已經工作了),然後我不必點擊或填充另一列來結束編輯模式,所以我只需要在文本框中輸入任何內容,然後按下按鈕直到我想停止。開始填充另一列 沒有人知道如何解決這個 sample image 編輯1:??你看到的差異看紅色圓圈,上面一個是目前在編輯模式下(因爲它有後*箭頭),最下面的一個不在編輯模式(我是通過選擇手動完成的)從組合框中的項目)。 這裏是從我剛纔的問題請我的代碼:

private void button1_Click(object sender, EventArgs e) 
    { 
     this.surat_jalanDataGridView.AllowUserToAddRows = true; 
     string tokNum = this.textBox1.Text; 

     if (this.textBox1.Text != "") 
     { 
      foreach (DataGridViewRow sjRow in this.surat_jalanDataGridView.Rows) 
      { 
       int RowIndex = surat_jalanDataGridView.RowCount - 1; 
       DataGridViewRow R = surat_jalanDataGridView.Rows[RowIndex]; 

       DataTable table = new DataTable(); 
       DataRow newRow = table.NewRow(); 
       table.Rows.Add(newRow); 

       DataGridViewCell cell = R.Cells[2]; 
       this.surat_jalanDataGridView.CurrentCell = cell; 
       this.surat_jalanDataGridView.BeginEdit(true); 

       R.Cells[2].Value = tokNum; 
       cell.DataGridView.EndEdit(DataGridViewDataErrorContexts.Commit); 
      } 
     } 
     this.surat_jalanDataGridView.EndEdit(DataGridViewDataErrorContexts.Commit); 
    } 

編輯2:所以,我將它從數據源的surat_jalan放到我的Windows窗體。然後它自動成爲與屬性名surat_jalanDataGridView和數據源的數據網格是surat_jalanBindingSource img 2

+0

你也可以在你的winform上使用'this.Validate' – 2012-01-05 03:18:29

+0

我已經試過了Gabriel,但那也行不通。但是,無論如何感謝:D – 2012-01-05 03:41:24

+0

爲什麼你在開始時總是要求結束?您無需進入編輯模式即可設置您的值。編輯模式是讓用戶輸入一個輸入。 – 2012-01-05 04:18:47

回答

1

很抱歉的延遲。在看到如何設置綁定到DataGridView後,我可以明確地向您提供有關如何編輯網格綁定數據的更好指導。當您從Visual Studio中的數據源視圖拖動表格並將其放到DataGridView上時,Visual Studio會爲您做好幾件事情。重要的是,您至少要了解所做的基礎知識,以便了解如何操縱數據向前發展。 This MSDN文章如何設置從Visual Studio窗體控件的綁定。最後一節描述你在做什麼。最後的句子「DataGridView控件現在綁定到已拖動到其上的表格,DataSet,TableAdapter和BindingSource出現在組件托盤中。」是重要的。由於Visual Studio生成將控件綁定到表的代碼,因此應該直接編輯數據以更新數據網格視圖。在這種情況下,你應該使用生成的DataSet(我假設它被命名爲surat_jalanDataSet)。 Here是如何編輯數據集中的數據的說明。爲您的具體情況Adding Rows。請讓我知道這是否有助於您實現目標。

相關問題