Zdravím, mám jeden dotaz, jelikož na komponentu LV nejde použít wizard jako na DGW, chtěl bych se zeptat, jak postupovat při vypisování jednotlivých řádku z DataTable, viz kód:

Hello, i have got one questions, because with ListView component i cannot use the Wizard, i would like to ask, how to writeout all rows stored in database, resp.DataTable into List View component.

See code:

Thanks for any advice and sorry for my English..

// Load Data from the DataSet into the ListView
            SqlConnection conn = new SqlConnection(@"
            server = .\SQLEXPRESS;
            integrated security = true;
            database = Phone_directory");

            conn.Open();

            
            string sqlIns = @"SELECT * FROM tbl_phonebook";

            //create query 
            SqlCommand cmdQry = new SqlCommand(sqlIns, conn);


            SqlDataAdapter da = new SqlDataAdapter(cmdQry);

            DataSet ds = new DataSet();

            da.Fill(ds, "tbl_phonebook");

    // Get the table from the data set
            DataTable dtable = ds.Tables["tbl_phonebook"];

    // Clear the ListView control
    listViewUsers.Items.Clear();
    // Add a column with width 20 and left alignment.
    listViewUsers.Columns.Add("mobile_phone", 100, HorizontalAlignment.Left);
    listViewUsers.Columns.Add("name", 100, HorizontalAlignment.Left);


    // Display items in the ListView control
    foreach (DataRow row in dtable.Rows)
    {

    [B]   WHAT TYPE HERE? [/B]
    }

>How to fill rows in ListView from DataRow?

DataTable dt = new DataTable();
            dt.Columns.Add("No");
            dt.Columns.Add("Name");
            dt.Rows.Add("1","AA");
            dt.Rows.Add("2","BB");
            dt.Rows.Add("3","CC");
            dt.Rows.Add("5","DD");

            listView1.Columns.Add("No");
            listView1.Columns.Add("Name");

            foreach (DataRow row in dt.Rows)
            {
                string []ar=Array.ConvertAll<object,string>(row.ItemArray,p=>p.ToString());
                listView1.Items.Add(new ListViewItem(ar));
            }

adatapost gave you exactly what I would have done.
But if you really want to know what to put in that space you have tagged as "WHAT TYPE HERE?"

// Display items in the ListView control
    foreach (DataRow row in dtable.Rows)
    {

       //WHAT TYPE HERE? 
      listViewUsers.Items.Add( new ListViewItem(new string[]{
              row["mobile_phone"].ToString() ,
              row["name"].ToString() }));

    }
commented: Cool! +6
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.