简体   繁体   English

如何根据组合框选择更新datagridview?

[英]How to update datagridview based off of combo box selection?

I am attempting to write code that will display data in a datagridview based off of the size of car selected in a combo box. 我正在尝试编写代码,该代码将根据组合框中选择的汽车大小在datagridview中显示数据。 When this code initially runs, it defaults to economy sized, and displays the correct information in the datagridview. 最初运行此代码时,它默认为经济大小,并在datagridview中显示正确的信息。 However, when a different size is selected in the combo box, the text boxes update correctly while the datagridview remains the same. 但是,当在组合框中选择其他大小时,文本框将正确更新,而datagridview保持不变。 What can I do to make it update every time the combo box is changed? 每当组合框更改时,我该怎么做才能使其更新? I thought the code in "private void cboSize_selectionChangeCommitted()" would accomplish this, but there was no change in the output. 我以为“ private void cboSize_selectionChangeCommitted()”中的代码可以完成此操作,但是输出中没有任何变化。

namespace carForm
{
    public partial class Form1 : Form
    {
        _Cars_1_DataSet cDataSet;
        BindingSource sizeBindingSource;
        BindingSource vehicleBindingSource;
        CarsDataClass clsCarsData;

        Boolean gridInitialized;

        public Form1()
        {
            InitializeComponent();
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the '_Cars_1_DataSet.Reservations' table. You can move, or remove it, as needed.
            this.reservationsTableAdapter.Fill(this._Cars_1_DataSet.Reservations);
            // TODO: This line of code loads data into the '_Cars_1_DataSet.Vehicle' table. You can move, or remove it, as needed.
            this.vehicleTableAdapter.Fill(this._Cars_1_DataSet.Vehicle);
            // TODO: This line of code loads data into the '_Cars_1_DataSet.CarSize' table. You can move, or remove it, as needed.
            this.carSizeTableAdapter.Fill(this._Cars_1_DataSet.CarSize);
            clsCarsData = new CarsDataClass();
            cDataSet = clsCarsData.GetDataSet();

            //Binding source sizes
            sizeBindingSource = new BindingSource();
            sizeBindingSource.DataSource = cDataSet;
            sizeBindingSource.DataMember = "CarSize";

            //Binding source vehicles
            vehicleBindingSource = new BindingSource();
            vehicleBindingSource.DataSource = cDataSet;
            vehicleBindingSource.DataMember = "Vehicle";

            //Combo box
            cboSize.DataSource = sizeBindingSource;
            cboSize.DisplayMember = "Size";
            cboSize.ValueMember = "SizeCode";

            //bind other controls
            txtDaily.DataBindings.Add("text", sizeBindingSource, "DailyRate");
            txtMileage.DataBindings.Add("text", sizeBindingSource, "MileageRate");

            //execute combo box
            cboSize_SelectionChangeCommitted(cboSize, e);
        }

        private void cboSize_SelectionChangeCommitted(object sender, EventArgs e)
        {
            string carSelected;
            carSelected = Convert.ToString(cboSize.SelectedValue);
            if (!gridInitialized)
            {
                dgvVehicles.DataSource = vehicleBindingSource;
                gridInitialized = true;
                ChangeGridColumns();
            }
            vehicleBindingSource.Filter = "CarSize = '" + carSelected + "'";
        }

        private void ChangeGridColumns()
        {
            //Change column headers
            //dgvVehicles.Columns["Inv_ID"].Visible = false;
        }
    }
}

使用SelectedIndexChanged而不是SelectionChangeCommitted。

Try using SelectedIndexChanged event from the events menu after clicking on the combobox in the design view. 在设计视图中单击组合框后,尝试从事件菜单中使用SelectedIndexChanged事件。

This should populate in your code: 这应该填充在您的代码中:

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
   Console.WriteLine("test!");
}

In there you can put this logic from your code: 您可以在其中放入代码中的以下逻辑:

string carSelected;
carSelected = Convert.ToString(cboSize.SelectedValue);
if (!gridInitialized)
 {
      dgvVehicles.DataSource = vehicleBindingSource;
      gridInitialized = true;
      ChangeGridColumns();
 }
 vehicleBindingSource.Filter = "CarSize = '" + carSelected + "'";

暂无
暂无

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

相关问题 在DataGridView组合框单元格中发生选择更改时,更新相邻单元格的数据源 - Update the datasource for an adjacent cell when selection change occurs in a DataGridView Combo box cell 如何根据从另一个组合框所做的选择填充组合框 - How to populate a combo box based on the selection made from another combo box C#Windows窗体如何根据第一个组合框中的选择更改第二个组合框的值 - C# Windows Forms how to change values of second combo box based on selection in first combo box 根据数据网格视图选择绑定到组合框 - Bind to Combo box based on Data Gridview selection BindingSource与DataGridView组合框 - BindingSource with DataGridView Combo Box 如何根据组合框中的选定值在 DataGridView 中显示数据? - How do I display data in a DataGridView based on a selected value from a combo box? 如何在 DatagridView 组合框 SelectedIndexChanged 更改事件上重置 DatagridView 组合框单元格 - How to reset DatagridView Combo box cell On the DatagridView Combo Box SelectedIndexChanged change event 根据 DataGridView 中组合框 B 中的选定值过滤组合框 A 中的值 - Filter values in combo box A based on selected value in combo box B in DataGridView 试图弄清楚如何根据组合框选择动态更改用于 label 的数组? - Trying to figure out how to dynamically change which array is used for a label based on combo box selection? 如何根据用户的组合框选择使用词典来操作变量? - How do I use dictionaries to manipulate variables based on the user's combo box selection?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM