如題,因為某些需求,DataGridView在輸入一次內容,就要呼叫來將內容提交,但是這樣做之后,控制元件就會當你輸入的內容都完成了,所以將內容全選。這就使得無法繼續輸入,如何避免全選中呢?
陳錦巍
uj5u.com熱心網友回復:
Hi 陳錦巍,我還是沒能重現你的問題,但正如你所說的,你可以嘗試使用代碼來重定位游標的位置。你可以參考以下代碼:
dataGridView1.CurrentCell = dataGridView1.Rows[0].Cells[0];
dataGridView1.BeginEdit(false);
TextBox tb = (TextBox)dataGridView1.EditingControl;
tb.SelectionStart = 0;
Regards,
Kyle
uj5u.com熱心網友回復:
Hi 陳錦巍,我訂閱了 CurrentCellDirtyStateChanged 事件,并在事件中執行如下代碼來代替“內容提交”。
private void dataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
Console.WriteLine(dataGridView1.CurrentCell.Value);
}
執行結果如下:
并沒有出現你所說的內容全選的情況,你能提供一個具體的可以重現你遇到的問題的代碼嗎?
Regards,
Kyle
uj5u.com熱心網友回復:
我是在CurrentCellDirtyStateChanged里隨時提交資料的,這樣提交后單元格就會被全選,因全選了,無法直接再輸入,只能用滑鼠重新定位輸入點到末尾。我是想問,如何改變這種提交即全選極致。private void dataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
if (this.IsCurrentCellDirty)
{
this.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
難道只能在執行完提交后,用代碼重設輸入點到末尾嗎?
陳錦巍
uj5u.com熱心網友回復:
Hi 陳錦巍,我還是沒能重現你的問題,但正如你所說的,你可以嘗試使用代碼來重定位游標的位置。你可以參考以下代碼:
dataGridView1.CurrentCell = dataGridView1.Rows[0].Cells[0];
dataGridView1.BeginEdit(false);
TextBox tb = (TextBox)dataGridView1.EditingControl;
tb.SelectionStart = 0;
Regards,
Kyle
uj5u.com熱心網友回復:
好的,謝謝了!陳錦巍uj5u.com熱心網友回復:
我就想問datagridview怎樣系結private void dataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)這個事件。轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/257235.html
標籤:其他技術討論專區
上一篇:qtableview使用removeRow洗掉剩下的最后一行時,程式崩潰
下一篇:visio立方體等分線怎么畫
