C# 如何将数据表中的项目添加到列表视图?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18584707/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 12:38:23  来源:igfitidea点击:

How to add items in a datatable to a listview?

c#sqllistview

提问by Cindrella

I am currently developing a C# Windows Form Application.

我目前正在开发 C# Windows 窗体应用程序。

Now I am trying to use a SQL Command to retrieve information from the database to fill in the information that I need to have in my Application.

现在我正在尝试使用 SQL 命令从数据库中检索信息以填写我的应用程序中需要的信息。

A sample query would be "select * from Location"

示例查询将是“从位置选择 *”

In the Location table there would be variables like locationId, LocationName , districId etc etc. I used the following code

在 Location 表中会有 locationId、LocationName、districId 等变量。我使用了以下代码

private void button1_Click(object sender, EventArgs e)
{
    SqlConnection con = new SqlConnection("connectionstring");
    SqlDataAdapter ada = new SqlDataAdapter("select * from MasterLocation", con);
    DataTable dt = new DataTable();
    ada.Fill(dt);

    for (int i = 0; i < dt.Rows.Count; i++)
    {
        DataRow dr = dt.Rows[i];
        ListViewItem listitem =new ListViewItem(dr["pk_Location_ID"].ToString());
        listitem.SubItems.Add(dr["var_Location_Name"].ToString());
        listitem.SubItems.Add(dr["fk_int_District_ID"].ToString());
        listitem.SubItems.Add(dr["fk_int_Company_ID"].ToString());
       listView1.Items.Add(listitem);
    } 

The output is:

输出是:

like this.

像这样。

but it should be like this:

但它应该是这样的:

sql output

sql输出

采纳答案by Shyam sundar shah

you have to change some code

你必须改变一些代码

private void button1_Click(object sender, EventArgs e)
{
    listView1.View = View.Details;
    SqlConnection con = new SqlConnection("connectionstring");
    SqlDataAdapter ada = new SqlDataAdapter("select * from MasterLocation", con);
    DataTable dt = new DataTable();
    ada.Fill(dt);

    for (int i = 0; i < dt.Rows.Count; i++)
    {
        DataRow dr = dt.Rows[i];
        ListViewItem listitem = new ListViewItem(dr["pk_Location_ID"].ToString());
        listitem.SubItems.Add(dr["var_Location_Name"].ToString());
        listitem.SubItems.Add(dr["fk_int_District_ID"].ToString());
        listitem.SubItems.Add(dr["fk_int_Company_ID"].ToString());
       listView1.Items.Add(listitem);
    } 

回答by Cindrella

enter image description here

在此处输入图片说明

Added the following code

添加了以下代码

listView1.View = View.Details;

and it worked.

它奏效了。

回答by RameezAli

 private void FormView_Load(object sender, EventArgs e)
  {
    sample = new DataTable(); //Sample Data
            sample.Columns.Add("id", typeof(string));
            sample.Columns.Add("name", typeof(string));
            sample.Rows.Add("1", "apple");
            sample.Rows.Add("2", "acer");
            sample.Rows.Add("3", "alpha");
            sample.Rows.Add("4", "beat");
            sample.Rows.Add("5", "ball");
            sample.Rows.Add("6", "cat");
            sample.Rows.Add("7", "catch");
            sample.Rows.Add("10", "zebra");

            listViewEx1.View = View.Details;
            listViewEx1.Columns.Add("id");
            listViewEx1.Columns.Add("name");
  }



         listViewEx1.Items.Clear();

            listViewEx1.FullRowSelect = true;

            foreach (DataRow row in sample.Rows)
            {
                    ListViewItem item = new ListViewItem(row["id"].ToString());
                    item.SubItems.Add(row["name"].ToString());
                    listViewEx1.Items.Add(item); //Add this row to the ListView
             }