Listview combobox examples using C#.NET.

LISTVIEW COMBO BOX C#

C# Tutorials

  

LISTVIEW COMBO BOX C#

Learn Tutorials
Listview combobox examples using C#.NET.

Example code for Listview combo box C#:
using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;


/* sample use of:
   
dbm=new DatabaseManager("servername","Notebook","notebook","notebookSQL");

      DataSet ds = dbm.getDataSet("Select * FROM data","data");
         foreach (DataRow dr in ds.Tables["Data"].Rows)
         {
            mainTextBox.Text=dr["content"].ToString();
            titleTextBox.Text=dr["title"].ToString();
         }



      string queryStringToSubmit=String.Format(
               "UPDATE data"+
               " SET title='{0}',"+
               "   content='{1}'"+
               
               " WHERE ID={2}",titleToInsert,mainTextToInsert,currentShowingContentID);//,currentCategory
            
         dbm.submitNonQuery(queryStringToSubmit);

   -----------------------------------------------------
   populating combo box:
      
      private bool repopulateRecentItemsComboBox()
      {
         recentItemsComboBox.Items.Clear();

         DataSet ds = dbm.getDataSet("SELECT TOP 15 * FROM data WHERE bDeleted = 'no' ORDER BY dateInserted DESC","data");
         //dump the dataset returned into the comboBox
         foreach (DataRow dr in ds.Tables["data"].Rows)
         {
            DateTime date =(DateTime)dr["dateInserted"];
            string dateString="("+date.Month+"/"+date.Day+") ";//.Month+"/"+date.Day
            recentItemsComboBox.Items.Add(new ComboBoxItem(dateString+dr["Title"].ToString(),(int)dr["id"]));
         }
         return true;
      }
      
   -------------------------------------------      
   populating list view:
      
      private bool repopulateListViewFromDB(string query, string table)
      {
         repopulateRecentItemsComboBox();
         
         DataSet ds = dbm.getDataSet(query,table);
         initializeListViewColumns(bottomListView);
         
         ListViewItem lvi;
         lastQueryUsedForListView=query;
         lastTableUsed=table;


         //dump the dataset returned into the listview
         foreach (DataRow dr in ds.Tables["vw_data_categories"].Rows)
         {
            lvi=new ListViewItem();
            lvi.Text=dr["Title"].ToString();
            lvi.Tag=dr["id"];
            bottomListView.ItemDrag += new System.Windows.Forms.ItemDragEventHandler(this.listViewItem_ItemDrag);
            DateTime date =(DateTime)dr["dateInserted"];
            lvi.SubItems.Add(date.DayOfWeek.ToString());//.Month+"/"+date.Day
            lvi.SubItems.Add(String.Format("{0:M}",date));
            lvi.SubItems.Add(dr["categoryName"].ToString());
            bottomListView.Items.Add(lvi);
         }
         if(ds.Tables["vw_data_categories"].Rows.Count>0)
            return true;
         else
            return false;
      }

      private void initializeListViewColumns(ListView listview)
      {
         listview.Clear();
      
         //mode like in windows explorer
         listview.View = View.Details;
         
         // Add a column with width 20 and left alignment.
         listview.Columns.Add("Title", 220, HorizontalAlignment.Left);
         listview.Columns.Add("Day", 70, HorizontalAlignment.Left);
         listview.Columns.Add("Date", 70, HorizontalAlignment.Left);
         listview.Columns.Add("Category", 100, HorizontalAlignment.Left);
      }

*/

public class DatabaseManager
{

   string server;
   string database;
   string username;
   string password;

   //constructor
   public DatabaseManager(string server, string database, string username, string password)
   {
      this.server=server;
      this.database=database;
      this.username=username;
      this.password=password;
   }


   //use for inserts deletes updates
   public void submitNonQuery(string query)
   {
      SqlConnection connObj=null;
      try
      {
         //create connection obj
         connObj = new SqlConnection();
         string temp=String.Format("server={0};Database={1};uid={2};pwd={3}",server,database,username,password);

         connObj.ConnectionString =    temp;
   
         //create SqlCommand object and set its properties below
         SqlCommand cmd = new SqlCommand();

         //set the sql statement as the CommandText property of the SqlCommand obj
         cmd.CommandText=query;

         //set the Connection property of the SqlCommand obj
         cmd.Connection = connObj;

         //open the connection
         connObj.Open();

         //call the ExecuteReader method of the Command obj to execute the command
         cmd.ExecuteNonQuery();
                  
         //close the Connection obj
         connObj.Close();

      }
      catch( Exception ex )
      {
         string errorText = "ERROR:" + ex.Message +"\n SQL :" + query;
         MessageBox.Show(errorText);
      }
      finally
      {
         // Close the connection
         //if( connObj.State == DBObjectState.Open )
            connObj.Close();
      }




   }



   public DataSet getDataSet(string query, string tableName)
   {
      SqlConnection connObj = new SqlConnection();
      string temp=String.Format("server={0};Database={1};uid={2};pwd={3}",server,database,username,password);

      connObj.ConnectionString =    temp;

      SqlDataAdapter da = new SqlDataAdapter(query,connObj);

      DataSet ds = new DataSet();

      //load the data into the dataset
      da.Fill(ds,tableName);

      return ds;
   }











      /***********************************************
       * the following methods are just dev tests
       *
       *
       *
       * ***********************************************/

   public SqlDataAdapter submitQueryAsDataAdapter(string query)
   {
      //create connection obj
      SqlConnection connObj = new SqlConnection();
      string temp=String.Format("server={0};Database={1};uid={2};pwd={3}",server,database,username,password);

      connObj.ConnectionString =    temp;
   
      SqlDataAdapter adp = new SqlDataAdapter(query,connObj);

      return adp;
   }


   public void accessDB()
   {
      //create connection obj
      SqlConnection connObj = new SqlConnection();
      string temp=String.Format("server={0};Database={1};uid={2};pwd={3}",server,database,username,password);

      connObj.ConnectionString =    temp;
   
      //create SqlDataReader object to receive the data from the database
      SqlDataReader dr;

      //create SqlCommand object and set its properties below
      SqlCommand cmd = new SqlCommand();

      //set the sql statement as the CommandText property of the SqlCommand obj
      cmd.CommandText="SELECT fname,lname FROM testtable";

      //set the Connection property of the SqlCommand obj
      cmd.Connection = connObj;

      //open the connection
      connObj.Open();

      //call the ExecuteReader method of the Command obj to execute the command
      dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);

      //dump the dataset returned
      while (dr.Read())
      {
         Console.WriteLine(dr.GetString(0)+" "+dr.GetString(1));
      }

      //close the Connection obj
      connObj.Close();

   }


   public void submitQueryPopulateListView(string query, ListView listview)
   {
      listview.Clear();
      
      //mode like in windows explorer
      listview.View = View.Details;
         
      // Add a column with width 20 and left alignment.
      listview.Columns.Add("Title", 100, HorizontalAlignment.Left);
      listview.Columns.Add("Day", 100, HorizontalAlignment.Left);
      listview.Columns.Add("Date", 100, HorizontalAlignment.Left);

      //create connection obj
      SqlConnection connObj = new SqlConnection();
      string temp=String.Format("server={0};Database={1};uid={2};pwd={3}",server,database,username,password);

      connObj.ConnectionString =    temp;
   
      //create SqlDataReader object to receive the data from the database
      SqlDataReader dr;

      //create SqlCommand object and set its properties below
      SqlCommand cmd = new SqlCommand();

      //set the sql statement as the CommandText property of the SqlCommand obj
      cmd.CommandText=query;

      //set the Connection property of the SqlCommand obj
      cmd.Connection = connObj;

      //open the connection
      connObj.Open();

      //call the ExecuteReader method of the Command obj to execute the command
      dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);

      ListViewItem lvi;

      //dump the dataset returned
      while (dr.Read())
      {
         lvi=new ListViewItem();
         lvi.Text=dr.GetString(1);
         string dateString =dr.GetDateTime(4).DayOfWeek.ToString();
         lvi.SubItems.Add(dateString);
         lvi.SubItems.Add(dr.GetDateTime(4).Date.ToShortDateString());

         listview.Items.Add(lvi);
      }

      //close the Connection obj
      connObj.Close();
   }


}
This is example code for listview combobox examples using c#.net.