如何从代码behinde将数据绑定到gridview中的DropDownList [英] how bind data to DropDownList in gridview from code behinde
本文介绍了如何从代码behinde将数据绑定到gridview中的DropDownList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我无法将数据绑定到gridview中的dropdownlist,而不是sqldatasource后面的代码
我的代码是:
i cant bind data to dropdownlist in gridview from code behind without sqldatasource
my code is:
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
GetData();
}
public void GetData()
{
DataTable dt = new DataTable();
string strSql = "SELECT Tbl_Order.ID_Order,"+
" Tbl_Order.FlNm, " +
" Tbl_Order.Email," +
" Tbl_Order.Sbj," +
" Tbl_Order.PhNo," +
" Tbl_Order.Psg," +
" Tbl_Order.FlUpld," +
" Tbl_Order.Srl_No," +
" Tbl_Prst.pursuit" +
" FROM Tbl_Order INNER JOIN Tbl_Prst ON Tbl_Order._id_prst = Tbl_Prst.id_prst";
Dal.Fill_Dt(strSql, dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
DataTable dt_pursuit = new DataTable();
Dal.Fill_Dt("select _id_prst,pursuit from Tbl_Prst)", dt_pursuit);
string FlNm = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtFlNm")).Text;
string Email = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtEmail")).Text;
string Sbj = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtSbj")).Text;
string PhNo = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtPhNo")).Text;
string Psg = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtPsg")).Text;
string Srl_No = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtSrl_No")).Text;
string pursuit = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtpursuit")).Text;
string id = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtID_Order")).Text;
((DropDownList)GridView1.Rows[e.RowIndex].FindControl("DDLpursuit")).DataSource = dt_pursuit;
((DropDownList)GridView1.Rows[e.RowIndex].FindControl("DDLpursuit")).DataTextField = "pursuit";
((DropDownList)GridView1.Rows[e.RowIndex].FindControl("DDLpursuit")).DataValueField= "_id_prst";
((DropDownList)GridView1.Rows[e.RowIndex].FindControl("DDLpursuit")).DataBind();
string str = string.Format("update Tbl_Order set FlNm=N'{0}',Email=N'{1}',Sbj=N'{2}',PhNo=N'{3}',Psg=N'{4}',Srl_No=N'{5}'" +
"WHERE ID_Order={6}", FlNm, Email, Sbj, PhNo, Psg, Srl_No, id);
Dal.Cmd_Qry(str);
GridView1.EditIndex = -1;
GetData();
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:GridView ID="GridView1"
AutoGenerateColumns="False"
OnRowUpdating="GridView1_RowUpdating"
OnRowEditing="GridView1_RowEditing"
OnRowCancelingEdit="GridView1_RowCancelingEdit"
OnRowDeleting="GridView1_RowDeleting"
DataKeyNames="ID_Order"
runat="server">
<Columns>
<asp:CommandField ShowEditButton="True" />
<asp:TemplateField>
<EditItemTemplate>
<asp:TextBox ID="txtID_Order" ReadOnly="true" runat="server" Text='<%# Bind("ID_Order") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblID_Order" runat="server" Text='<%# Bind("ID_Order") %>'>></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<EditItemTemplate>
<asp:TextBox ID="txtFlNm" runat="server" Text='<%# Bind("FlNm") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblFlNm" runat="server" Text='<%# Bind("FlNm") %>'>></asp:Label>
</ItemTemplate>
</asp:TemplateField>
.......
.........
............
i希望将代码后面的数据绑定到dropdownlist,如下所示/>
i want bind data from code behind to dropdownlist like this
public void ShowDDL_pursuit()
{
DataTable dt_pursuit = new DataTable();
Dal.Fill_Dt("select _id_prst,pursuit from Tbl_Prst)", dt_pursuit);
DDLpursuit.DataSource = dt_pursuit;
DDLpursuit.DataTextField = "pursuit";
DDLpursuit.DataValueField = "_id_prst";
DDLpursuit.DataBind();
}
但是在课堂上并不知道
我试试这个:
but is not unknown in class
and i try this:
((DropDownList)GridView1.Rows[e.RowIndex].FindControl("DDLpursuit")).DataSource = dt_pursuit;
((DropDownList)GridView1.Rows[e.RowIndex].FindControl("DDLpursuit")).DataTextField = "pursuit";
((DropDownList)GridView1.Rows[e.RowIndex].FindControl("DDLpursuit")).DataValueField= "_id_prst";
((DropDownList)GridView1.Rows[e.RowIndex].FindControl("DDLpursuit")).DataBind();
但它不起作用
请帮帮我怎样才能做到这一点
but it''s not work
please help me how can i do this
推荐答案
在GridView1_RowDataBound事件中添加dropdownList值。
1 。将grvDC_RowDataBound事件添加到页面
2.在pageload中获取数据集中的下拉列表值。并声明DataTable''dt_pursuit''对所有函数都是通用的
3.在grvDC_RowDataBound事件中将数据集值添加到下拉列表
as
protected void grvDC_RowDataBound(object sender,System.Web.UI.WebControls.GridViewRowEventArgs e)
{
尝试{
ddl = new DropDownList();
if(e.Row.RowType = = DataControlRowType.DataRow)
{
ddl =(DropDownList)e.Row.FindControl(" ddlDCTypes");
DDLpursuit.DataSource = dt_pursuit;
DDLpursuit.DataTextField =追求;
DDLpursuit.DataValueField =_ id_prst;
}
} catch(例外Ex){
抛出Ex;
}
}
Add the dropdownList value at GridView1_RowDataBound event.
1. Add the grvDC_RowDataBound event to page
2. Get the dropdownlist value in dataset at pageload. and declare the DataTable ''dt_pursuit'' as common to all function
3. In grvDC_RowDataBound event add the dataset value to dropdown
as
protected void grvDC_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{
try {
ddl = new DropDownList();
if (e.Row.RowType == DataControlRowType.DataRow)
{
ddl = (DropDownList)e.Row.FindControl("ddlDCTypes");
DDLpursuit.DataSource = dt_pursuit;
DDLpursuit.DataTextField = "pursuit";
DDLpursuit.DataValueField = "_id_prst";
}
} catch (Exception Ex) {
throw Ex;
}
}
您好
您是否尝试过这样的事情。
((DropDownList)GridView1.Rows [e.RowIndex] .FindControl(DDLpursuit))。DataSource = dt_pursuit.Rows.AsQueryable()。OfType< system .data.datarow>()。ToList();
我不确定是否会起作用但值得一试。
问候
Jegan
Hi
Have you tried something like this.
((DropDownList)GridView1.Rows[e.RowIndex].FindControl("DDLpursuit")).DataSource = dt_pursuit.Rows.AsQueryable().OfType<system.data.datarow>().ToList();
I am not sure whether will work but could be worth a try.
Regards
Jegan
Hi
实际答案在这里:
使用c#将数据绑定到下拉列表 [ ^ ]
问候
Jegan
Hi Actually answer is here:
bind data to dropdownlist with c#[^]
Regards
Jegan
这篇关于如何从代码behinde将数据绑定到gridview中的DropDownList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文