I have a win form which has a 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. 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.
You can have in your form a field of type
DataTablethat will hold the data you want. Then, whenever you add an item to your listbox, add a row to the data table with same data:And when you need to send the data to database, just send
_tablefield as a parameter to the method that saves data.