当我选择一个特定的日期(如我在MonthCalendar中的代码中的代码)时,我会删除列表框中的某些项目,但是之后,当我在MonthCalendar中选择另一个日期时,我希望这些项目又回到列表框中,该怎么做?选择特定日期后,我无法取回这些物品。

private void monthCalendar1_DateChanged_1(object sender, DateRangeEventArgs e)
    {
        MyConn3 = new MySqlConnection(MyConnection3);
        string mday= monthCalendar1.SelectionStart.Date.ToString("yyyyMMdd");
        string test = ("select day, time, name from system where day='"+mday+"'");
        cmd = new MySqlCommand(test, MyConn3);
        MySqlDataReader MyReader2;
        try
        {
            MyConn3.Open();
            MyReader2 = cmd.ExecuteReader();

            while (MyReader2.Read())
            {
                 eDay = MyReader2.GetString("day");
                 eTime = MyReader2.GetString("time");
                string namn = MyReader2.GetString("name");

                if (eDay.Contains(mday))
                {
                    Console.WriteLine("time " + eTime);
                    Console.WriteLine(eDay);
                    Console.WriteLine(eTime);
                    Console.WriteLine("test: " + mday);

                    listBox1.Items.Remove(eTime);
                }
                }

        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

        MyConn3.Close();


    }

最佳答案

您需要在“ monthCalendar1_DateChanged_1”方法开始起作用之前保留listBox1的dataSource。

在方法开始时,必须将dataSource设置为listBox1,然后从listBox中删除项目。因此,代码将如下所示:

private List<string> myGlobalDataSource= new List<string>{"item1","item2"};

private void monthCalendar1_DateChanged_1(object sender, DateRangeEventArgs e)
    {
        listBox1.DataSource = myGlobalDataSource;
        MyConn3 = new MySqlConnection(MyConnection3);

....

关于c# - 在MonthCalendar中选择特定日期后如何删除项目,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35701892/

10-11 17:55