У меня есть проект: ![dataGridView with some info](https://i.stack.imgur.com/KrNS7.png)
Мне нужен comboBox для сортировки dataGridView, например, я выбираю coupe, и должны отображаться только 86 и Supra.
Также, если я дважды щелкну по нужной машине, появится другое окно ![All available Camry sets](https://i.stack.imgur.com/AVZPK.png)
Мне нужно, чтобы этот 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();
}
Может быть эти картинки могут помочь вам понять общую ситуацию.
Вот что я попробовал сам: создать метод bool, который фильтрует dataGridView для отображения только необходимых строк, но возникают некоторые проблемы.
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#, так что не вините меня слишком много пожалуйста за этот дерьмовый код. Заранее спасибо!