c# - 使用组合框对 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# 有点陌生,所以不要因为这个糟糕的代码而责怪我太多。先感谢您!
解决方案
推荐阅读
- wxwidgets - 如何动态缩小 wxBitmapButton 的位图以适应?
- android - MapView 内的按钮不显示
- azure - 尝试发布到 Azure 应用服务时构建失败
- php - 在centOs 7服务器上使用php Imagick转换pdf文件时出错
- azure-sql-database - Azure 数据工厂复制任务 OnPremises DB 到 Azure 数据库
- javascript - 第一次加载页面时如何打开 Popper
- python - 在单独的字典中按其各自的值对列表进行排序?
- python - Python - Selenium:为什么 move_by_offset 不起作用。解决方法请
- c++ - 从 C++ 中的 Content-Disposition 标头中正确提取文件名的更快方法
- ruby - 如何在 rspec 中测试没有视图的控制器?