combobox綁定數(shù)據(jù)庫與datagridview綁定數(shù)據(jù)庫

          1.combobox綁定數(shù)據(jù)庫
             在面頁加載事件中綁定:
            SqlConnection connection = new SqlConnection(connectionString)
            DataSet ds = new DataSet();
                          try
                          {
                              connection.Open();
                              SqlDataAdapter command = new SqlDataAdapter(SQLString, connection);
                              command.Fill(ds, "ds");
                          }
                          catch (System.Data.SqlClient.SqlException ex)
                          {
                              throw new Exception(ex.Message);
                          }
            this.combobox1.DataSource = ds.Tables[0];
                this.combobox1.ValueMember = "字段名";//下拉框綁定的值,一般與displaymember一一對應(yīng);
                this.combobox1.DisplayMember = "字段名"; //下拉框中顯示的值。 
            
            this.combobox1.SelectedValue.ToString();//這是下拉框中的值被選定后,獲得的被選項。

          combobox1.DropDownStyle = ComboBoxStyle.DropDownList;//限制combobox的內(nèi)容不能被用戶編輯,只能從下拉表中選

          2.datagridview綁定數(shù)據(jù)庫

            SqlConnection connection = new SqlConnection(connectionString)
            DataSet ds = new DataSet();
                          try
                          {
                              connection.Open();
                              SqlDataAdapter command = new SqlDataAdapter(SQLString, connection);
                              command.Fill(ds, "ds");
                          }
                          catch (System.Data.SqlClient.SqlException ex)
                          {
                              throw new Exception(ex.Message);
                          }

            this.dataGridView1.DataSource = ds;
                      dataGridView1.AutoGenerateColumns = false;
                      dataGridView1.DataMember = ds.Tables[0].TableName;
                      dataGridView1.ReadOnly = true;
          然后在datagridview的編輯中添加顯示的列名,并將每列的datapropertyname屬性與數(shù)據(jù)庫中的字段一一對應(yīng)(綁定)。

          3.當(dāng)下拉框中的內(nèi)容發(fā)生改變時引的的事件一般是selectionchangecommitted事件。

          posted on 2012-04-17 09:54 SkyDream 閱讀(1262) 評論(0)  編輯  收藏 所屬分類: C# WinForm

          <2012年4月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          導(dǎo)航

          統(tǒng)計

          常用鏈接

          留言簿(3)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 健康| 苍南县| 盱眙县| 铅山县| 涟源市| 察哈| 历史| 普定县| 临泉县| 五常市| 日土县| 新乡县| 六盘水市| 巴东县| 金昌市| 神池县| 南召县| 乌鲁木齐市| 板桥市| 白城市| 当阳市| 筠连县| 社会| 和顺县| 新蔡县| 沂南县| 平南县| 冕宁县| 曲松县| 湾仔区| 齐河县| 于都县| 崇明县| 友谊县| 和静县| 合山市| 大同市| 简阳市| 新野县| 门源| 夏津县|