`

使用DataGrid动态绑定DropDownList

阅读更多
简单的使用模板列绑定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默认选中。(注释:为了方便大家学习,我给出一个简单代码的例子,供大家参考)

<asp:datagrid allowpaging="True" borderwidth="1px" bordercolor="#3366CC" autogeneratecolumns="False" id="DataGrid1" borderstyle="None" backcolor="White" datakeyfield="bdid" width="100%" cellpadding="4" runat="server"><!----><asp:templatecolumn headertext="标记"><asp:dropdownlist id="DDLBJ" runat="server"><asp:templatecolumn headertext="标记"><asp:templatecolumn headertext="标记">          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))</asp:templatecolumn></asp:templatecolumn></asp:dropdownlist></asp:templatecolumn><asp:datagrid allowpaging="True" borderwidth="1px" bordercolor="#3366CC" autogeneratecolumns="False" borderstyle="None" backcolor="White" datakeyfield="bdid" width="100%" cellpadding="4" runat="server"><asp:templatecolumn headertext="标记"><asp:dropdownlist runat="server"><asp:templatecolumn headertext="标记"><asp:templatecolumn headertext="标记">  
         {
              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;
             }
            }</asp:templatecolumn></asp:templatecolumn></asp:dropdownlist></asp:templatecolumn></asp:datagrid></asp:datagrid>

<asp:datagrid allowpaging="True" borderwidth="1px" bordercolor="#3366CC" autogeneratecolumns="False" borderstyle="None" backcolor="White" datakeyfield="bdid" width="100%" cellpadding="4" runat="server"></asp:datagrid><asp:templatecolumn headertext="标记"></asp:templatecolumn><asp:dropdownlist runat="server"></asp:dropdownlist><asp:templatecolumn headertext="标记"></asp:templatecolumn><asp:templatecolumn headertext="标记">            private string BindDDL(int ddd)
            {
             string sss = "";
             if (ddd==1)
             {
              sss="张三";
              return sss;
             }
             else
             {
                 sss="李四";
              return sss;
             }
            }
</asp:templatecolumn>

   注释:msg为一个类似WinForm的messagebox对话框,不必理会。可以使用label.Text代替

分享到:
评论

相关推荐

    asp.net 自定义DropDownList(CustomDropDownList)(上一个有点bug)

    用DataGrid基于jQuery(实际上只用了它的按照类查找元素一个方法)写的自定义DropDownList,解决不能控制生成后的Select html控件不能控制zIndex而遮挡Div层及其他复杂样式,没有封装成dll,压缩包里是例子及所有用到...

    asp.net专家疑难解答200问源码

    113.如何控制DataGrid绑定列绑定的数据类型为日期的显示格式 114.如何把DataGrid中某一列的统计值显示在页脚中 115.如何在DataGrid中让不同条件的行显示不同的背景颜色 116.如何在DataGrid中实现分页功能 117....

    asp.net专家疑难解答200问

    如何控制DataGrid绑定列绑定的数据类型为日期的显示格式 114.如何把DataGrid中某一列的统计值显示在页脚中 115.如何在DataGrid中让不同条件的行显示不同的背景颜色 116.如何在DataGrid中实现分页功能 ...

    DataGrid同时具有分页和排序功能及注意点

    当DataGrid同时具有分页和排序功能时应注意在重新绑定数据源时,MyDataGrid.CurrentPageIndex=0;下面给实现以上功能的原码,也就不多缀了aspx中包含有DataGrid和控制其数据源变化的dropdownlistDataGrid代码 &lt;...

    ASP.net技术内幕

    11.3.7 使用模板编辑DataGrid控件中的条目 11.4 小结 &lt;br/&gt;第12章 操作DataSet &lt;br/&gt;12.1 理解DataSet 12.1.1 DataSet的元素 12.1.2 向DataSet添加DataTable 12.1.3 将控件绑定...

    asp.net技术内幕(1)

    11.3.2 对DataGrid使用样式 11.3.3 对DataGrid控件中的列进行排序 11.3.4 对DataGrid中的记录进行分页 11.3.5 选择DataGrid控件中的行 11.3.6 编辑DataGrid控件中的条目 11.3.7 使用模板...

    asp.net技术内幕(2)

    11.3.2 对DataGrid使用样式 11.3.3 对DataGrid控件中的列进行排序 11.3.4 对DataGrid中的记录进行分页 11.3.5 选择DataGrid控件中的行 11.3.6 编辑DataGrid控件中的条目 11.3.7 使用模板...

    asp.net技术内幕(5)

    11.3.2 对DataGrid使用样式 11.3.3 对DataGrid控件中的列进行排序 11.3.4 对DataGrid中的记录进行分页 11.3.5 选择DataGrid控件中的行 11.3.6 编辑DataGrid控件中的条目 11.3.7 使用模板...

    asp.net技术内幕(4)

    11.3.2 对DataGrid使用样式 11.3.3 对DataGrid控件中的列进行排序 11.3.4 对DataGrid中的记录进行分页 11.3.5 选择DataGrid控件中的行 11.3.6 编辑DataGrid控件中的条目 11.3.7 使用模板...

    asp.net技术内幕(3)

    11.3.2 对DataGrid使用样式 11.3.3 对DataGrid控件中的列进行排序 11.3.4 对DataGrid中的记录进行分页 11.3.5 选择DataGrid控件中的行 11.3.6 编辑DataGrid控件中的条目 11.3.7 使用模板...

    ASP.NET应用与开发案例教程

    1.1.2动态网站设计技术 1.1.3ASP.NET的介绍 1.1.4ASP.NET和ASP的对比 1.2运行环境配置 1.2.1ASP.NET运行环境介绍 1.2.21lS的安装和配置 1.2.3安装Microsoft.NETFrameworkSDK 1.2.4VisualStudio.NET的简介 1.3从Hello...

    ASP.NET应用开发案例教程

    1.1.2 动态网站设计技术 1.1.3 ASP.NET的介绍 1.1.4 ASP.NET和ASP的对比 1.2 运行环境配置 1.2.1 ASP.NET运行环境介绍 1.2.2 lis的安装和配置 1.2.3 安装Microsoft.NET Framework SDK ...

    AspNetPager.dll

    当使用下拉框显示页索引时(即PageIndexBoxType=DropDownList时),如果总页数超过80页,将在下拉框中只列出部分页索引而不是全部页索引,以防页数太多导致下拉框选项太多而拖慢页面下载和打开速度。页索引下拉框的...

    ASP.NET程序中常用的三十三种代码.txt

     不要使用Response.Redirect,而应该使用Server.Transfer  e.g // in global.asax protected void Application_Error(Object sender, EventArgs e) { if (Server.GetLastError() is HttpUnhandledException) Server...

    ASP.NET常用代码

    不要使用Response.Redirect,而应该使用Server.Transfer e.g // in global.asax protected void Application_Error(Object sender, EventArgs e) { if (Server.GetLastError() is HttpUnhandledException) Server....

Global site tag (gtag.js) - Google Analytics