C#click事件无法将数组中数据传入,导致DateTimePicker所选数据无法传送

           string doc_date = date[m].Value.ToString();//date数据(求解)--此时的doc_date还是原定数据,并不是我所选中的数据;如果将此行放入click内,而date[m]则会显示NULL,无法运行!!!!!!!!!
            string doc_name = doctor_name[m];
            string doc_price = price[m].Text;
            string doc_time1 = doctor_time1[m];
            string doc_remind1 = doctor_remind1[m];

            time1_btn[m].Click += (time1_btn_s, time1_btn_e) =>
            {
                //将数据导入record
                string strcon3 = "Data Source=LAPTOP;Initial Catalog=Big1;Integrated Security=True";
                SqlConnection conn3 = new SqlConnection(strcon3);
                conn3.Open();
                
                string time = "insert into record(账号,医院科室,医师名称,挂号类别,挂号价格,挂号日期,挂号时间)values('" + userId + "','"+ doctor_dep + "','" + doc_name + "','专家号','" + doc_price + "','" + doc_date + "','" + doc_time1 + "')";
                SqlCommand com_time = new SqlCommand(time, conn3);
                com_time.ExecuteNonQuery();
                conn3.Close();
                //使余量减一
                int remind = Convert.ToInt32(doc_remind1);
                if(remind > 0)
                {
                    string remind_str = Convert.ToString(remind - 1);
                    string strcon4 = "Data Source=LAPTOP;Initial Catalog=Big1;Integrated Security=True";
                    SqlConnection conn4 = new SqlConnection(strcon4);
                    conn4.Open();
                    string remind_del = "update Doctor_information set 时间余量1=" + remind_str + " where 医院科室='" + doctor_dep + "' and 医师名称='" + doc_name + "'";
                    SqlCommand com_remind = new SqlCommand(remind_del, conn4);
                    com_remind.ExecuteNonQuery();
                    conn4.Close();

                    MessageBox.Show("挂号成功!");
                    this.Visible = false;
                    Login log = new Login();
                    log.ShowDialog();
                }
                else
                {
                    MessageBox.Show("此时间已无余量,请另选时间或医师!");
                }
                
            };

以下只是根据你的问题简单设计了一个示例程序,具体的逻辑处理自行完成。

先看运行效果:

img

完整示例代码我放到了这里:

核心代码如下:

Form2.cs

private List<Doctor> _doctors;
        private void Form2_Load(object sender, EventArgs e)
        {
            _doctors = new List<Doctor>
            {
                new Doctor{Id = 1,Name = "Doctor(1)"},
                new Doctor{Id = 2,Name = "Doctor(2)"},
                new Doctor{Id = 3,Name = "Doctor(3)"}
            };
            foreach (var doctor in _doctors)
            {
                var p = new Panel();
                p.Location = new Point(0, doctor.Id * 50 + 10);
                p.Size = new Size(panel1.Width, 50);
                p.BorderStyle = BorderStyle.FixedSingle;
                p.Anchor = AnchorStyles.Left | AnchorStyles.Right;

                var lb = new Label
                {
                    Text = doctor.Name,
                    Location = new Point(0, 10),
                    Size = new Size(100, 30),
                    Font = new Font(FontFamily.GenericSansSerif, 14, FontStyle.Bold)
                };

                var dp = new DateTimePicker
                {
                    Location = new Point(110, 10)
                };
                var btn = new Button
                {
                    Text = @"预约",
                    Dock = DockStyle.Right,
                    Size = new Size(80, 20)
                };
                btn.Click += (s, ev) =>
                {
                    MessageBox.Show($"你当前选择的医生是:{doctor.Name},预约时间是:{dp.Value.ToString("yyyy-MM-dd")}");
                };
                p.Controls.Add(btn);
                p.Controls.Add(lb);
                p.Controls.Add(dp);
                panel1.Controls.Add(p);
            }
        }

你的date[m]数组是从哪里有的呢,变量m又是如何定义和使用的呢?
单从你贴的代码片段,看不从具体的问题。

我在挂号时用了datetimepicker控件,也是动态生成的,每个医生都有对应的一个。
但是在挂号click里面,不知道为啥不能直接用数组的数据,就是不能直接用detetimepicker[m].value.tostring()。
所以我先在click外面用一个字符串将时间数据导入,但是这个时候又出现了问题,就是如果这个字符串的定义和赋值是在click外面,那么他所附的值是初始值,并不是我所选中的值。