我正在创建一个 WMI 程序,它将查询输出到数据表,然后输出到数据网格。我想创建一个历史下拉列表,将当前日期和时间作为一个项目,并将刚刚创建的数据表克隆为“值”。
选择下拉列表中的项目会将数据网格更改为当时运行的任何先前数据表。
这是到目前为止的代码,但我不确定如何将数据表添加为字符串或数组?
// copy current data table
DataTable cloneTable = new DataTable();
cloneTable = dt.Copy();
ddlEventhistory.Items.Add(new Data {
Name = DateTime.Now.ToString(),
Value = cloneTable ()
});
ddlEventhistory.DisplayMember = "Name";
ddlEventhistory.ValueMember = "Value";
更新 1:这是我的自定义类:
private class Data
{
public string Name { get; set; }
public Object Value { get; set; }
}
更新 2:这是我的下拉列表更改的情况:
private void ddlEventhistory_SelectionChangeCommitted(object sender, EventArgs e)
{
dgEvent.DataSource = (ddlEventhistory.SelectedValue as Data).Value as DataTable;
}
更新 3:这是最终代码
private class Data
{
public string Name { get; set; }
public Object Value { get; set; }
}
//History
//take existing data table and copy it's source
DataTable eventhistory = new DataTable();
eventhistory = (DataTable)dgEvent.DataSource;
ddlEventhistory.Items.Add(new Data { Name = DateTime.Now.ToString(), Value = eventhistory });
ddlEventhistory.DisplayMember = "Name";
ddlEventhistory.ValueMember = "Value";
//when drop down list selection is changed do....
private void ddlEventhistory_SelectionChangeCommitted(object sender, EventArgs e)
{
// Assign DataTable of selected ComboBox item to DataGrid.
dgEvent.DataSource = (ddlEventhistory.SelectedItem as Data).Value as DataTable;
}