修改DataTable

时间:2009-08-15 20:24:20

标签: c# wpf datagrid datatable wpftoolkit

情况:

您好!我正在尝试使用MS Access数据库填充WPF工具包DataGrid。

这是我现在所拥有的(它有效):

//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 对象)。


问题:

那么,我如何在 table 变量中插入一个列,为该新列中的每一行添加行号,并在DataGrid中包含数据库中的所有数据?

2 个答案:

答案 0 :(得分: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转储到网格中。

答案 1 :(得分:2)

另一种方法是在将IDataReader加载到DataTable之前在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