Click here to Skip to main content
15,907,906 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear People

How can I display data in listview from database. What I have done so far is. Here is my code...

public partial class DtposMDIParentSystem : Form
    {
        List<object[]> result = new List<object[]>();
        
        public DtposMDIParentSystem()
        {
            InitializeComponent();

            //create the database connection
            OleDbConnection aConnection = new
OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\AP_AE\Desktop\DTPOS_APP\DataBase\DtposDatabase.accdb;");

            //create the command object and store the sql query
            OleDbCommand aCommand = new OleDbCommand("SELECT * FROM Food", aConnection);

            try
            {
                aConnection.Open();

                //create the datareader object to connect to table
                OleDbDataReader reader = aCommand.ExecuteReader();

                int i = 0;
                while (reader.Read())
                {
                    result.Add(new Object[reader.FieldCount]);
                    reader.GetValues(result[i]);
                }
                reader.Close();
                aConnection.Close();
            }
            catch (InvalidOperationException ex)
            {
                MessageBox.Show("Invalid Masseage = " + ex.Message);
            }
            
        }

         private void cmdOlives_Click(object sender, EventArgs e)
        {
            if (result.Count > 0)
	            {
	                string temp = "";
                    
	                for (int i = 0; i < result[1].Length; i++)
	                {
	                    temp += result[1][i] + "     ";
	                }
	                TableOrderListView.Items.Add(temp);
	            }
        }
}

My question is when I click cmdOlives_Click event button I want to display in my listview such as:

-----------------------------
Num Name Price
-----------------------------
1 Olives 3.95


-----------------------------

Any help would be very very greatfull...

Thanks in advance


Kind regards

Agron
Posted
Updated 13-Dec-10 15:46pm
v2

 
Share this answer
 
v2
Comments
LAPEC 13-Dec-10 22:14pm    
Hi Mark

Its me again, thanks for responding to my question, but I didnt find any help that you sugested.
If you could show me with a sample of code it would be greatfull, I'm really strugling with it...
Thnaks again in advance


kind regards

Agron
I can not understand your problem
but just try this

ListViewItem is the better way for adding items in ListView controls.
First add Item and Suuitems in the respective ListViewItem Control after the adding you just add listViewItem to ListView Control like,
ListViewItem listViewItem;
listViewItem.Item.Add("....");
listView.SubItems.Add("....");
....
....  
TableOrderListView.Items.Add(listViewItem);
 
Share this answer
 
v2
Comments
Toniyo Jackson 14-Dec-10 1:35am    
Always put your code inside code block.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900