首页 > 解决方案 > 使用组合框对 dataGridView 进行排序

问题描述

我有一个项目:带有一些信息的 dataGridView

我需要 comboBox 来对 dataGridView 进行排序,例如,我选择 coupe 并且应该只显示 86 и Supra。

此外,如果我双击所需的汽车,将弹出另一个窗口所有可用的凯美瑞套装

我也需要对这个 dataGridView2 进行排序。

所以我有2节课

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Toyota_beta
{
    public class Models
    {

        public string Model { get; set; }
        public string Year { get; set; } 
        public string Type { get; set; }
        public string Price { get; set; }

        public List<Cars> Cars { get; set; }



        public Models(string model, string type, string year, 
        string price, List<Cars> cars)
        {
            Model = model; Year = year; Type = type;
            Price = price; Cars = cars;
        }
        public Models()
        {

            Cars = new List<Cars>();
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Toyota_beta
{
    public class Cars
    {

            public string Model { get; set; }
            public int Year { get; set; }
            public string Transmission { get; set; }
            public string Drive { get; set; }
            public int Hp { get; set; }
            public string Engine { get; set; }
            public int Price { get; set; }


            public Cars(string model, int year, string transmission, string drive, string engine,
            int hp, int price)
            {
                Model = model; Year = year; Transmission = transmission;
                Drive = drive; Hp = hp; Engine = engine; Price = price;
            }
            public Cars()
            { }

    }
}

以及我手动插入的dataGridView中的所有数据

namespace Toyota_beta
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public List<Models> listM = new List<Models>();
        public List<Cars> listC;
        private void Form1_Load(object sender, EventArgs e)
        {

            //Yaris---------------------------------------------------------------------------------------------
            listC = new List<Cars>();
            listC.Add(new Cars("Yaris L", 2020, "Manual", "FWD", "Fuel", 106, 15650));
            listC.Add(new Cars("Yaris XLE", 2020, "Automatic", "FWD", "Fuel", 106, 18750));

            listM.Add(new Models("Yaris ", "Sedan", "2020", "from 15,650$", listC));

            //Yaris Hatchback---------------------------------------------------------------------------------------------
            listC = new List<Cars>();
            listC.Add(new Cars("Yaris LE Hatchback", 2020, "Automatic", "FWD", "Fuel", 106, 17750));


            listM.Add(new Models("Yaris Hatchback", "Hatchback", "2020", "from 17,750$", listC));

            //Corolla---------------------------------------------------------------------------------------------
            listC = new List<Cars>();
            listC.Add(new Cars("Corolla L", 2020, "Automatic", "FWD", "Fuel", 139, 19600));
            listC.Add(new Cars("Corolla LE", 2020, "Automatic", "FWD", "Fuel", 139, 20050));
            listC.Add(new Cars("Corolla Hybrid LE", 2020, "Automatic", "FWD", "Fuel", 169, 23100));

            listM.Add(new Models("Cororlla", "Sedan", "2020", "from 19,600$", listC));
...

这就是我填充数据第二个dataGridView的方式

private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dataGridView1.CurrentRow == null || dataGridView1.CurrentRow.Index == dataGridView1.RowCount - 1)
                return;

            Info formC = new Info();


            int n = dataGridView1.CurrentRow.Index;
            formC.carsBindingSource.DataSource = listM[n].Cars;

            formC.ShowDialog();
            modelsBindingSource.ResetCurrentItem();

        }

也许这些图片可以帮助您了解一般情况。

这就是我自己尝试的方法:创建一个过滤 dataGridView 以仅显示需要的行的 bool 方法,但出现了一些问题。

private void b_Show_Click(object sender, EventArgs e)
        {
            bool select = false;
            dataGridView1.CurrentCell = null;

            for (int i = 0; i < dataGridView1.RowCount - 1; i++)
            {
                if (TestRow(i))
                    dataGridView1.Rows[i].Visible = true;
                else
                {
                    dataGridView1.Rows[i].Visible = false;
                    select = true;
                }
            }

        }
        private bool TestRow(int c)
        {
            Cars crs = Form1.listM[m];

            if (comboBox_type.Text != "" &&
                !crs.Type.Contains(comboBox_type.Text)) return false;





        }

我对 c# 有点陌生,所以不要因为这个糟糕的代码而责怪我太多。先感谢您!

标签: c#datagridviewcombobox

解决方案


推荐阅读