這是我之前的一個跟進問題。你可以找到它here。如何自動結束DataGridView單元格編輯模式?
我終於解決了之前的一個問題,當我將按鈕添加到DataGrid的新行中時,整個單元格將處於編輯模式,直到我單擊其他單元格並填充它, /或選項卡直到行的末尾(很明顯,這個不起作用),那麼它將結束編輯模式。 我使用dataGridView.BeginEdit(true);
開始編輯模式,以便我可以解析文本框中的值(請參閱我的上一個問題)。因此,如果我插入另一個值並按下按鈕,新值將替換先前插入的舊值,因爲它目前仍處於編輯模式。 我試圖使用,dataGridView.EndEdit(DataGridViewDataErrorContexts.Commit);
,cell.DataGridView.EndEdit()
和cell.DataGridView.EndEdit(DataGridViewDataErrorContexts.Commit);
但顯然不會結束編輯模式:( 我想要的是當我按下按鈕時,文本框內的值將被插入到第一個文本框列(這個已經工作了),然後我不必點擊或填充另一列來結束編輯模式,所以我只需要在文本框中輸入任何內容,然後按下按鈕直到我想停止。開始填充另一列 沒有人知道如何解決這個 編輯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
你也可以在你的winform上使用'this.Validate' – 2012-01-05 03:18:29
我已經試過了Gabriel,但那也行不通。但是,無論如何感謝:D – 2012-01-05 03:41:24
爲什麼你在開始時總是要求結束?您無需進入編輯模式即可設置您的值。編輯模式是讓用戶輸入一個輸入。 – 2012-01-05 04:18:47