Hello
I have some doubts in listveiw
I have a listview with two columns, i want to add items to the first column which are given programmatically and the other column is from database... like

column1         column2
apple        from database
orange            "
banana            "
so on             "

can this be done....
can any one help me out with this...

thanks
vince

Edited 5 Years Ago by vincezed: n/a

Hi, you can do it this way:

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            listView1.Columns.Add("column 1", -2, HorizontalAlignment.Left);
            listView1.Columns.Add("column 2", -2, HorizontalAlignment.Left);
            listView1.View = View.Details;

            PopulateListView();
        }

        private void PopulateListView()
        {
            DataTable table = GetDataFromDB();
            for (int i = 0; i < table.Rows.Count; i++)
            {
                ListViewItem lvi = new ListViewItem();
                lvi.Text = "your data " + (i + 1);
                lvi.SubItems.Add(table.Rows[i]["col1"].ToString());
                listView1.Items.Add(lvi);
            }
        }

        private DataTable GetDataFromDB()
        {
            DataTable table = new DataTable("myTable");
            //get your own data from db, with using sqlDataAdpater to fill DataTable!!

            //I will only show an example of creating my own column and some rows!
            table.Columns.Add("col1", typeof(string));
            table.Rows.Add("item 1");
            table.Rows.Add("item 2");
            table.Rows.Add("item 3");
            return table;
        }
    }

Hi Mitja thanks for ur reply,... u see i am new to listview and i did not get it... how i add the column1 data..... sorry for distrubing u....

thanks
vince

Hi,
1st you create a new instance of ListViewItem class.
Then you use this instance and a Text property (this one is for the 1st column).
If oyu want want to add other columns (subitems) you have to use instance property and SubItems.Add() method:

ListViewItem lvi = new ListViewItem();
lvi.Text = "1st column data";
lvi.SubItems.Add("2nd column data");
lvi.SubItems.Add("3nd column data");
//and so on...

//and add items and subItems to listView
listView1.Items.Add(lvi);

Instead of 1st two rows of code you can do:

ListViewItem lvi = new ListViewItem("1st column data"); //its the same
//you add item to the 1st item (that means to 1st column)

Hope this helps explaining how to add items to listView, and usig Items and SubItems.

Sorry to disturbe u again.. in my listview the first column is a string and the second column is a double... i tried my way but it is showing this error....

Input string was not in a correct format.Couldn't store <InitTempValue> in InitTempValue Column. Expected type is Double.

In this InitTempValue is the column name which i am storing in InitTempValue of the listview....

i tried converting the column to double it is showing the same error.. i am stuck here from many days.. pls help

thanks
vince

listView only shows strings, so if you want to convert those values form 2nd column to double they have to be double types, otherwise you will get that error.
Would you show me some more code to help you out?

string[] rowData = {"Power",
                            "Temp",                           
                            "Heat",                           
                            "Area"};

          private void PopulateListView()
           {
              DataTable table = GetDataFromDB();
              for (int i = 0; i < table.Rows.Count; i++)
              {
                ListViewItem lvi = new ListViewItem();
                //lvi.Text = "your data " + (i + 1);
                lvi.Text = rowData[i];
                lvi.SubItems.Add(table.Rows[i]["col1"].ToString());
                listView2.Items.Add(lvi);
              }
           }
                 
           private DataTable GetDataFromDB()
           {
               DBClass db = new DBClass();
               SqlCeConnection datCon = db.getConnection();

               string str = "select * from database";
               SqlCeDataAdapter a = new SqlCeDataAdapter(str , datCon);


               DataTable table = new DataTable();
               a.Fill(table);

               table.Columns.Add("col1", typeof(String));             
               table.Rows.Add("InitTempValue");
               table.Rows.Add("SpecTempValue");
              
               return table;
           }

this is the code u gave... this is the one i am using ...
help me out of this.
thanks
vince

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