我想将两个下拉列表绑定到网格视图 [英] i want to bind two dropdownlist into grid view

查看:42
本文介绍了我想将两个下拉列表绑定到网格视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将两个DropDownList绑定到ASP.NET中的GridView中.

我确实将一个DropDownListDatabound事件绑定在一起,但是问题是如何在GridView中绑定第二个DropDownList?

一个DropDownList完全与数据库绑定,但另一个不与同一事件或GridView绑定.

我的代码:

I would like to bind two DropDownList into a GridView in ASP.NET.

I did bind one DropDownList with Databound event, but the problem is how can I bind the second DropDownList in the GridView?

One DropDownList completely binds with database, but the other does not bind with the same event or GridView.

My code:

public DataTable FetchCustomerType()
   {
       string sql = "Select Distinct type From cust_info";
       SqlDataAdapter da = new SqlDataAdapter(sql, conn);
       DataTable dt = new DataTable();
       da.Fill(dt);
       return dt;
   }

   public DataTable FetchCustomerGender()
   {
       string sql = "Select Distinct gender From cust_info";
       SqlDataAdapter da = new SqlDataAdapter(sql, conn);
       DataTable dt = new DataTable();
       da.Fill(dt);
       return dt;
   }

   protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
   {
       if (e.Row.RowType == DataControlRowType.DataRow)
       {
           DropDownList cmbtype = (DropDownList)e.Row.FindControl("cmbType");
           DropDownList cmbgender = (DropDownList)e.Row.FindControl("cmbGender");
           if (cmbtype != null && cmbgender !=null)
           {
               cmbtype.DataBind();
               cmbgender.DataBind();
               cmbtype.DataSource = FetchCustomerType();
               cmbgender.DataSource = FetchCustomerGender();
               cmbtype.SelectedValue = GridView1.DataKeys[e.Row.RowIndex].Values[1].ToString();
               cmbgender.SelectedValue = GridView1.DataKeys[e.Row.RowIndex].Values[2].ToString();
           }
       }



       if (e.Row.RowType == DataControlRowType.Footer)
       {
           DropDownList cmbnewtype = (DropDownList)e.Row.FindControl("cmbNewType");
           DropDownList cmbnewgnder =(DropDownList)e.Row.FindControl("cmbNewGender");
           cmbnewtype.DataSource = FetchCustomerType();
           cmbnewgnder.DataSource = FetchCustomerGender();
           cmbnewtype.DataBind();
           cmbnewgnder.DataBind();
       }

   }


我应该将其他DropDownList编码放在哪里才能正确执行此代码?

谢谢.


Where should I put the other DropDownList coding into this code which executes properly?

Thank you.

推荐答案


您的代码很好,但需要对RowataBound事件进行一些改动
如下
Hi,
your code is fine but need some chanages into RowataBound Event
which is as below
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            DropDownList cmbtype = (DropDownList)e.Row.FindControl("cmbType");
            DropDownList cmbgender = (DropDownList)e.Row.FindControl("cmbGender");
            if (cmbtype != null && cmbgender !=null)
            {
                cmbtype.DataSource = FetchCustomerType();
cmbtype.DataTextField = "type";
cmbtype.DataValueField = "type";
                cmbtype.DataBind();
                cmbtype.SelectedValue = GridView1.DataKeys
e.Row.RowIndex].Values[1].ToString();

    cmbgender.DataSource = FetchCustomerGender();
    cmbnewgnder.DataTextField = "gender";
    cmbnewgnder.DataValueField = "gender";

    cmbgender.DataBind();         
                cmbgender.SelectedValue = GridView1.DataKeys[e.Row.RowIndex].Values[2].ToString();
            }
        }
}


这篇关于我想将两个下拉列表绑定到网格视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆