简体   繁体   English

通过按钮点击C#Winform获取动态创建表单中每条记录的ID

[英]Get the ID of each record in a dynamically created form through button click C# Winform

I have a form that returns all appointments of a certain doctor.我有一个表格,可以返回某个医生的所有预约。 What I want to know is how to get the ID of each returned data.我想知道的是如何获取每个返回数据的ID。

Here's what I'm trying to achieve.这就是我想要实现的目标。 1. Create controls dynamically based on the number of appointments on this particular doctor. 1. 根据此特定医生的预约次数动态创建控件。 2. To edit each appointments - but in order to do that. 2. 编辑每个约会 - 但为了做到这一点。 I need to get the ID of a specific panel through a click event.我需要通过点击事件获取特定面板的 ID。 What I have right now is.. I only get the ID of the last record –我现在拥有的是..我只得到最后一条记录的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;
}

You can use DataRowView to get the column of the table when clicked on it..单击它时,您可以使用 DataRowView 获取表格的列..

For an example on a selection changed event of your table you can use this:对于表的选择更改事件的示例,您可以使用:

DataRowView dr = table_name.SelectedItem as DataRowView;
int id = Convert.ToInt32(dr["appointments_id"]);

Or in your case if its the 5th element:或者在您的情况下,如果它是第 5 个元素:

int id = Convert.ToInt32(dr[4]);

just check the value with只需检查值

MessageBox.Show(dr.ToString());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM