c# - 在组合框中写入

标签 c# mysql .net database

我有 3 个组合框
起初我输入了站数。例如 1165013450more。 当用户从第一个combobox中选择号码后,自动根据第二个combobox中的站号给出日期。因为每个站的日期都不一样。

我的问题是,如果用户在第二个 combobox 中输入数字键以显示日期,我该怎么做?

我的代码是:

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {



        string MyConString1 = "SERVER=localhost;" +
                "DATABASE=hydrodb;" +
                "UID=root;" +
                "PASSWORD=;";

        MySqlConnection connection1 = new MySqlConnection(MyConString1);

        string command1 = "select Dat FROM hydgod where Station=" + comboBox1.SelectedItem.ToString();
        MySqlDataAdapter da1 = new MySqlDataAdapter(command1, connection1);
        DataTable dt1 = new DataTable();
        da1.Fill(dt1);

        comboBox2.Items.Clear();
        comboBox3.Items.Clear();
        comboBox2.SelectedItem = -1;
        comboBox3.SelectedItem = -1;
        foreach (DataRow row in dt1.Rows)
        {
            string rowz = string.Format("{0}", row.ItemArray[0]);
            comboBox2.Items.Add(rowz);
            comboBox3.Items.Add(rowz);
        }
        connection1.Close();
    }

最佳答案

我建议将所有相应的日期放入 List<string> 中并将其绑定(bind)为 DataSource到第二个ComboBox

List<string> dateList = new List<string>();

foreach (DataRow row in dt1.Rows)
{
    dateList.Add(string.Format("{0}", row.ItemArray[0]);
}

comboBox2.DataSource = dateList;

编辑:

I received the same values result in combobox. 2010-01-01 12:00:00AM
I want to cut the 12:00:00AM

如果row.ItemArray[0]类型为 DateTime你可以试试:

dateList.Add(row.ItemArray[0].ToString("yyyy-MM-dd));

如果是简单的 string你可以拆分它:

dateList.Add(row.ItemArray[0].Split(' ')[0]);

附言。要填充第三个组合框,您也可以将相同的列表也挂接到第三个组合框:

comboBox3.DataSource = dateList;

不要手动添加项目,然后将列表挂接到 DataSource .要么做要么

关于c# - 在组合框中写入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41567887/

相关文章:

.net - 如果所需的 app/web.config 配置设置不存在,最适合抛出的异常是什么?

c# - 使用 C# Regex 解析多行 BBCode

c# - Azure 函数无法访问 mscorelib.dll 访问被拒绝

c# - NHibernate ICriteria - 排序是否允许空值?

c# - 以编程方式在 Azure Function 中设置 RunOnStartup?

mysql - 从 csv 文件导入数据时如何添加日期作为自动更新?

mysql - 无法从本地计算机连接到 AWS RDS 中创建的实例

关联表上的mysql外连接(已选择)

c# - 如何使用c#创建按时间顺序排列的文件夹?

c# - w3wp.exe 中未处理的 Microsoft .net Framework 异常