开发者

Create dynamic controls and bind to dataset

I've got a log file that can contain the same event (Verification) multiple times, i.e.:

ID: 001 Verification Result:

Verification Start: 05:42:34

Verification End: 07:12:52

Verification Result:

Verification Start: 12:38:01

Verification End: 14:05:12

I've dynamically created controls (a label and a TextBox or DateTimePicker) that displays this information to the user; what I want to know is how to bind these controls to my dataset in such a way that when written to the database, each verification is a new row associated with the same ID, i.e.:

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

Codewise: I point the program a directory of logs, which are displayed in a checklistbox. On SelectedIndex change, I call the below:

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();
        lblV开发者_运维百科erStart.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);
    }

I then want to use the DateTimePickers and VerTime.Text to update a table with each verification event.


You got to construct the Data object exactly in the format you have mentioned in the table. Then you can bind it to your controls or dataset.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜