DataGridView 특정 셀 강제 포커스 하기

2020. 5. 8. 11:38C#

728x90
반응형

 

DataGridVIew - Focus a specific cell 

 


<C#>

Private void AfterEnterName(object sender, DataGridViewCellEventargs e)
{
    // 미리 DataTable에 담음
    // Binding 되어 있는 DataGridView의 값을 수정 할 때 사용 
    DataTable dt_compare = (DataTable)DataGridView1.DataSource;

    int rowIndex = 0;

    DataGridView1.CellValueChanged -= AfterEnterName;

    rowIndex = DataGridView1.CurrentCell.RowIndex;
    
    DataGridView1.DataSource = dt_compare;
    DataGridView1.CurrentCell = DataGridView1.Rows[rowIndex].Cells["Tester"];


    DataGridView1.CellValueChanged += AfterEnterName;
}







<VB.NET>

Private Sub AfterEnterName(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellValueChanged
        Dim dt_compare As DataTable = TryCast(DataGridView1.DataSource, DataTable)

        RemoveHandler DataGridView1.CellValueChanged, AddressOf AfterEnterName

        dt_compare.Rows(.RowIndex)("Column1") = "Test"

        rowIndex = DataGridView1.CurrentCell.RowIndex   


        DataGridView1.DataSource = dt_compare

        ' 포커스 조정
        DataGridView1.CurrentCell = DataGridView1.Rows(rowIndex).Cells("Tester")

        AddHandler DataGridView1.CellValueChanged, AddressOf AfterEnterName

End Sub
















 

728x90
반응형