简体   繁体   English

组合框绑定到需要在组合框上进行选择之前显示全部的字段

[英]Combo box bound to a field required to show All before a selection is made on the Combo box

I have created a Combo box bound to "Clinics" field of a table.我创建了一个绑定到表格“诊所”字段的组合框。 I wanted to show "All" on the first line of the Combo box list to show all activities of the clinics by default in a table.我想在组合框列表的第一行显示“全部”,以在表格中默认显示诊所的所有活动。 When a specific clinic is selected it shows the activities of the selected clinic.选择特定诊所后,它会显示所选诊所的活动。 My script looks like this:我的脚本如下所示:

[My Code looks like this] [我的代码看起来像这样]

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

    private void Form1_Load(object sender, EventArgs e)
    {
        string mainconn = ConfigurationManager.ConnectionStrings["Myconn"].ConnectionString;
        SqlConnection sqlconn = new SqlConnection(mainconn);
        string sqlquery = "select * from [dbo].[EC_HARS_DQ]";
        SqlCommand sqlcomm = new SqlCommand(sqlquery, sqlconn);
        sqlconn.Open();
        SqlDataAdapter sdr = new SqlDataAdapter(sqlcomm);
        DataTable dt = new DataTable();
        sdr.Fill(dt);
        comboBox1.DisplayMember = "clinic";
        comboBox1.DataSource = dt;
        sqlconn.Close();


    }

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        string mainconn = ConfigurationManager.ConnectionStrings["Myconn"].ConnectionString;
        SqlConnection sqlconn = new SqlConnection(mainconn);
        string sqlquery = "select * from [dbo].[EC_HARS_DQ] where clinic='"+comboBox1.Text.ToString()+"'";
        SqlCommand sqlcomm = new SqlCommand(sqlquery, sqlconn);
        sqlconn.Open();
        SqlDataAdapter sdr = new SqlDataAdapter(sqlcomm);
        DataTable dt = new DataTable();
        sdr.Fill(dt);
        dataGridView1.DataSource = dt;
        sqlconn.Close();
    }
}

} }

First off, refrain from SELECT * , instead select only needed columns.首先,不要使用SELECT * ,而是 select 只需要列。

You can add All using a UNION.您可以使用 UNION 添加All

In this example, table name is Categories with the intent to provide the primary key for use after a selection is made and description to display.在此示例中,表名称是 Categories,旨在提供选择后使用的主键和要显示的描述。

SELECT CategoryID, CategoryName FROM dbo.Categories;

Now using a SELECT with 0 in this case to represent an identifier and 'All` to display in the ComboBox then union for all rows in the table.现在使用 SELECT,在这种情况下,0 表示标识符,“All”显示在 ComboBox 中,然后联合表中的所有行。

SELECT 0 AS CategoryID, 'All' AS CategoryName 
UNION ALL SELECT CategoryID, CategoryName FROM dbo.Categories;

在此处输入图像描述

In code you can read the data into a DataTable, set DisplayMember , in this case to CategoryName and ValueMember to CategoryID.在代码中,您可以将数据读入 DataTable,将DisplayMember设置为 CategoryName,将ValueMember设置为 CategoryID。

When a selection is made check SelectedItem , if 0 All is the selection, else an existing primary key would be used for your business logic.进行选择后检查SelectedItem ,如果选择 0 All ,则现有主键将用于您的业务逻辑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM