如何在GridView中绑定下拉菜单? [英] How to bind dropdown in a gridview?

查看:189
本文介绍了如何在GridView中绑定下拉菜单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将另一个数据表中的数据绑定到位于网格视图中的下拉控件中.

I want to bind the data from the other datatable in a dropdown control which is in the gridview.

推荐答案

网格视图有一个名为gridview1_rowdatabound的事件.按照以下代码:


Grid view has an event called gridview1_rowdatabound. Follow code as :


protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {

            DropDownList ddl = (DropDownList)e.Row.FindControl("DropDownList1");
            // then bind the ddl
        }
    }




您可以使用GridView RowDataBound 事件来绑定下拉列表,也可以使用简单的Foreach 循环来迭代并绑定
Hi,

You can use either GridView RowDataBound event to bind dropdown or you can use a simple Foreach loop to iterate and bind each dropdown in a
GridView<br />

中的每个下拉列表.如果使用RowDataBound事件,请在* .aspx页上

. If you use RowDataBound Event, On *.aspx page

<asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound">
</asp:GridView>


在* .aspx.cs上,请在下面使用此代码


and on *.aspx.cs use this code below

void GridView1_RowDataBound(Object sender, GridViewRowEventArgs e)
{
  if(e.Row.RowType == DataControlRowType.DataRow)
  {
    DropDownList  drdList=(DropDownList)e.Row.FindControl("DropDownList1");
  }
}


或者,您也可以在GridView1上的DataBinding之后使用以下代码.


Or alternatively you can use the code below after DataBinding on GridView1

DropDownList drdList;
foreach (GridViewRow grdRow in GridView1.Rows)
{
    /// Nested DropDownList Control reference is passed to the DrdList object. 
    ///This will allow you access the properties of dropdownlist placed inside the GridView Template column.
    drdList = (DropDownList)( GridView1.Rows[ grdRow.RowIndex ].Cells[1].FindControl( "DropDownList1" ));

    // DataBinding of nested DropDownList Control for each row of GridView.
    drdList.DataSource = myDataSet;
    drdList.DataValueField = "ColumnName";
    drdList.DataTextField = "ColumnName";
    drdList.DataBind();
}


您可以在此处 [ ^ ]
希望这会有所帮助.


You will find more details here[^]
Hope this will help.


我认为您应该为此使用Data Set.
在数据集上,添加一个数据表.完成添加后.
然后添加另一个Query.写这个

WHERE column name=@column name
然后重建您的代码.

之后,从下拉菜单中选择一个数据源.
选择objectdatasource

选择您首先在数据集上创建的一个.
然后从那里选择要控制的对象.


然后从您的gridview中.
选择第二个.


-CG-
i think you should use a Data Set for that.
on the Data set, add a datatable. when you finish adding.
add another Query then from that. Write this

WHERE column name=@column name
then rebuild your code.

after that, choose a datasource from your dropdown.
choose objectdatasource

select the one you first created on your dataset.
and from there you will select from which you want to control.


then from your gridview.
select the second one.


-CG-


这篇关于如何在GridView中绑定下拉菜单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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