2017-11-19 2 views
1

이 오류 메시지가 있습니다 : [Nom complet] 열을 찾을 수 없습니다.Datatable 열 이름을 확인하는 방법?

datagridviewbinded입니다. 여기에는 다른 테이블이있는 Access 데이터베이스가 있습니다. From Sauvegarde 테이블 일부 열을 선택했으며 [Nom complet] 열에서 combobox을 사용하여 검색 필터링을 수행하려고하지만이 메시지가 나타납니다. [Nom complet] 열을 찾을 수 없습니다. 어디에 문제가 있는지 알 수 없습니다. 다음 코드입니다.

DataTable table = new DataTable(); 
public recherche() 
{ 
    InitializeComponent(); 
} 
private void filldata() 
{ 
    OleDbConnection ccn = new   
    OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data 
Source='C:\mysource.accdb'"); 
    string qur = "Select [N], [Code machine], [Type programme], [Nom 
complet], [Motif], [Remarque] From [Sauvegarde]"; 
    OleDbCommand ccmd = new OleDbCommand(qur, ccn); 
    DataTable table = new DataTable(); 
    OleDbDataAdapter da = new OleDbDataAdapter(ccmd); 
    da.Fill(table); 
    dataGridView1.DataSource = table; 
} 
private void recherche_Load(object sender, EventArgs e) 
{ 
    filldata(); 
} 
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e) 
{ 

    DataView dv = new DataView(table); 

    if (comboBox2.SelectedItem.ToString()=="All") 
    { 
     dataGridView1.DataSource = table; 
    } 
    else 
    { 
     dv.RowFilter = string.Format("[Nom complet] LIKE '%{0}%'", 
comboBox2.SelectedItem.ToString()); 
     dataGridView1.DataSource = dv; 

    } 
} 

답변

0

이제는 더 명확합니다. 문제는 전역 변수 테이블을 초기화하지 않고 대신 로컬 변수를 생성한다는 사실 때문에 발생합니다. 그런 다음 전체를 필터링하려고하면 절대 열을 찾을 수 없습니다.

DataTable table = new DataTable(); 
.... 
private void filldata() 
{ 
    ..... 
    // This creates a local DataTable variable with the same name of the global one 
    // You are hiding the global and using the local one 
    // DataTable table = new DataTable(); 

    // here instead you use the global variable 
    // So you are referencing the same variable in the SelectedIndexChanged handler 
    table = new DataTable(); 
    .... 
}