如何绑定gridview中的下拉列表? [英] how to bind dropdown list in a gridview ?
本文介绍了如何绑定gridview中的下拉列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个按类别搜索的下拉列表。我需要帮助在页面加载时绑定我的gridview,但同时,我还有一个select命令作为投票
I have a dropdown list to search by categories. I need help to bind my gridview at page load, but at the same time, I also have a select command as votes
推荐答案
在Gridview中添加模板字段就像那样
Add template field in Gridview Like that
<asp:templatefield headertext="Some Text" xmlns:asp="#unknown">
<itemtemplate>
<asp:dropdownlist id="ddlAnything" runat="server" />
</itemtemplate>
</asp:templatefield>
< br $> b $ b
// In page Load Bing GridView
GridView1.DataSource = <Your DataSource>;
GridView1.DataBind();
// In RowDataBound Event Of GridView Bind Your DropDown
protected void GridView1_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
// your code to get data
DropDownList ddlAnything= (DropDownList)e.Row.FindControl("ddlAnything");
ddlAnything.DataSource = dataset.Tables[0].DefaultView;
ddlAnything.DataValueField = "ValueField";
ddlAnything.DataTextField = "TextField";
ddlAnything.DataBind();
}
}
如何在asp.net中的行数据绑定事件中绑定网格视图中的下拉列表 [ ^ ]
http://stackoverflow.com/questions/18656733/how-to-bind-asp-net-dropdownlist-control-in-edititemtemplate-of-gridview-on-edit [ ^ ]
< asp:gridview id =gvUserInforunat =serverautogeneratecolumns =falseonrowdatabound =gvUserInfo_RowDataBoundxmlns:asp =#unknown>
< headerstyle backcolor =#df5015font-bold =trueforecolor =White>
< columns> < asp:boundfield datafield =CountryIdheadertext =CountryId>
< asp:boundfield datafield =CountryNameheadertext =CountryName>
< asp:templatefield headertext =Location>
< itemtemplate>
< asp:dropdownlist id =ddlCityrunat =serverwidth = 100px>
c#code:
qlConnection con = new SqlConnection(Data Source = SureshDasari; Integrated Security = true; Initial Catalog = MySampleDB);
protected void Page_Load(object sender,EventArgs e)
{
if(!IsPostBack)
{
BindGridview();
}
}
//此方法用于绑定gridview数据库
protected void BindGridview()
{
con.Open();
SqlCommand cmd = new SqlCommand(选择TOP 4 CountryId,CountryName from Country,con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
gvUserInfo.DataSource = ds;
gvUserInfo.DataBind();
}
protected void gvUserInfo_RowDataBound(对象发送者,GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
con.Open();
var ddl =(DropDownList)e.Row.FindControl(ddlCity);
int CountryId = Convert.ToInt32(e .Row.Cells [0] .Text);
SqlCommand cmd = new SqlCommand(select * from State where CountryID =+ CountryId,con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
con.Close( );
ddl.DataSource = ds;
ddl.DataTextField =StateName;
ddl.DataValueField =StateID;
ddl.DataBind();
ddl.Items.Insert(0,new ListItem( - Select- - ,0));
}
}
试试这个
<asp:gridview id="gvUserInfo" runat="server" autogeneratecolumns="false" onrowdatabound="gvUserInfo_RowDataBound" xmlns:asp="#unknown">
<headerstyle backcolor="#df5015" font-bold="true" forecolor="White">
<columns><asp:boundfield datafield="CountryId" headertext="CountryId">
<asp:boundfield datafield="CountryName" headertext="CountryName">
<asp:templatefield headertext="Location">
<itemtemplate>
<asp:dropdownlist id="ddlCity" runat="server" width="100px">
c# code:
qlConnection con =new SqlConnection("Data Source=SureshDasari;Integrated Security=true;Initial Catalog=MySampleDB");
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGridview();
}
}
// This method is used to bind gridview from database
protected void BindGridview()
{
con.Open();
SqlCommand cmd = new SqlCommand("select TOP 4 CountryId,CountryName from Country", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
gvUserInfo.DataSource = ds;
gvUserInfo.DataBind();
}
protected void gvUserInfo_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
con.Open();
var ddl = (DropDownList)e.Row.FindControl("ddlCity");
int CountryId = Convert.ToInt32(e.Row.Cells[0].Text);
SqlCommand cmd = new SqlCommand("select * from State where CountryID=" + CountryId, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
ddl.DataSource = ds;
ddl.DataTextField = "StateName";
ddl.DataValueField = "StateID";
ddl.DataBind();
ddl.Items.Insert(0, new ListItem("--Select--", "0"));
}
}
try this
这篇关于如何绑定gridview中的下拉列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文