使用DataGrid動態綁定DropDownList

來源:互聯網
上載者:User

使用DataGrid動態綁定DropDownList

hbzxf (阿好)
http://www.cnblogs.com/hbzxf

        簡單的使用模板資料行繫結DropDownList,初學者想必都會了,但有時候,我們要做的就是在編輯的時候想讓某一列定製為DropDownList,並且根據正常情況下顯示的值自動變換DropDownList中所選的值,然後儲存選擇後的值到資料庫或XML檔案,其實要做到這樣的功能並不難,只要我們學會使用DataGrid的DataGrid1_ItemDataBound事件就行了,跟我來做個例子。

        //檢索資料庫的函數
          public DataSet GetZcbd()
          {
            try
           {
            DataSet ds=new DataSet();   
            string searchString="select id,yy,bj from zc";
            da=new OleDbDataAdapter(searchString,conn);
            da.Fill(ds,"yy");    
             return ds;
           }
           catch
           {
            return null;    
           }  
          }

         //綁定DataGrid   
        private void BindGrid()
          {
           DataSet ds = new DataSet();
           ds = us.GetZcbd();
           if (ds!=null)
           {
            this.DataGrid1.DataSource = ds;
            this.DataGrid1.DataBind();
           }
           else
           {
            msg.Alert("載入資料錯誤!",Page);
           }
          }
       
        綁定好DataGrid以後,設定模板列,讓其正常顯示下為Label,並綁定為資料庫中一ID值,在編輯狀態下為DropDownList,並綁定為資料庫中一Name值,我們現在要做的就是當我們選擇編輯時根據Label的值自動從資料庫中取出編號為ID值的姓名,並用DropDownList預設選中。(注釋:為了方便大家學習,我給出一個簡單代碼的例子,供大家參考)

          private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
            {
             if (e.Item.ItemType == ListItemType.EditItem)
             {
                 DataRowView drv = (DataRowView)e.Item.DataItem;
              string current = drv["label1"].ToString();
              DropDownList ddl = (DropDownList)e.Item.FindControl("ddl");
              ddl.SelectedIndex = ddl.Items.IndexOf(ddl.Items.FindByValue(current));
             }
             if ((e.Item.ItemType == ListItemType.Item)||(e.Item.ItemType == ListItemType.AlternatingItem))  
         {
              Label t = (System.Web.UI.WebControls.Label)e.Item.FindControl("label1");
              string current = this.BindDDL(int.Parse(t.Text));
              e.Item.Cells[1].Text = current;
             }
            }

            private string BindDDL(int ddd)
            {
             string sss = "";
             if (ddd==1)
             {
              sss="張三";
              return sss;
             }
             else
             {
                 sss="李四";
              return sss;
             }
            }

   注釋:msg為一個類似WinForm的messagebox對話方塊,不必理會。可以使用label.Text代替

代碼很亂,敬請諒解!

2004.4.2


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.