2016-06-30 3 views
-1

그리드의 모든 열을 검색해야하지만 DataGridView 셀에 검색 텍스트 상자를 적용해야하는 프로그램을 만들고 있습니다. 여기서 의미하는 것은 DataGridView이며 그 아래에있는 데이터에서 검색을 적용하는 검색 행으로 맨 위의 행이 있습니다. 아래 예와 같이DataGridView에서 사용자 지정 검색

:

enter image description here

이 DataGridView에 그 아래의 값을 검색 적용되는 첫 번째 행의 셀 검색을 갖는다.

+0

대상 : Winforms, WPF, ASP ..? __Always__ 질문에 올바르게 태그를 답니다. – TaW

+1

winforms. 앞으로도이 사실을 명심하십시오. –

+0

안녕하세요 @OmerWaheed, 스택 오버플로에 오신 것을 환영합니다. 추천 태그를 추가하려면 수정을 클릭하고 새 요약 태그를 수정 요약 및 저장 버튼 위에 추가하십시오. – ardila

답변

0

이 작업은 수행 할 수 있으며 아래에서 볼 수 있듯이 실제로 필요한 코드는 많지 않습니다.

enter image description here

그것은 당신이 AllowUserToAddRows 세트와 DataTable에 결합 DataGridView 있다고 가정 :하지만 당신은 당신이 연구 할 수 있습니다 그래서 여기 예입니다, 몇 트릭를 사용해야합니까 그릇된.

당신이 원하는 첫번째 것은 편집 제어을 잡아입니다 일반적으로 TextBox 사용자가 편집을 시작할 때 다음 TextChanged에서

private void dataGridView1_EditingControlShowing(object sender, 
          DataGridViewEditingControlShowingEventArgs e) 
{ 
    // unhook the old handle 
    if (editDgvEc != null) editDgvEc.TextChanged -= editDgvEc_TextChanged; 
    // store a reference 
    editDgvEc = e.Control as DataGridViewTextBoxEditingControl; 
    // hook up the TextChanged event 
    editDgvEc.TextChanged += editDgvEc_TextChanged; 
} 

: 우리는이 이벤트를 잡아

DataGridViewTextBoxEditingControl editDgvEc = null; // at class level! 

이벤트 몇 가지 테스트를 수행 한 다음 모든 피팅 행을 선택하십시오.

void editDgvEc_TextChanged(object sender, EventArgs e) 
{ 
    if (dataGridView1.CurrentCell.RowIndex == 0) 
    { 
     int col = dataGridView1.CurrentCell.ColumnIndex; 
     if (editDgvEc.Text == "") dataGridView1.ClearSelection(); 
     else 
      foreach (DataGridViewRow row in dataGridView1.Rows) 
      { 
       if (row.Index > 0) row.Selected = 
        row.Cells[col].Value.ToString().Contains(editDgvEc.Text); 
      } 
    } 
} 

입력하는 동안 직장에서 선택 과정을 볼 수 있습니다. 행이 많으면 편집 텍스트 상자가 아닌 CellEndEdit 이벤트의 검색 텍스트를 가져 와서 검색 텍스트를 변경하여 변경할 수 있습니다.

마지막으로 검색 행을 설정하려고합니다. . 우리는 DataTable dt 후에 작성되는 것을 할 필요가 : 당신의 DGV 코드를 데이터 바인딩되지 않은 경우

private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) 
{ 
    if (e.ColumnIndex >= 0 || e.RowIndex != 0) return; 
    e.Graphics.DrawString("$", new Font("Wingdings",11f), Brushes.Black, e.CellBounds); 
    e.Handled = true; 
} 

: 훨씬 더 좋은 터치에 대한

dataGridView1.DataSource = dt; 
DataRow dr = dt.NewRow(); 
dt.Rows.InsertAt(dr, 0); 
dataGridView1.Rows[0].Frozen = true; 
dataGridView1.Rows[0].DividerHeight = 2; 
dataGridView1.Rows[0].DefaultCellStyle.BackColor = Color.LightBlue; 

우리는 사용자 정의 RowHeader을 그립니다 CellPainting 이벤트를 코딩 할 수 있습니다 검색 행을 데이터 소스에 추가하는 대신 DGV에 직접 추가하여 간단하게 적용 할 수 있습니다.

+0

THANKYOU 너무 좋아 !!!! ... 이것이 내가 필요한 것입니다. 검증 됨. 고맙습니다 –

관련 문제