当数据集为空时在 WPF DataGrid 中显示空白行
我正在 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();
}
如果有人可以帮助我获取这个空行以便用户可以输入数据,我将不胜感激!
谢谢!
I am running an SQL query on a SQL Server 2008 database. The results from this query are displayed in a WPF DataGrid. My code works great, except when the dataset is empty. I want the user to be able to add new rows, but when the dataset is empty there is no empty row for the user to enter data into. Below is my code:
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();
}
If anybody can help me out with getting this empty row so the user can enter data, I would greatly appreciate it!
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
也许你可以先检查一下来源。如果从数据库获取的源为空,那么您将在 GridView 中添加一个新行。
Maybe you can check the source first. if source you get from database is null, then you will add a new row into the GridView.
我刚刚开始明白这一点,但到目前为止这已经奏效了。
然后,为了让“CanUserAddRows”正常工作,您需要在 ViewModel 中使用默认构造函数(在其中进行绑定)。如果没有默认构造函数,则不会得到空白行。
在实际的窗口中,您必须为 DataGrid 设置 DataContext
并在用户编辑或删除时更新数据库,请检查此链接,它看起来很有前途。
http://www.dotnetcurry.com/ShowArticle.aspx?ID=566
I'm just starting to get this, but this has worked so far.
Then to get the 'CanUserAddRows' to work, you need a default constructor in your ViewModel (where you do the bindings). If there is no default constructor, you won't get a blank row.
And in the actual Window, you have to set the DataContext for the DataGrid
And for updating the database when the user edits or deletes, check this link, it looks very promising.
http://www.dotnetcurry.com/ShowArticle.aspx?ID=566
尝试将
DataGrid
的CanUserAddRows
设置为true
Try setting
CanUserAddRows
totrue
ofDataGrid
使用
ListCollectionView
作为源:然后使用以下函数添加空行(如果未出现):
Use a
ListCollectionView
as source:and then following function to add empty row if it is not appeared: