简体   繁体   English

如何从Listbox(在C#中)中获取所有元素/字段并将它们放入DataTable中?

[英]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.

相关问题 UNITY,我如何从另一个 C# class 获取所有属性并将它们放入枚举 - UNITY, How can I get all properties from another C# class and put them into an enum 在 C# 中,如何使列表框中的所有项目不更改为我最后设置的颜色? - In C#, how can I keep all my items in a listbox from changing to the color I set last? C#如何读取datagridview中的所有数据并将其放入列表框中? - C# How to read all the data in a datagridview and put it in a listbox? 如何获取C#类的内容并放入/取出字符串? - How can I take the contents of a C# class and put into / take out of a string? 如何使用C#将两个列表中的元素混合并存储为另一个列表? - How can I mix elements from two list and store them is another list using C#? 如何在C#中从另一台PC访问列表框 - How can i access a listbox from another pc in C# 我如何获得多个价格数据并使用 Selenium 将它们排序并放入带有 c# 的标签? - How can i get plural price data and put them order and put in labels with c# using Selenium? 在C#中从数据表获取值到列表框 - to obtain the value from a datatable to a listbox in c# 从访问和列表框中获取数据C# - Take data from access and to listbox c# 如何使用Selenium找到所有元素并将它们放在列表中? - How can I find all elements and put them in a list using Selenium?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM