2016-05-08 10 views
3

DataGridViewコントロールがDataTableからデータを取得するようにしようとしています.DataTableコントロール自体は、Oracleデータベース上のSQLクエリの結果で満たされます。これは、ListBoxコントロールからテーブル名をダブルクリックした後に発生します。DataSourceを変更した後、DataGridViewはソート後にのみ行を表示するのはなぜですか?

これは最初にテーブル名をダブルクリックすると正常に機能しますが、2回目には列名と行が表示されません。

私が発見したことは、DataGridViewを任意の列に基づいて並べ替えると、突然表示される行が発生し、グラフィック/レンダリングの不具合が疑われることになります。

これが期待どおりの動作ですか?

// event handler for ListBox 
private void tables_MouseDoubleClick(object sender, MouseEventArgs e)  
{ 
    // tables is a ListBox populated with table names 
    int index = this.tables.IndexFromPoint(e.Location);  
    String table_name; 
    if (index != System.Windows.Forms.ListBox.NoMatches) 
    { 
     // reusing the DataTable 
     dt.Reset();  
     table_name =tables.Items[index].ToString(); 
     OracleCommand cmd = new OracleCommand(); 
     cmd.Connection = con; 
     // case sensitive 
     cmd.CommandText = String.Format(@"select * from ""{0}""",table_name); 
     cmd.CommandType = CommandType.Text; 
     OracleDataAdapter da = new OracleDataAdapter(cmd); 
     da.MissingSchemaAction = MissingSchemaAction.AddWithKey; 
     da.Fill(dt); 
     dt.TableName = table_name; 
     label7.Text = table_name; 
     // these three lines don't actually accomplish anything in this case 
     table.DataSource = null;  
     table.Rows.Clear(); 
     table.DataSource = dt; 

     // uncommenting these lines produces the expected behavior 
     //DataGridViewColumn c = table.Columns[0];  
     //table.Sort(c,System.ComponentModel.ListSortDirection.Descending); 
    } 
} 

After second double-click(without sorting)

After second double-click(with sorting)

+0

好奇心:もっと簡単なバージョンにしてはどうでしょうか? 'table_name = tables.SelectedItem.ToString();' ListBoxを無効にしましたか?あなたは 'table.Refresh()'を試しましたか? – TaW

+0

@TaW提案していただきありがとうございます。私はRefresh()、Update()などのメソッドを試しましたが、ソートは動作する唯一のものと思われます。また、ListBoxは常に有効になっているため、後続の表を選択できます。 –

+0

ええ、これは奇妙に聞こえます。私は確かに何か他のことが起こっているはずです..あなたが設定したDGVの他のプロパティ? VitualMode? – TaW

答えて

1

問題がDataTable.Reset呼び出しによって引き起こされているように見えます。

それはあなたのコードの問題(DataTableまたはDataViewの中で最も可能性の高いバグ)ではないですが、私が代わりにResetClear方法を使用するか、新しいDataTableインスタンスを作成することをお勧め。

+0

はいdt.Reset()の代わりに 'dt = new DataTable();トリックをしました。ありがとうございました。 –

関連する問題