DataGridView에 선택한 행이 표시되도록하려면 어떻게해야합니까?
DataGridView
선택한을 표시 하도록 강제해야합니다 row
.
요컨대, 에 입력 된 textbox
내용에 따라 DGV
선택 을 변경 하는 textbox
. 이 경우 선택 항목이 일치하는 row
.
불행히도 선택한 항목 row
이보기에서 벗어난 경우 수동으로 스크롤하여 선택 항목을 찾아야합니다. 누구든지 DGV
선택한 항목을 표시 하도록 강제하는 방법을 알고 row
있습니까?
감사!
다음을 설정할 수 있습니다.
dataGridView1.FirstDisplayedScrollingRowIndex = dataGridView1.SelectedRows[0].Index;
다음은 이 속성에 대한 MSDN 설명서 입니다.
이것은 맨 위에 놓지 않고 선택한 행으로 스크롤합니다.
dataGridView1.CurrentCell = dataGridView1.Rows[index].Cells[0];
이 코드도 고려하십시오 (competent_tech에서 제안한 방법 사용).
private static void EnsureVisibleRow(DataGridView view, int rowToShow)
{
if (rowToShow >= 0 && rowToShow < view.RowCount)
{
var countVisible = view.DisplayedRowCount(false);
var firstVisible = view.FirstDisplayedScrollingRowIndex;
if (rowToShow < firstVisible)
{
view.FirstDisplayedScrollingRowIndex = rowToShow;
}
else if (rowToShow >= firstVisible + countVisible)
{
view.FirstDisplayedScrollingRowIndex = rowToShow - countVisible + 1;
}
}
}
행을 선택한 후 해당 행을 넣으십시오.
dataGridView1.FirstDisplayedScrollingRowIndex = dataGridView1.SelectedRows[0].Index;
int rowIndex = -1;
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (row.Cells[0].Value.ToString().Equals(searchString))
{
rowIndex = row.Index;
break;
}
}
if (rowIndex >= 0)
{
dataGridView1.CurrentCell = dataGridView1[visibleColumnIndex, rowIndex];
}
visibleColumnIndex-선택한 셀이 표시되어야합니다.
DataGridView가 활성화되어 있지 않을 때 FirstDisplayedScrollingRowIndex 를 설정 하면 목록이 원하는 행으로 스크롤되지만 스크롤바는 해당 위치를 반영하지 않습니다. 가장 간단한 솔루션은 DGV를 다시 활성화 및 비활성화하는 것입니다.
dataGridView1.Enabled = true;
dataGridView1.FirstDisplayedScrollingRowIndex = index;
dataGridView1.Enabled = false;
// 작동하며 대소 문자를 구분하며 첫 번째 검색 항목을 찾습니다.
private bool FindInGrid(string search)
{
bool results = false;
foreach (DataGridViewRow row in dgvData.Rows)
{
if (row.DataBoundItem != null)
{
foreach (DataGridViewCell cell in row.Cells)
{
if (cell.Value.ToString().Contains(search))
{
dgvData.CurrentCell = cell;
dgvData.FirstDisplayedScrollingRowIndex = cell.RowIndex;
results = true;
break;
}
if (results == true)
break;
}
if (results == true)
break;
}
}
return results;
}
다음과 같이합니다.
dataGridView1.CurrentCell = dataGridView1.Rows[index].Cells[0];
첫 번째 열이 표시되는 경우에만 작동합니다. 숨겨져 있으면 예외가 발생합니다. 이것이 더 안전합니다.
var column = dataGridView1.CurrentCell != null ? dataGridView1.CurrentCell.ColumnIndex : dataGridView1.FirstDisplayedScrollingColumnIndex; dataGridView1.CurrentCell = dataGridView1.Rows[iNextHighlight].Cells[column];
대상 행이 이미 화면에있는 경우 스크롤하지 않고 선택을 재설정합니다. 또한 인라인 편집을 허용 한 경우 중요 할 수있는 현재 열 선택을 유지합니다.
나는 다음 검색 기능을 디스플레이에서 스크롤 선택을 위해 잘 작동하도록 만들었습니다.
private void btnSearch_Click(object sender, EventArgs e)
{
dataGridView1.ClearSelection();
string strSearch = txtSearch.Text.ToUpper();
int iIndex = -1;
int iFirstFoundRow = -1;
bool bFound = false;
if (strSearch != "")
{
dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
/* Select All Rows Starting With The Search string in row.cells[1] =
second column. The search string can be 1 letter till a complete line
If The dataGridView MultiSelect is set to true this will highlight
all found rows. If The dataGridView MultiSelect is set to false only
the last found row will be highlighted. Or if you jump out of the
foreach loop the first found row will be highlighted.*/
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if ((row.Cells[1].Value.ToString().ToUpper()).IndexOf(strSearch) == 0)
{
iIndex = row.Index;
if(iFirstFoundRow == -1) // First row index saved in iFirstFoundRow
{
iFirstFoundRow = iIndex;
}
dataGridView1.Rows[iIndex].Selected = true; // Found row is selected
bFound = true; // This is needed to scroll de found rows in display
// break; //uncomment this if you only want the first found row.
}
}
if (bFound == false)
{
dataGridView1.ClearSelection(); // Nothing found clear all Highlights.
}
else
{
// Scroll found rows in display
dataGridView1.FirstDisplayedScrollingRowIndex = iFirstFoundRow;
}
}
}
참고URL : https://stackoverflow.com/questions/8437916/how-do-i-make-the-datagridview-show-the-selected-row
'Development Tip' 카테고리의 다른 글
백본에서 상위 클래스에 액세스 (0) | 2020.11.09 |
---|---|
R dplyr : 여러 열 삭제 (0) | 2020.11.09 |
.translate ()를 사용하여 Python 3.x의 문자열에서 구두점을 제거하는 방법은 무엇입니까? (0) | 2020.11.09 |
Python 문자열에서 허용 목록에없는 HTML 태그 제거 (0) | 2020.11.09 |
Oracle의 기본 날짜 형식은 YYYY-MM-DD입니다. 이유는 무엇입니까? (0) | 2020.11.09 |