4

状況:

こんにちは!WPF ツールキットの DataGrid に MS Access データベースを設定しようとしています。

これが私が今持っているものです(動作します):

//Load the datagrid with the database
    private void LoadDataGrid(string filename, string path)
    {
        string databaseConn = "Provider=Microsoft.ACE.OLEDB.12.0;" +
                              "Data Source=" + path + "\\" + filename,
               tableName ="";
        OleDbConnection conn = null;
        DataTable schemaTable,
                  table = new DataTable();

        try
        {
            conn = new OleDbConnection(databaseConn);
            try
            {
                conn.Open();
                schemaTable = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables,
                                                       new object[] { null, null, null, "TABLE" });
                tableName = "[" + schemaTable.Rows[0].ItemArray[2].ToString() + "];";
                string sqlQuery = "SELECT * FROM " + tableName;
                OleDbCommand command = new OleDbCommand(sqlQuery, conn);
                OleDbDataReader reader;
                reader = command.ExecuteReader();
                table.Load(reader);
                DataGrid_.ItemsSource = table.DefaultView;
            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show(ex.Message); 
            }
            finally
            {
                conn.Close();
            }
        }
        catch (Exception ex)
        {
            System.Windows.MessageBox.Show(ex.Message); 
        }
    }

上記のコード サンプルは、MS Access データベースを利用して WPF ツールキットの DataGrid を読み込みます。

私がやりたいことは、最初に DataGrid に列を挿入できるようにすることです。この列は、行番号を書き込むために使用されます。私がうまくいくと思うのは、テーブル変数 ( DataTableオブジェクト) を変更することです。


質問:

では、テーブル変数に列を挿入し、その新しい列に各行の行番号を追加し、データベースからのすべてのデータを DataGrid に含めるにはどうすればよいでしょうか?

4

2 に答える 2

2

別の方法は、IDataReader をロードする前に DataTable に列を作成することです。


// the rest of your code
//
DataTable table = new DataTable();
DataColumn col = table.Columns.Add("RowNumber", typeof(int));
col.AutoIncrementSeed = 1;
col.AutoIncrement = true;

//
// the rest of your code
//

table.Load(reader)
//
// the rest of your code

以下のコード スニペットは、質問の文脈から外れた手法を示しています。


//Simulates data coming from a database or another data source
DataTable origin = new DataTable(); 
DataColumnCollection columns = origin.Columns; 
columns.Add("Id", typeof(int)); 
columns.Add("Name", typeof(string)); 
origin.Rows.Add(55, "Foo"); 
origin.Rows.Add(14, "Bar"); 
IDataReader reader = origin.CreateDataReader();

DataTable table = new DataTable(); 

//Sets up your target table to include a new column for displaying row numbers
//These are the three lines that make it all happen.
DataColumn col = table.Columns.Add("RowNumber", typeof(int)); 
col.AutoIncrementSeed = 1; 
col.AutoIncrement = true; 

//Simulates loading data from the database
table.Load(reader); 

// Examine table through the debugger. Is will have the contents of "origin" with the column "RowNumber" prepended
于 2009-08-16T15:27:20.480 に答える
2

最も簡単な解決策は、次のように、元の SELECT クエリに「仮想」RowNumber フィールドを含めるようにコードを変更することです。

SELECT ROWNUM AS ROWNUMBER, * FROM TABLE1

残念ながら、Access には ROWNUM 関数のようなものはありません。そのため、次のように SELECT クエリに RowNumber 列を追加するのが最も簡単な解決策だと思います。

SELECT 0 AS ROWNUMBER, * FROM TABLE1

最初にすべてゼロを含む列を追加し、結果の DataTable を反復処理して、次のように行番号を設定します。

int rownumber = 1;
foreach (DataRow row in table.Rows)
{
    row["ROWNUMBER"] = rownumber;
    rownumber++;
}

次に、DataTable をグリッドにダンプします。

于 2009-08-15T21:19:01.013 に答える