[英]How can I take all the elements / fields from a Listbox (in C#) and put them into a DataTable?
I have a win form which has a ListBox
. 我有一个有
ListBox
的获胜表格。 I want to create dynamically a DataTable
(till now I only declared some columns - you can see in the code - that I later want to use to link the DataTable
to an existing empty DataBase
) but don't know how to link it to the Listbox
in order to "take" the 4 elements from it: event_time , event_filename , event_name , event_fullpath
. 我想动态创建一个
DataTable
(到现在为止,我只声明了一些列-您可以在代码中看到-以后我想用它来将DataTable
链接到现有的空DataBase
),但是不知道如何将其链接到Listbox
是为了从中“获取”四个元素: event_time , event_filename , event_name , event_fullpath
。 Pls Help, 请帮助,
Part of my code till now is: 到目前为止,我的部分代码是:
private delegate void AppendListHandler(string event_filename, String event_name, String event_fullpath);
private void AppendText(string event_filename, String event_name, String event_fullpath)
{
if (lstResultLog.InvokeRequired)
lstResultLog.Invoke(new AppendListHandler(AppendText), new object[] { event_filename, event_name, event_fullpath });
else
{
DateTime event_time = DateTime.Now;
//String event_duration = event_time.ToString("HH:mm");
lstResultLog.Items.Add(event_time + event_filename + event_name + event_fullpath);
}
DataTable table = new DataTable("tbl_Event");
table.Columns.Add("event_duration");
table.Columns.Add("event_name");
table.Columns.Add("event_filename");
table.Columns.Add("event_fullpath");
table = (DataTable)lstResultLog.DataSource;
}
lstResultLog
is the name of the ListBox, all the fields from the ListBox have the exact name as in the declared DataTable, and as the DataBase. lstResultLog
是ListBox的名称,ListBox中的所有字段都具有与声明的DataTable和数据库相同的确切名称。
You can have in your form a field of type DataTable
that will hold the data you want. 您可以在表单中具有一个类型为
DataTable
的字段,该字段将保存所需的数据。 Then, whenever you add an item to your listbox, add a row to the data table with same data: 然后,每当您向列表框中添加一个项目时,就向数据表中添加一行具有相同数据的数据:
public class YourForm
{
private DataTable _table;
public YourForm()
{
InitializeComponents();
_table = BuildDataTable();
}
private DataTable BuildDataTable()
{
DataTable table = new DataTable("tbl_Event");
table.Columns.Add("event_duration");
table.Columns.Add("event_name");
table.Columns.Add("event_filename");
table.Columns.Add("event_fullpath");
return table;
}
private void AppendText(string event_filename, String event_name, String event_fullpath)
{
if (lstResultLog.InvokeRequired)
lstResultLog.Invoke(new AppendListHandler(AppendText), new object[] { event_filename, event_name, event_fullpath });
else
{
DateTime event_time = DateTime.Now;
lstResultLog.Items.Add(event_time + event_filename + event_name + event_fullpath);
//Create new row
var row = _table.NewRow();
// Fill row values
row["event_name"] = event_name;
// Add row to table
_table.Rows.Add(row);
}
}
}
And when you need to send the data to database, just send _table
field as a parameter to the method that saves data. 而且,当您需要将数据发送到数据库时,只需将
_table
字段作为参数发送到保存数据的方法即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.