创建动态控件并绑定到数据集

时间:2011-02-07 17:16:17

标签: c# data-binding dynamic controls datasource

我有一个日志文件,可以多次包含相同的事件(验证),即:

ID:001 验证结果:
验证开始时间:05:42:34
验证结束时间:07:12:52

验证结果:
验证开始时间:12:38:01
验证结束时间:14:05:12

我动态创建了向用户显示此信息的控件(标签和TextBox或DateTimePicker);我想知道的是如何将这些控件绑定到我的数据集,以便在写入数据库时​​,每个验证都是与相同ID相关联的新行,即:

UID | ID  | Start    | End  
1   | 001 | 05:42:34 | 07:12:52  
2   | 001 | 12:38:01 | 14:05:12  

Codewise:我指向程序一个日志目录,它显示在一个清单框中。在SelectedIndex更改时,我调用以下内容:

void ParseImagerLogs(string source)
    {
    int i = 0;
    //Log fields are parsed to a key/value pair
         if (key.Equals("LogID"))
         {
             logIdTextBox.Text = value
         }

         if (key.Equals("VerStarted"))
         {
             //Date format ddd MMM dd HH:mm:ss yyyy is parsed to a useable format and 
             //saved as timeStart
             verStartedDateTimePicker.Value = verTimeStart;
         }

         if (key.Equals("VerEnded"))
         {
             //Same parsing technique
             verEndedDateTimePicker.Value = verTimeEnd;
             //Work out the elapsed time
             verTime - timeEnd.Subtract(timeStart).ToString();
             //Pass the fields to a new method which dynamically generates the 
             GenerateVerificationFields(i, verStartedDateTimePicker.Value, verEndeDateTimePicker.Value, verTime.TextBox
             //I have my first verification instance, so i++ and look for the next one.
             i++;
          }
     }

private void GenerateVerificationFields(int amount, DateTime verStartval, DateTime verEndval, string verTimeval)
    {

        int i = amount;
        int j = i + 1;
        //Create groupBox
        GroupBox groupBox = new GroupBox();
        groupBox.Name = "verGroup" + i.ToString();
        groupBox.Width = 430;
        groupBox.Height = 120;
        groupBox.Left = 5;
        groupBox.Top = 42 + (groupBox.Height * i) + (10 * i);
        groupBox.Text = "Verification #" + j.ToString();

        //Create VerStart
        DateTimePicker verStart = new DateTimePicker();
        verStart.Name = "verStart" + i.ToString();
        verStart.Width = 224;
        verStart.Height = 22;
        verStart.Location = new Point(160, 25);
        verStart.DataBindings.Add(new Binding("Value", acqreports_testDataSet, "verification.VerStarted"));
        verStart.Value = verStartval;

        //Create VerEnded
        DateTimePicker verEnd = new DateTimePicker();
        verEnd.Name = "verStart" + i.ToString();
        verEnd.Width = 224;
        verEnd.Height = 22;
        verEnd.Location = new Point(160, 55);
        verEnd.DataBindings.Add(new Binding("Value", acqreports_testDataSet, "verification.VerEnded"));
        verEnd.Value = verEndval;

        //Create VerTime
        TextBox verTime = new TextBox();
        verTime.Name = "verTime" + i.ToString();
        verTime.Width = 224;
        verTime.Height = 22;
        verTime.Location = new Point(160, 85);
        verTime.Text = verTimeval;

        //Create Ver Start label
        Label lblVerStart = new Label();
        lblVerStart.Name = lblVerStart + i.ToString();
        lblVerStart.Text = "Ver Started: ";
        lblVerStart.Location = new Point(5, 25);

        //Create Ver End label
        Label lblVerEnd = new Label();
        lblVerEnd.Name = lblVerEnd + i.ToString();
        lblVerEnd.Text = "Ver Ended: ";
        lblVerEnd.Location = new Point(5, 55);

        //Create Ver Time label
        Label lblVerTime = new Label();
        lblVerTime.Name = lblVerTime + i.ToString();
        lblVerTime.Text = "Ver Time: ";
        lblVerTime.Location = new Point(5, 85);

        //Add created controls to groupBox
        groupBox.Controls.Add(verStart);
        groupBox.Controls.Add(verEnd);
        groupBox.Controls.Add(verTime);
        groupBox.Controls.Add(lblVerStart);
        groupBox.Controls.Add(lblVerEnd);
        groupBox.Controls.Add(lblVerTime);
        tabControl2.TabPages[2].Controls.Add(groupBox);
    }

然后我想使用DateTimePickers和VerTime.Text来更新每个验证事件的表。

1 个答案:

答案 0 :(得分:0)

您必须使用表中提到的格式完全构造Data对象。然后,您可以将其绑定到控件或数据集。