【发布时间】:2011-11-29 04:38:23
【问题描述】:
我正在 SQL Server 2008 数据库上运行 SQL 查询。此查询的结果显示在 WPF DataGrid 中。我的代码运行良好,除非数据集为空。我希望用户能够添加新行,但是当数据集为空时,没有空行供用户输入数据。以下是我的代码:
try
{
string accountQuery = "SELECT a.AccountName AS 'Account Name', a.AccountDesc AS 'Account Description', " +
"a.AccountNumber AS 'Account #', b.AccountType AS 'Account Type', c.AccountName AS 'Account Parent' " +
"FROM Accounts AS a INNER JOIN AccountType AS b ON a.AccountTypeID = b.AccountTypeID " +
"LEFT OUTER JOIN Accounts AS c ON c.AccountID = a.AccountParentID " +
"WHERE a.UserID = " + currentUserID;
SqlDataReader accounts = null;
SqlCommand query = new SqlCommand(accountQuery, dbConnection);
accounts = query.ExecuteReader();
DataTable accountsTable = new DataTable();
accountsTable.Load(accounts);
this.GridAccounts.ItemsSource = accountsTable.DefaultView;
accounts.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
// Close the DB if there was an error.
if (dbConnection.State == ConnectionState.Open)
dbConnection.Close();
}
如果有人可以帮助我获取这个空行以便用户输入数据,我将不胜感激!
谢谢!
【问题讨论】:
标签: c# wpf datagrid wpfdatagrid