【发布时间】:2020-12-26 07:28:44
【问题描述】:
我正在制作 PC 配置程序,但我卡住了。在第一种形式中,我选择了 CPU 和文本框中显示的信息,但在第二种形式中,我选择了 MoBo。 CPU 信息之一是套接字,它是主板表单上的第一个组合框。有没有一种方法可以将 textBox 中的值与该组合框链接?我也用过 Sql Studio。
我真的需要帮助,我的编程很糟糕,我这样做只是为了不让这个学期不及格。请帮助我。
This is picture of my code for that comboBox
private void MaticnaPloca_Load(object sender, EventArgs e) {
con.Open();
SqlCommand sc = new SqlCommand("SELECT distinct(soket) from MaticnaPloca", con);
SqlDataReader reader;
reader = sc.ExecuteReader();
DataTable dt = new DataTable();
dt.Columns.Add("soket", typeof(string));
dt.Load(reader);
cbSoket.ValueMember = "soket";
cbSoket.DataSource = dt;
cbSoket.SelectedIndex = -1;
con.Close();
con.Open();
SqlCommand sc1 = new SqlCommand("SELECT distinct(proizvodjac) from MaticnaPloca order by Proizvodjac", con);
SqlDataReader reader1;
reader1 = sc1.ExecuteReader();
DataTable dt1 = new DataTable();
dt1.Columns.Add("proizvodjac", typeof(string));
dt1.Load(reader1);
comboBox2.ValueMember = "proizvodjac";
comboBox2.DataSource = dt1;
comboBox2.SelectedIndex = -1;
comboBox2.Enabled = false;
con.Close();
con.Open();
SqlCommand sc2 = new SqlCommand("SELECT (model) from MaticnaPloca order by model", con);
SqlDataReader reader2;
reader2 = sc2.ExecuteReader();
DataTable dt2 = new DataTable();
dt2.Columns.Add("model", typeof(string));
dt2.Load(reader2);
comboBox3.ValueMember = "model";
comboBox3.DataSource = dt2;
comboBox3.SelectedIndex = -1;
comboBox3.Enabled = false;
con.Close();
}
private void cbSoket_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
SqlCommand sc = new SqlCommand();
con.Open();
if (cbSoket.SelectedValue.ToString() == "1151")
sc = new SqlCommand("select distinct (proizvodjac) from MaticnaPloca where Soket = '1151'", con);
else if (cbSoket.SelectedValue.ToString() == "AM4")
sc = new SqlCommand("select distinct (proizvodjac) from MaticnaPloca where Soket = 'AM4'", con);
else if (cbSoket.SelectedValue.ToString() == "TR4")
sc = new SqlCommand("select distinct (proizvodjac) from MaticnaPloca where Soket = 'TR4'", con);
SqlDataReader reader;
reader = sc.ExecuteReader();
DataTable dt = new DataTable();
dt.Columns.Add("proizvodjac", typeof(string));
dt.Load(reader);
comboBox2.ValueMember = "proizvodjac";
comboBox2.DataSource = dt;
comboBox2.SelectedIndex = -1;
comboBox2.Enabled = true;
con.Close();
}
catch (Exception)
{
}
}
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
SqlCommand sc = new SqlCommand();
con.Open();
if (comboBox2.SelectedValue.ToString() == "Asus" && cbSoket.SelectedValue.ToString() == "1151")
sc = new SqlCommand("select distinct (model) from MaticnaPloca where Proizvodjac = 'Asus' AND Soket = '1151'", con);
if (comboBox2.SelectedValue.ToString() == "Asus" && cbSoket.SelectedValue.ToString() == "AM4")
sc = new SqlCommand("select distinct(model) from MaticnaPloca where Proizvodjac = 'Asus' AND Soket = 'AM4'", con);
if (comboBox2.SelectedValue.ToString() == "Asus" && cbSoket.SelectedValue.ToString() == "TR4")
sc = new SqlCommand("select distinct(model) from MaticnaPloca where Proizvodjac = 'Asus' AND Soket = 'TR4'", con);
if (comboBox2.SelectedValue.ToString() == "Biostar" && cbSoket.SelectedValue.ToString() == "AM4")
sc = new SqlCommand("select distinct(model) from MaticnaPloca where Proizvodjac = 'Biostar' AND Soket = 'AM4' ", con);
if (comboBox2.SelectedValue.ToString() == "Biostar" && cbSoket.SelectedValue.ToString() == "1151")
sc = new SqlCommand("select distinct(model) from MaticnaPloca where Proizvodjac = 'Biostar' AND Soket = '1151'", con);
if (comboBox2.SelectedValue.ToString() == "Gigabyte" && cbSoket.SelectedValue.ToString() == "1151")
sc = new SqlCommand("select distinct(model) from MaticnaPloca where Proizvodjac = 'Gigabyte' AND Soket = '1151'", con);
if (comboBox2.SelectedValue.ToString() == "Gigabyte" && cbSoket.SelectedValue.ToString() == "AM4")
sc = new SqlCommand("select distinct(model) from MaticnaPloca where Proizvodjac = 'Gigabyte' AND Soket = 'AM4'", con);
if (comboBox2.SelectedValue.ToString() == "Gigabyte" && cbSoket.SelectedValue.ToString() == "TR4")
sc = new SqlCommand("select distinct(model) from MaticnaPloca where Proizvodjac = 'Gigabyte' AND Soket = 'TR4'", con);
if (comboBox2.SelectedValue.ToString() == "MSI" && cbSoket.SelectedValue.ToString() == "AM4")
sc = new SqlCommand("select distinct(model) from MaticnaPloca where Proizvodjac = 'MSI' AND Soket = 'AM4'", con);
if (comboBox2.SelectedValue.ToString() == "MSI" && cbSoket.SelectedValue.ToString() == "TR4")
sc = new SqlCommand("select distinct(model) from MaticnaPloca where Proizvodjac = 'MSI' AND Soket = 'TR4'", con);
SqlDataReader reader;
reader = sc.ExecuteReader();
DataTable dt2 = new DataTable();
dt2.Columns.Add("model", typeof(string));
dt2.Load(reader);
comboBox3.ValueMember = "model";
comboBox3.DataSource = dt2;
comboBox3.Enabled = true;
con.Close();
}
catch (Exception)
{
}
【问题讨论】:
-
我们真的无法猜到您要做什么。您需要告诉我们您正在使用什么 UI 框架(Windows 窗体、WPF、web-y 等),并且您需要向我们展示您拥有的代码 minimal reproducible example。您的问题的答案(文本框的值是否可以与该组合框链接?)是“是”。您可能还想为不熟悉 MoBo 的人拼写一次“主板”
-
我用的是WPF,刚刚添加了我的代码图片,需要添加表单的图片吗?对不起,我不是很擅长这些,我会编辑那部分。
-
向问题添加代码时,需要将其添加为文本。粘贴后,如果它没有显示为代码,请按
{}按钮将其格式化为代码。请注意,在您粘贴代码图片之前,您使用数据库的唯一提示是您说我使用了 Sql Studio(我假设是 SQL Server Management Studio)。假设您正在尝试使用绑定,请查看:docs.microsoft.com/en-us/dotnet/desktop-wpf/data/…。你可能想要一个BindingList<T> -
点击我上次评论中的链接,在文档中查找主从绑定场景