简体   繁体   中英

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 . 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 DataTable that 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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM