如果SQL Server数据库中存在所选项,请从组合框中选择下一项 [英] Select next item from combo box if the selected item exists in SQL server database

查看:109
本文介绍了如果SQL Server数据库中存在所选项,请从组合框中选择下一项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Windows窗体上,有两个组合框,分别显示特定课程的名称和部分的数量。但是,我想为我的组合框(部分数量)编写一些代码,使其能够自动选择正确的部分编号。例如,对于课程C#,如果第一部分插入到SQL Server中,则部分编号的组合框应选择该课程的第二部分。

 SqlConnection objConnection =  new  SqlConnection( @ 数据源=(LocalDB)\ MSSQLLocalDB ; AttachDbFilename = C:\UniversityDataBase.mdf; Integrated Security = True); 

private void CourseName_Click(对象 sender,EventArgs e)
{
string query = SELECT * FROM CourseTable;

SqlDataAdapter SDA = new SqlDataAdapter(query,objConnection);
DataTable dt = new DataTable();
SDA.Fill(dt);

comboBox1.DataSource = dt;
comboBox1.DisplayMember = Coursename;
objConnection.Close();
}

private void SectionNo_Click( object sender,EventArgs e)
{
string query = SELECT * FROM SecNo;

SqlDataAdapter SDA = new SqlDataAdapter(query,objConnection);
DataTable dt = new DataTable();
SDA.Fill(dt);

comboBox2.DataSource = dt;
comboBox2.DisplayMember = SectoinNumber;

objConnection.Close();
}





我的尝试:



c# - 如果SQL Server数据库中存在该项,则从组合框中选择下一项 - Stack Overflow [ ^ ]

解决方案

这可以是你的解决方案



1)你需要在选择组合框一个事件时编写代码逻辑

2)in代码逻辑 - 首先运行查询以获取为所选课程插入的部分

3)然后更新第二个combox框的属性以选择部分(将在部分编号+ 1之上)

On my Windows form, there are two comboboxes showing name of specific courses and number of sections respectively. But, I want to write some codes for my combobox (number of sections) that makes it enable to choose the correct section number automatically. For example, for course C#, if section one is inserted into SQL Server, the combobox for section number should select section two for that course.

SqlConnection objConnection = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\UniversityDataBase.mdf;Integrated Security=True");

private void CourseName_Click(object sender, EventArgs e)
{
    string query = "SELECT *FROM CourseTable";

    SqlDataAdapter SDA = new SqlDataAdapter(query, objConnection);
    DataTable dt = new DataTable();
    SDA.Fill(dt);

    comboBox1.DataSource = dt;
    comboBox1.DisplayMember = "Coursename";
    objConnection.Close();
}

private void SectionNo_Click(object sender, EventArgs e)
{
    string query = "SELECT * FROM SecNo";

    SqlDataAdapter SDA = new SqlDataAdapter(query, objConnection);
    DataTable dt = new DataTable();
    SDA.Fill(dt);

    comboBox2.DataSource = dt;
    comboBox2.DisplayMember = "SectoinNumber";

    objConnection.Close();
}



What I have tried:

c# - Select Next Item from Combo Box if the item exists in SQL Server database - Stack Overflow[^]

解决方案

this can be your solution

1) you need to write code logic on selection of combobox one event
2) in the code logic - first run query to get the section inserted for that selected course
3) then update the property of second combox box to select the section (which will be above section number + 1)


这篇关于如果SQL Server数据库中存在所选项,请从组合框中选择下一项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆