[英]Get the ID of each record in a dynamically created form through button click C# Winform
我有一個表格,可以返回某個醫生的所有預約。 我想知道的是如何獲取每個返回數據的ID。
這就是我想要實現的目標。 1. 根據此特定醫生的預約次數動態創建控件。 2. 編輯每個約會 - 但為了做到這一點。 我需要通過點擊事件獲取特定面板的 ID。 我現在擁有的是..我只得到最后一條記錄的ID——
MySqlConnection con = new MySqlConnection("Server = localhost ;Database = mica_userclient_laravel; username = root;");
// MySqlConnection con = new MySqlConnection("Server = sql12.freemysqlhosting.net; Database = sql12221392; Uid=sql12221392; Password=5gZN1uN6NX;");
MySqlDataAdapter da = new MySqlDataAdapter(@"SELECT users.id,
users.fname, users.mname, users.lname,
appointments.created_at, appointments.id,
appointments.status
FROM users, appointments
WHERE users.id = user_id
AND appointments.status = 0
AND appointments.client_id = '" + textBox2.Text + "'", con);
DataTable dt = new DataTable();
da.Fill(dt);
//textBox1.Text = dt.Rows[1].ItemArray[5].ToString();
//login.quantity = textBox2.Text;
//label4.Text = Convert.ToDateTime(dt.Rows[1].ItemArray[4]).ToString("MMM dd, yyyy");
//label7.Text = Convert.ToDateTime(dt.Rows[1].ItemArray[4]).ToString("h:mm tt");
//label3.Text = dt.Rows[1].ItemArray[3].ToString() + ", " + dt.Rows[1].ItemArray[1].ToString();
for (int i = 0; i < dt.Rows.Count; i++)
{
textBox[i] = new TextBox();
textBox[i].Name = "n" + i;
textBox[i].Text = dt.Rows[i].ItemArray[5].ToString();
textBox[i].Visible = true;
textBox[i].Location = new Point(txtboxX, txtboxY);
txtboxY += 25;
panel[i] = new Panel();
panel[i].Name = "n" + i;
panel[i].Width = 793;
panel[i].Height = 56;
panel[i].BackColor = Color.WhiteSmoke;
panel[i].Location = new Point(panelX, panelY);
panelY += 80;
label[i] = new Label();
label[i].Name = "n" + i;
//label[i].Parent = panel[i];
label[i].Font = new Font("Century Gothic", 14);
label[i].Height = 22;
label[i].Width = 250;
label[i].ForeColor = Color.Gray;
label[i].Text = dt.Rows[i].ItemArray[2].ToString() + ", " + dt.Rows[i].ItemArray[1].ToString();
label[i].Location = new Point(labelX, labelY);
label1[i] = new Label();
label1[i].Name = "n" + i;
//label[i].Parent = panel[i];
label1[i].Font = new Font("Century Gothic", 14, FontStyle.Bold);
label1[i].Height = 22;
label1[i].Width = 250;
label1[i].ForeColor = Color.Gray;
label1[i].Text = Convert.ToDateTime(dt.Rows[i].ItemArray[4]).ToString("MMM dd, yyyy - h:mm tt");
label1[i].Location = new Point(label1X, label1Y);
imgbtn[i] = new BunifuImageButton();
imgbtn[i].BackColor = Color.Transparent;
imgbtn[i].SizeMode = PictureBoxSizeMode.Zoom;
imgbtn[i].ImageLocation = @"Image\edit.png";
imgbtn[i].Width = 46;
imgbtn[i].Height = 38;
//imgbtn[i].ImageActive = @"Image\edit2.png";
imgbtn[i].Location = new Point(imgbtnX, imgbtnY);
imgbtn[i].Click += new EventHandler(bunifuImageButton1_Click);
this.Controls.Add(panel[i]);
this.Controls.Add(label[i]);
this.Controls.Add(imgbtn[i]);
this.Controls.Add(textBox[i]);
panel[i].Controls.Add(label[i]);
panel[i].Controls.Add(label1[i]);
panel[i].Controls.Add(imgbtn[i]);
gumanaka = textBox[i].Text;
}
單擊它時,您可以使用 DataRowView 獲取表格的列..
對於表的選擇更改事件的示例,您可以使用:
DataRowView dr = table_name.SelectedItem as DataRowView;
int id = Convert.ToInt32(dr["appointments_id"]);
或者在您的情況下,如果它是第 5 個元素:
int id = Convert.ToInt32(dr[4]);
只需檢查值
MessageBox.Show(dr.ToString());
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.