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一一對應;
                this.combobox1.DisplayMember = "字段名"; //下拉框中顯示的值。 
            
            this.combobox1.SelectedValue.ToString();//這是下拉框中的值被選定后,獲得的被選項。

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

          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ù)庫中的字段一一對應(綁定)。

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

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

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

          導航

          統(tǒng)計

          常用鏈接

          留言簿(3)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 宁都县| 拜城县| 泰来县| 宾川县| 汉寿县| 高台县| 高州市| 刚察县| 金湖县| 夹江县| 彝良县| 那曲县| 泸定县| 纳雍县| 墨脱县| 黔江区| 荔浦县| 郑州市| 塔河县| 平陆县| 武邑县| 宁明县| 长丰县| 同德县| 虞城县| 灯塔市| 根河市| 安龙县| 绥棱县| 钟山县| 和顺县| 都江堰市| 蛟河市| 永平县| 原平市| 怀宁县| 砀山县| 武陟县| 田阳县| 石棉县| 雷州市|