简体   繁体   English

从表中读取多个数据并创建表格形式的文本框

[英]Read mutliple data from table and create textboxes in form

My table in MySQL is like this: 我在MySQL中的表是这样的:

MySQL表

I have a panel in my Form, and I want to create a TextBox for each data on this table and write sikicerik data to that TextBox's text. 我的窗体中有一个面板,我想为该表上的每个数据创建一个TextBox并将sikicerik数据写入该TextBox的文本中。 Actually I did it but it creates only one TextBox and selects only the first data on the table. 实际上,我做到了,但是它仅创建一个TextBox并仅选择表中的第一个数据。

My code is like this: 我的代码是这样的:

int count = oku.FieldCount;

reader.Read();
{
    for (int i = 0; i < count; i++)
    {
        TextBox txt1 = new TextBox();
        Point txtyer = new Point(x, y);
        txt1.Text = reader["sikicerik"].ToString();
        txt1.Name = i.ToString();
        x = x + 25;
        y = y + 25;
        panel1.Controls.Add(txt1);
    }
}

It creates just one TextBox and writes "5" in it. 它仅创建一个TextBox并在其中写入“ 5”。

How can I do that repeatedly? 我该怎么做呢?

What you need to understand is that you need to call reader.Read() after going through each record. 您需要了解的是,您需要在遍历每条记录之后调用reader.Read() Let's say your result data set has 5 records, so in order to read the 1st record, you need to call reader.Read() which would populate the reader object with the appropriate data. 假设您的结果数据集有5条记录,因此要读取第1条记录,您需要调用reader.Read() ,它将使用适当的数据填充reader对象。 In order to read the 2nd record, you need to again call reader.Read() . 为了读取第二条记录,您需要再次调用reader.Read() Something like this: 像这样:

int count = reader.FieldCount;

for (int i = 0; i < count; i++)
{
    reader.Read();
    TextBox txt1 = new TextBox();
    Point txtyer = new Point(x, y);
    txt1.Location = txtyer;
    txt1.Text = reader["sikicerik"].ToString();
    txt1.Name = i.ToString();
    y = y + 25;
    panel1.Controls.Add(txt1);
 }

The reason why you may not be able to view all TextBoxes is that you are updating both x and y coordinates of the TextBox. 您可能无法查看所有TextBoxes的原因是,您正在更新TextBox的x和y坐标。 What you may probably want to do is just increment the y coordinate as above and all the TextBoxes would appear vertically. 您可能想要做的就是像上面那样增加y坐标,所有TextBoxes都将垂直显示。 You may also want to resize your form so that the TextBoxes don't get hidden. 您可能还需要调整窗体的大小,以使TextBoxes不会被隐藏。

Also, as @Steve mentioned in the comments above (which I missed), you need to assign the location of the new TextBoxes created as is done in the code above. 另外,正如上面评论中提到的@Steve(我想念的那样),您需要分配在上面代码中创建的新TextBoxes的位置。

I suppose that you get the FieldCount from the reader variable. 我想您是从reader变量获取FieldCount的。

Now in this context you should loop over the read and inside the loop create the textbox for each field retrieved by the original query. 现在,在此上下文中,您应该遍历读取,并在循环内部为原始查询检索的每个字段创建文本框。

Finally, if you don't set the Location property of the TextBoxes they will be created each on top of the others and you see only the topmost one with the latest value obtained in the loop 最后,如果您未设置TextBoxes的Location属性,则会在每个文本框的顶部创建它们,并且您只会在循环中看到最顶部的,具有最新值的文本框。

// Get the number of fields present in the reader....
int count = reader.FieldCount;

// Read one record at time
while(reader.Read())
{
    // Create a textbox for each field in the record
    for (int i = 0; i < count; i++)
    {
        TextBox txt1 = new TextBox();

        // Set its location on screen
        // Probably if you have many fields you need to 
        // use a better algorithm to calculate x,y position 
        txt1.Location = new Point(x, y);
        txt1.Text = reader[i].ToString();
        txt1.Name = i.ToString();
        x = x + 25;
        y = y + 25;
        panel1.Controls.Add(txt1);
    }
}

If you want to create textboxes only for the field sikicerik you have two options. 如果只想为sikicerik字段创建文本框,则有两个选择。 The first one is the recommended because it causes less overhead on your database consist in changing the SELECT query used to build the read to 推荐第一个,因为它会减少数据库开销,只需更改用于将读取内容构建为

 SELECT sikicerik FROM yourTableName

The other option is a simple change to your code. 另一个选择是对代码进行简单的更改。 You don't need to loop over FieldCount because you already know that you are interested in only one field 您无需遍历FieldCount,因为您已经知道只对一个字段感兴趣

// Read one record at time
while(oku.Read())
{
    textBox1.Text = oku["soru"].ToString();
    label1.Text = Form2.sinavno;

    // Create the single textbox required for the only field required
    TextBox txt1 = new TextBox();

    // Set its location on screen
    txt1.Location = new Point(x, y);
    txt1.Text = oku["sikicerik"].ToString();
    txt1.Name = i.ToString();
    x = x + 25;
    y = y + 25;
    panel1.Controls.Add(txt1);

    // Repeat the loop for each record.

}

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

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