数据不会在列表视图中传输

时间:2016-05-31 11:34:38

标签: c# xml winforms

我有两个xml文件。如果从下拉列表中选择,则将XML文件中的数据移动到表中。但是当我点击一秒钟时只能工作第一个xml文件,然后没有任何反应,表格没有填充。我的错误是什么?

private void listView1_SelectedIndexChanged(object sender, EventArgs e)
    {

        if (listView1.SelectedItems.Count > 0)
        {
            ListViewItem lv = listView1.SelectedItems[0];
            textBox1.Text = lv.SubItems[1].Text;
            textBox2.Text = lv.SubItems[2].Text;
            textBox3.Text = lv.SubItems[3].Text;

        }

    }

    private void comboBox1_SelectedIndexChanged_1(object sender, EventArgs e)
    {
        if (comboBox1.SelectedIndex == 0)
        {
            DataSet ds = new DataSet();
            DataTable dt = new DataTable();
            ds.ReadXml(@"C:\XMLFile1.xml");
            dt = ds.Tables[0];
            if (dt.Rows.Count > 0)
            {
                int i = 0;
                foreach (DataRow Dr in dt.Rows)
                {
                    ListViewItem lv = new ListViewItem(dt.Rows[i]["ID"].ToString());
                    lv.SubItems.Add(dt.Rows[i]["Name"].ToString());
                    lv.SubItems.Add(dt.Rows[i]["group"].ToString());
                    lv.SubItems.Add(dt.Rows[i]["document"].ToString());
                    i++;
                    listView1.Items.Add(lv);


                }
            }
            if (comboBox1.SelectedIndex == 1)
            {
                DataSet ds1 = new DataSet();
                DataTable dt1 = new DataTable();
                ds1.ReadXml(@"C:\XMLFile1.xml");
                dt1 = ds1.Tables[0];
                if (dt1.Rows.Count > 0)
                {
                    int i = 0;
                    foreach (DataRow qw in dt1.Rows)
                    {
                        ListViewItem lv = new ListViewItem(dt1.Rows[i]["ID"].ToString());
                        lv.SubItems.Add(dt1.Rows[i]["Name"].ToString());
                        lv.SubItems.Add(dt1.Rows[i]["group"].ToString());
                        lv.SubItems.Add(dt1.Rows[i]["document"].ToString());
                        i++;
                        listView1.Items.Add(lv);


                    }
                }
            }

1 个答案:

答案 0 :(得分:0)

两个comboBox1选项都有完全相同的文件: ds.ReadXml(@"C:\XMLFile1.xml");。我假设第二个文件的@"C:\XMLFile1.xml"必须不同。

private void comboBox1_SelectedIndexChanged_1(object sender, EventArgs e)
{ //Opening bracket 1
    if (comboBox1.SelectedIndex == 0) //Option 1
    { //Opening bracket 2

        ds.ReadXml(@"C:\XMLFile1.xml");  //<<====================== File1
        dt = ds.Tables[0];
        if (dt.Rows.Count > 0)
        {//Opening bracket 3
            int i = 0;
            foreach (DataRow Dr in dt.Rows)
            {//Opening bracket 4
                ListViewItem lv = new ListViewItem(dt.Rows[i]["ID"].ToString());
                lv.SubItems.Add(dt.Rows[i]["Name"].ToString());
                lv.SubItems.Add(dt.Rows[i]["group"].ToString());
                lv.SubItems.Add(dt.Rows[i]["document"].ToString());
                i++;
                listView1.Items.Add(lv);


            }//Closing bracket 4
        }//Opening bracket 3
    }//Closing Bracket 2
    if (comboBox1.SelectedIndex == 1)  //Option 2
    {
         DataSet ds1 = new DataSet();
         DataTable dt1 = new DataTable();
         ds1.ReadXml(@"C:\XMLFile1.xml");     //<<====================== still File1
         dt1 = ds1.Tables[0];
         if (dt1.Rows.Count > 0)
         {
             int i = 0;
             foreach (DataRow qw in dt1.Rows)
             {
                 ListViewItem lv = new ListViewItem(dt1.Rows[i]["ID"].ToString());
                 lv.SubItems.Add(dt1.Rows[i]["Name"].ToString());
                 lv.SubItems.Add(dt1.Rows[i]["group"].ToString());
                 lv.SubItems.Add(dt1.Rows[i]["document"].ToString());
                 i++;
                 listView1.Items.Add(lv);



            }
        }
    }
}//Closing Bracket 1

编辑:

  1. 计算您的开始和结束括号
  2. 您只是添加到列表视图listView1.Items.Add(lv)。除非您删除旧条目,否则新条目将显示在列表底部或混合使用。