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");


            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
    // 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();


            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() }));

This article has been dead for over six months. Start a new discussion instead.