1 Replies - 13391 Views - Last Post: 14 December 2010 - 12:33 AM Rate Topic: -----

#1 lapeci_10  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 10-October 10

How to show data in listview from database?

Posted 13 December 2010 - 08:02 PM

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 the database record 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

This post has been edited by modi123_1: 13 December 2010 - 10:07 PM
Reason for edit:: please use hte code tags..


Is This A Good Question/Topic? 0
  • +

Replies To: How to show data in listview from database?

#2 mavarazo  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 36
  • View blog
  • Posts: 181
  • Joined: 25-October 10

Re: How to show data in listview from database?

Posted 14 December 2010 - 12:33 AM

I propose you to define a class to hold the informations that you read.

(Everything is pseudo-code)

public class Food {
   public String Num { get; set; }
   public String Name { get; set; }
   public String Price { get; set; }
}



Set the List to
List<Food> result...


Define in the listview the columns and fill it up like this:

listView.BeginUpdate();

foreach (Food food in result) {
   ListViewItem lvi = new ListViewItem(food.Num);
   lvi.SubItem.Add(food.Name);
   lvi.SubItem.Add(food.Price);

   listView.Items.Add(lvi);
}

listView.EndUpdate();


This post has been edited by mavarazo: 14 December 2010 - 12:36 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1