在gridview中显示下拉列表的数据 [英] showing data of dropdownlist in the gridview

查看:92
本文介绍了在gridview中显示下拉列表的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在表单中有2个下拉列表和1个gridview,我们如何在gridview中显示依赖于单个下拉列表的selectedindexedchanged事件的数据

只有单个下拉列表可以一次工作但是我们当我们选择第一个下拉列表然后显示将取决于第一个下拉列表的数据时,以及当我们选择第二个下拉列表然后显示将依赖于下拉列表的数据时,需要两个下拉列表以

的形式工作

我在同一桌面的gridview中有2个下拉菜单

我只获得第一个下拉列表数据和第二个下拉列表无效

解决方案

< blockquote> Default.aspx

//我在gridview fst中有两个下拉状态,对于国家/地区来说,状态为秒。

//在加载绑定上country ddl to country list and state dropdownlist to state list

// on fst ddl click我绑定它状态

//关于状态下拉单击我绑定它country to fst ddl



 <   asp: GridView     ID   =  grd    runat   =  server    AutoGenerateColumns   =  False  

onrowdatabound = grd_RowDataBound



< Columns >

< asp:TemplateField HeaderText = 国家/地区 >
< ItemTemplate >
< asp:DropDownList ID = DropDownList1 OnSelectedIndexChanged = DropDownList1_SelectedIndexChanged 宽度 = 200px AutoPostBack = true EnableViewState = true runat = server >

< / asp:DropDownList >
< / ItemTemplate >
< / asp:TemplateField >
< asp:TemplateField HeaderText = 状态 >
< ItemTemplate >
< asp:DropDownList ID = DropDownList2 OnSelectedIndexChanged = DropDownList2_SelectedIndexChanged 宽度 = 200px AutoPostBack = true EnableViewState = true runat = server >

< / asp:DropDownList >
< / ItemTemplate >
< / asp:TemplateField >
< /列 >
< / asp:GridView >



< br $> b $ b



Default.cs


//我使用edmx绑定DropDown在网格



保护无效DropDownList1_SelectedIndexChanged(对象仙,EventArgs的)
{
如果(Page.IsPostBack)
{
var ddl =(DropDownList)sen;

GridViewRow row =(GridViewRow)ddl.Parent.Parent;
DropDownList ddl2 =(DropDownList)row.FindControl(DropDownList2);
int idx = row.RowIndex;
if(ddl.SelectedIndex> 0)
{
ddl2.Items.Clear();
var x = ddl.SelectedValue;
int id = Convert.ToInt32(x);
TestEntities en = new TestEntities();
ddl2.DataSource = en.State.Where(s => s.Country.CountryID == id).ToList();
ddl2.DataTextField =StateName;
ddl2.DataValueField =StateID;
ddl2.DataBind();
ddl2.Items.Insert(0,new ListItem( - Select State - ,0));
}
}
}
保护无效DropDownList2_SelectedIndexChanged(对象仙,EventArgs的)
{
如果(Page.IsPostBack)
$ { b $ b var ddl2 =(DropDownList)sen;

GridViewRow row =(GridViewRow)ddl2.Parent.Parent;
DropDownList ddl1 =(DropDownList)row.FindControl(DropDownList1);
int idx = row.RowIndex;
if(ddl2.SelectedIndex> 0)
{
ddl1.Items.Clear();
var x = ddl2.SelectedValue;
int id = Convert.ToInt32(x);
TestEntities en = new TestEntities();
var contryId = en.State.Where(s => s.StateID == id).First();
int cc = Convert.ToInt32(contryId.CountryReference.EntityKey.EntityKeyValues [0] .Value);
ddl1.DataSource = en.Country.Where(s => s.CountryID == cc)。ToList();
ddl1.DataTextField =CountryName;
ddl1.DataValueField =CountryID;
ddl1.DataBind();
ddl1.Items.Insert(0,new ListItem( - Select Country - ,0));
}
}
}











快乐编码:) :) :)


I have 2 dropdownlist & 1 gridview in the form, how can we show the data in gridview that is depend on selectedindexedchanged event of single dropdownlist
Only single dropdownlist can work at a single time but we required both dropdownlist is working in the form
when we select 1st dropdownlist then show the data that will depend on 1st dropdownlist & when we select 2nd dropdownlist then show the data that will depend on dropdownlist
I have 2 dropdown in that gridview from same table
I am getting only 1st dropdownlist data & 2nd dropdownlist is not working

解决方案

Default.aspx
// i have a two drop down inside gridview fst one for country seccond for state
//on load binding country ddl to country list and state dropdownlist to state list
//on fst ddl click i am binding it to state
//on state drop down click i am binding its country to fst ddl

<asp:GridView ID="grd" runat="server" AutoGenerateColumns="False"

         onrowdatabound="grd_RowDataBound"

         

 <Columns>

 <asp:TemplateField HeaderText="Country">
 <ItemTemplate >
 <asp:DropDownList ID="DropDownList1" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"  Width="200px" AutoPostBack="true" EnableViewState="true" runat="server" >

 </asp:DropDownList>
 </ItemTemplate>
 </asp:TemplateField>
    <asp:TemplateField HeaderText="State">
 <ItemTemplate>
  <asp:DropDownList ID="DropDownList2" OnSelectedIndexChanged="DropDownList2_SelectedIndexChanged"  Width="200px" AutoPostBack="true" EnableViewState="true" runat="server" >

 </asp:DropDownList>
 </ItemTemplate>
 </asp:TemplateField>
 </Columns>
 </asp:GridView>





Default.cs

// i am using edmx to bind DropDown in grid

protected void DropDownList1_SelectedIndexChanged(object sen, EventArgs e)
        {
            if (Page.IsPostBack)
            {
                var ddl = (DropDownList)sen;

                GridViewRow row = (GridViewRow)ddl.Parent.Parent;
                DropDownList ddl2 = (DropDownList)row.FindControl("DropDownList2");
                int idx = row.RowIndex;
                if (ddl.SelectedIndex > 0)
                {
                    ddl2.Items.Clear();
                    var x = ddl.SelectedValue;
                    int id = Convert.ToInt32(x);
                    TestEntities en = new TestEntities();
                    ddl2.DataSource = en.State.Where(s => s.Country.CountryID == id).ToList();
                    ddl2.DataTextField = "StateName";
                    ddl2.DataValueField = "StateID";
                    ddl2.DataBind();
                    ddl2.Items.Insert(0, new ListItem("--Select State --", "0"));
                }
            }
        }
        protected void DropDownList2_SelectedIndexChanged(object sen, EventArgs e)
        {
            if (Page.IsPostBack)
            {
                var ddl2 = (DropDownList)sen;

                GridViewRow row = (GridViewRow)ddl2.Parent.Parent;
                DropDownList ddl1 = (DropDownList)row.FindControl("DropDownList1");
                int idx = row.RowIndex;
                if (ddl2.SelectedIndex > 0)
                {
                    ddl1.Items.Clear();
                    var x = ddl2.SelectedValue;
                    int id = Convert.ToInt32(x);
                    TestEntities en = new TestEntities();
                  var contryId=  en.State.Where(s => s.StateID == id).First();
                  int cc = Convert.ToInt32(contryId.CountryReference.EntityKey.EntityKeyValues[0].Value);
                    ddl1.DataSource = en.Country.Where(s => s.CountryID ==cc ).ToList();
                    ddl1.DataTextField = "CountryName";
                    ddl1.DataValueField = "CountryID";
                    ddl1.DataBind();
                    ddl1.Items.Insert(0, new ListItem("--Select Country --", "0"));
                }
            }
        }






happy coding :) :) :)


这篇关于在gridview中显示下拉列表的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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