日新阁

取日三省之意,记生活珠玑,每日清新,谓日新阁.

 

将数据填充到下拉列表

 从数据库提取数据,封装进DataSet,返回后,循环加载到combox中,涉及到的通用代码:

public static void BindComboBox(DataTable table ,ComboBox comTemp)
  {
   try
   {
    comTemp.Items.Clear();
    for (int i=0;i<table.Rows.Count;i++)
    {
     string display=table.Rows[i][0].ToString();  //[filed]
     comTemp.Items.Add(display);
    }
    comTemp.SelectedIndex=-1;
   }
   catch (Exception err)
   {
    throw err;
   }
  }

 #region/***查询字段***/
  /// <summary>
  /// 查询现有关键字段数据
  /// </summary>
  /// <param name="mdbFullName">数据库全路径</param>
  /// <param name="tablename">表名</param>
  /// <param name="title">字段名</param>
  /// <returns>DataSet</returns>
  public DataSet  addFieldData(string mdbFullName,string tablename,string title)
  {
   DataSet  dataset=null;
   DatabaseOp dataOp = null;

   StringBuilder sqlStr=new StringBuilder();
   sqlStr.Append("select  ");
   sqlStr.Append(title); 
   sqlStr.Append(" from ").Append(tablename);
   sqlStr.Append(" group by ").Append(title);
   string selectSQL=sqlStr.ToString();

   try
   {
    dataOp=new DatabaseOp(mdbFullName);
    dataset=dataOp.GetDataSet(selectSQL);

   }
   catch (Exception ex)
   {
    throw ex;
   }
   finally
   {
    dataOp.Close();
   }
   return dataset;
  }
  #endregion

posted on 2007-06-06 17:26  flashicp  阅读(382)  评论(0编辑  收藏  举报

导航