【发布时间】:2011-02-07 17:16:16
【问题描述】:
我有一个日志文件可以包含多次相同的事件(验证),即:
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 来更新每个验证事件的表。
【问题讨论】:
-
我有一些想法,但我需要更多细节,我不明白你在做什么。一些代码会有所帮助
标签: c# data-binding dynamic controls datasource