在文本框上传递ListView项

时间:2016-11-20 16:22:20

标签: c# listview

我有这个代码并且错误,对象引用没有设置为对象的实例。

  private void updateRecordToolStripMenuItem_Click(object sender, EventArgs e)
    {
        foreach (ListViewItem list in lvViewEmp.Items)
        {
            Employee ss = new Employee();
            ss.Show();
            frmEmp.txtEmpID.Text = lvViewEmp.SelectedItems[0].Text;
        }
    }

这是获取数据的代码。

        ConnectionStringSettings conSettings = ConfigurationManager.ConnectionStrings["MyConnection"];
        string MyConnection = conSettings.ConnectionString;
        con = new SqlConnection(MyConnection);

        cmd = con.CreateCommand();
        cmd.CommandText = "Select * From tbl_emp_info";
        sda = new SqlDataAdapter(cmd);
        ds = new DataSet();
        dt = new DataTable();
        sda.Fill(ds);
        sda.Fill(dt);
        if (con.State != ConnectionState.Open)
        {
            con.Open();
            {
                lvViewEmp.View = View.Details;

                foreach (DataColumn c in ds.Tables[0].Columns)
                {

                    ColumnHeader h = new ColumnHeader();
                    h.Text = c.ColumnName;
                    h.Width = 90;

                    lvViewEmp.Columns.Add(h);
                }

                string[] str = new string[ds.Tables[0].Columns.Count];
                foreach (DataRow rr in dt.Rows)
                {
                    for (int col = 0; col <= ds.Tables[0].Columns.Count - 1; col++)
                    {
                        str[col] = rr[col].ToString();
                    }
                    ListViewItem ii;
                    ii = new ListViewItem(str);
                    lvViewEmp.Items.Add(ii);
                    lvViewEmp.GridLines = true;
                    lvViewEmp.FullRowSelect = true;
                    con.Close();
                }
            }
        }

如何通过其他表单上的文本框传递列表视图中的所有数据?

0 个答案:

没有答案