将列表框选定项目导出到数据表

时间:2012-10-04 08:40:30

标签: c#

我将选定的listboxitems导出到数据表时遇到问题,我希望将多个选定的工厂导出到数据表中,您能帮忙吗?

   int numberofplants = 0;

        foreach (ListItem li in lbxPlants.Items)
        {
            if (li.Selected)
            {
                numberofplants++;
                DataTable dtplants = new DataTable();
                dtplants.Columns.Add("Plants");
                DataRow drplants = dtplants.NewRow();
                drplants[0] = li.Value;
                dtplants.Rows.Add(drplants);

            }
        }

1 个答案:

答案 0 :(得分:1)

int numberofplants = 0;

DataTable dtplants = new DataTable();
dtplants.Columns.Add("Plants");

foreach (ListItem li in lbxPlants.Items)
{
    if (li.Selected)
    {
        numberofplants++;

        DataRow drplants = dtplants.NewRow();
        drplants[0] = li.Value;
        dtplants.Rows.Add(drplants);
    }
}