简体   繁体   English

使用填充的组合框值在where子句中使用以过滤结果

[英]Using a populated combobox value to use in a where clause to filter results

Just another problem. 只是另一个问题。

I am trying to populate a data table with just a combo box when a user selects a crop type. 当用户选择作物类型时,我试图仅用组合框填充数据表。

When the user selects a crop from the dropdown ie wheat, barley and oats for example I want that to be added to a view so return the values in the database. 当用户从下拉列表中选择一种作物时,例如小麦,大麦和燕麦,我希望将其添加到视图中,以便返回数据库中的值。

With the crop selected then it will only show the fields that have that crop planted and the costs associated with it. 选择了作物之后,它将仅显示种植该作物的田地以及与此相关的成本。

var select = "SELECT * from fieldCostOverview where CropType = ('" + cmbCrops.SelectedItem.ToString + "')";

//var c = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=H:\School\Magee\Project\Programme\Farm Costs\Farm Costs\farmCosts.mdf;Integrated Security=True"); // Your Connection String here
var c = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=F:\School\Magee\Project\Programme\Farm Costs\Farm Costs\farmCosts.mdf;Integrated Security=True");
var dataAdapter = new SqlDataAdapter(select, c);

var commandBuilder = new SqlCommandBuilder(dataAdapter);
var ds = new DataSet();
dataAdapter.Fill(ds);
dgFieldDetails.ReadOnly = true;
dgFieldDetails.DataSource = ds.Tables[0];

the value in the combo box will then show me only the fields and the details of that crop type. 然后,组合框中的值将仅向我显示该作物类型的字段和详细信息。

Any advice or tips? 有什么建议或提示吗?

使用SelectedValue

  var select = "SELECT * from fieldCostOverview where CropType = ('" +      cmbCrops.SelecteValue + "')";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM