修改数据表
情况:
您好! 我正在尝试使用 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 中插入一列。 该列将用于写入行号。 我认为可行的是修改 table 变量(这是一个 DataTable 对象)。
问题:
那么,如何在 table 变量中插入一列,为该新列中的每行添加行号,并将数据库中的所有数据存储在 DataGrid 中?
Situation:
Hello! I am trying to populate a WPF toolkit DataGrid with a MS Access database.
Here is what I have right now (it works):
//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);
}
}
The code sample above loads a WPF toolkit DataGrid with the help of a MS Access database.
What I would like to do is be able to insert a column in the DataGrid at the very beginning. This column would be used to write the row number. What I think could work is to modify the table variable (which is a DataTable object).
Question:
So, how can I insert a column in the table variable, add the row number for each rows in that new column, and have all the data from the database in the DataGrid?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
另一种方法是在将 IDataReader 加载到 DataTable 中之前先在 DataTable 上创建一个列。
下面的代码片段演示了问题上下文之外的技术
An alternative is to create a column on the DataTable before loading the IDataReader into it.
The code snippet bellow demonstrates the technique out of the question's context
最简单的解决方案是修改代码以在原始 SELECT 查询中包含“虚拟”RowNumber 字段,如下所示:
不幸的是,Access 没有类似 ROWNUM 函数的功能,因此我认为最简单的解决方案是添加 RowNumber SELECT 查询中的列如下所示:
它将在开头添加一个包含全零的列,然后迭代生成的 DataTable 并设置行号,如下所示:
然后将 DataTable 转储到网格中。
Your easiest solution would be to modify your code to include a "virtual" RowNumber field in your original SELECT query, like so:
Unfortunately, Access doesn't have anything like a ROWNUM function, so I think the easiest solution is to add a RowNumber column in the SELECT query like this:
which will add a column containing all zeroes at the beginning, and then iterate through the resulting DataTable and set the row number, like this:
and then dump the DataTable into the grid.