【问题标题】:Writing in combobox在组合框中书写
【发布时间】:2017-05-24 21:17:12
【问题描述】:

我有 3 个comboboxes
起初我输入了电台的数量。例如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();
    }

【问题讨论】:

  • 只是为了澄清:您想根据所选站号使用日期填充第二个ComboBox
  • 是的,我想填充第二个和第三个组合框

标签: c# mysql .net database


【解决方案1】:

我建议将所有相应日期放入List<string> 并将其作为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;

编辑:

我在组合框中收到了相同的值。 2010-01-01 12:00:00AM
我想剪掉 12:00:00AM

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

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

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

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

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

comboBox3.DataSource = dateList;

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

【讨论】:

  • 在combobox1的方法中复制你的代码,但是没有成功。
  • @BlagovestPizhev 究竟什么不起作用?你得到错误的价值观吗?你根本没有价值吗?你确定你从数据库中得到正确的值吗?你检查了吗?
  • 通过输入我编写的代码,我在组合框中收到了相同的值。 2010-01-01 12:00:00AM 我想缩短 12:00:00AM。我尝试在查询中写入 Date_Format(Dat, %Y%m%d) 但我再次收到相同的值
  • @BlagovestPizhev 是ItemArray[0] DateTime 的类型吗?
  • dateList.Add(string.Format("{0}", row.ItemArray[0]));
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-05-13
  • 1970-01-01
  • 2021-05-15
  • 1970-01-01
  • 2013-10-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多