2013-02-02 2 views
-3

검색 버튼을 눌렀을 때 텍스트 상자에서 문자열을 가져오고 해당 데이터 격자가 강조 표시된 행을 원합니다. 나는 이것이 가능하다고 믿는다. 어떻게 할 수 있는지 모르겠습니다. 제발 도와주세요.데이터 그리드를 사용하여 검색하는 방법

string query = " SELECT * FROM suboffice where so_id like '%" + sor_id.Text + "%' "; 
      SqlConnection objConn = new SqlConnection(connectionString); 
      objConn.Open(); 
      SqlDataAdapter subofficeTableAdapter1 =new SqlDataAdapter(query,objConn); 
      SqlCommandBuilder cBuilder = new SqlCommandBuilder(subofficeTableAdapter1); 
      DataTable dTable = new DataTable(); 
      subofficeTableAdapter1.Fill(dTable); 
      dataGridView1.DataSource = dTable; 
      subofficeTableAdapter1.Update(dTable); 

여기서 sor는 검색 할 때마다 탭에 넣을 때 데이터 그리드보기가 업데이트됩니다. 이 프로그램은 C 언어로 작성됩니다 #

+2

(http://mattgemmell.com/2008/12/08/what-have-you-tried/) –

+0

이 시도 : 1) 구글 2) Stackoverflow search 3) 아무 것도주지 않으면 코드로 질문을 게시하십시오 ... http://stackoverflow.com/questions/6210781/search-in-datagridview-c-sharp-winfom http : // stackoverflow. co.kr/questions/6989242/일치하는 또는 일치하지 않는 부분에 대한 데이터 검색 http://stackoverflow.com/questions/797946/search-datagridview-on-user-keypress –

+0

어떤 유형의 응용 프로그램입니까? ? winforms? wpf? asp.net? – Tomtom

답변

0

Windows Forms를 사용하는 경우 여기를 클릭하십시오. "다음 찾기", "이전 찾기", "모두 찾기"등과 같은 일종의 기능을 구현하려는 경우 용도에 맞게 코드를 수정해야 할 수 있습니다.

다음은 SearchButton이라는 단추, SearchTextBox라는 텍스트 상자 및 MyDataGridView라는 DataGridView 다음 코드는 검색 버튼의 클릭 이벤트에 포함됩니다. [? 당신이 시도 무엇``]

private void SearchButton_Click(object sender, EventArgs e) 
    { 
     MyDataGridView.ClearSelection(); //this will clear any currently selected cells 
     string searchstring = SearchTextBox.Text; 
     foreach (DataGridViewRow r in MyDataGridView.Rows) 
      foreach (DataGridViewCell c in r.Cells) 
      if (c.Value.ToString().Contains(searchstring)) 
      { 
       r.Selected = true; //this will highlight the entire row 
       break; //if you want to "Select All" that are found, take this line out 
      } 
    } 
+0

나는 작동하지 않는 ur 코드를 시도했다. "개체 참조가 개체의 인스턴스로 설정되지 않았습니다." if line (c.Value.ToString(). Contains (searchstring)) –

+0

이제이 prob를 어떻게 수정합니까? –

+0

어떤 객체가 null을 반환합니까? 그것은 c.value 또는 searchstring입니까? 어쩌면 그 중 하나가 해당 줄 바로 앞에 null인지 확인해야합니다. – Mash

관련 문제