[英]Pass parameter into where clause and refresh grid view - C#
I would like to filter a datagridview by selecting a value in a combobox. 我想通过在组合框中选择一个值来过滤datagridview。
The top part of my code is a form that inserts data into a table on our server based on a customer profile which is pulled from a view but when a user selects the customer in the top combobox I would like the datagridview to show previous transactions from the table. 我的代码的顶部是一个表单,该表单根据从视图中拉出的客户个人资料将数据插入我们服务器上的表中,但是当用户在顶部组合框中选择客户时,我希望datagridview显示以前的交易桌子。
The code below contains comments of where I will insert the actual code, but wanted to know if I am on the right track. 下面的代码包含有关我将在其中插入实际代码的注释,但想知道我是否处在正确的轨道上。
private void combobox_SelectedChangeCommitted(object sender, EventArgs e)
{
// CONNECTION STRING
{
connection.Open();
//change value into a parameter ready for the next step
//run SQL Command (select * from xxxx where column = @parameter)
//FILL DATA GRID
connection.Close();
}
}
Regards, 问候,
Neil 尼尔
You should bind your Combobox SelectedItem property to your ViewModel like so: 您应该像这样将Combobox SelectedItem属性绑定到ViewModel:
SelectedItem="{Binding UserSelection, Mode=TwoWay}"
And then in your ViewModel your property would be something like so: 然后在ViewModel中,您的属性将如下所示:
private string _userSelection;
public string UserSelection
{
get
{
return _userSelection;
}
set
{
_userSelection = value;
UpdateData();
OnPropertyChanged();
}
}
Then you'd implement your database code something like this: 然后,您将实现数据库代码,如下所示:
private void UpdateData(){
Task.Factory.StartNew(()=>{
connection.Open();
DataProperty = yourNewData;
connection.Close();
});
}
That way you're not doing any database reading/updating on the UI thread, locking your application. 这样,您无需在UI线程上进行任何数据库读取/更新操作,即可锁定应用程序。
Of course, in a decently written WPF application you'd bind your DataGrid to the DataProperty
that I showed in the example code. 当然,在编写得体的WPF应用程序中,您需要将DataGrid绑定到示例代码中显示的
DataProperty
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.