在asp.net gridview中添加datatable.row的下拉列表 [英] add dropdown to datatable.row in asp.net gridview
本文介绍了在asp.net gridview中添加datatable.row的下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨!
我有一个网格视图,我想通过cs文件绑定它。这是我的aspx页面代码:
Hi!
i have a grid view and i want to bind it through cs file. here is my aspx page code:
<asp:DropDownList ID="drpdwndec" runat="server" AutoPostBack="True">
</asp:DropDownList>
<div>
<asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="false" >
<Columns>
<asp:BoundField DataField="sno" HeaderText="Serial.No" />
<asp:BoundField DataField="aspno" HeaderText="ADP.No" />
<asp:BoundField DataField="scheme" HeaderText="Nomenclature" />
<asp:BoundField DataField="adpcost" HeaderText="ADP Cost" />
<asp:BoundField DataField="pc1cost" HeaderText="PC-1 Cost" />
<asp:BoundField DataField="decision" HeaderText="Decision" />
</Columns>
</asp:GridView>
</div>
这是我的cs代码
and here is my cs code
List<Database.BLL.MeetingAgendaBLL.Fatadepart> list = meetingagendabll.GetFataDepart(year, meetingid);
int count=1;
DataTable dt = new DataTable();
int rowcount = 0;
DataTable dt1=new DataTable();
dt.Columns.AddRange(new DataColumn[6] { new DataColumn("sno", typeof(string)),
new DataColumn("aspno", typeof(string)),
new DataColumn("scheme",typeof(string)) ,
new DataColumn("adpcost",typeof(string)),
new DataColumn("pc1cost",typeof(string)),
new DataColumn("decision",typeof(WebControl))});
foreach (var f in list)
{
string a = f.FataDepartTitle;
dt.Rows.Add(a, "", "", "", "", null);
List<Database.BLL.MeetingAgendaBLL.Department1> List1 = meetingagendabll.GetMAgendarpt(year, f.FataDepartID, meetingid);
foreach (var g in List1)
{
dt.Rows.Add(count.ToString(), g.ADPCode.ToString(), g.SchemeName, g.ADPcost.ToString(), g.PC1cost.ToString(),drpdwndec);
count++;
}
}
gvCustomers.DataSource = dt;
gvCustomers.DataBind();
但是当我运行该程序时显示
System.Web.UI.WebControls.DropDownList
$ b 6美元而不是下拉美元。
请告诉我我做错了什么
提前谢谢
but when i run the program is show
System.Web.UI.WebControls.DropDownList
in 6th colm instead of dropdown.
Please tell me what i have made wrong
Thanks in advance
推荐答案
您不能将DropDownList添加到DataTable,它不是用于添加控件而是用于处理数据。
您必须将第6列更改为模板列并添加下拉列表。
< asp:GridView ID =gvCustomersrunat =serverAutoGenerateColumns =False>
< Columns>
< asp:BoundField DataField =snoHeaderText =Serial.No/>
< asp:BoundField DataField =aspnoHeaderText =ADP.No />
< asp:BoundField DataField =schemeHeaderText =Nomenclature/>
< asp:BoundField DataField =adpcostHeaderText = ADP成本/>
< asp:BoundField DataField =pc1costHeaderText =PC-1 Cost/>
< asp:TemplateField HeaderText =决定>
< ItemTemplate>
< asp:DropDownList ID =drpdwndecrunat =serverAutoPostBack =True>
< / asp:DropDownList>
< / ItemTemplate>
< / asp:TemplateField>
< / Columns>
< ; / asp:GridView>
代码落后:
//'将此声明为页面级变量避免重复数据库读取
DataTable dtbl;
protected DataTable GetDecisions()
{
if(dtbl == null){
dtbl = new DataTable();
//添加代码填写数据表
}
返回dtbl;
}
protected void gvCustomers_RowDataBound(object sender,System.Web.UI.WebControls.GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow){
//查找行中的DropDownList
DropDownList drpdwndec =(DropDownList)e.Row.FindControl(drpdwndec);
drpdwndec.DataSource = GetDecisions();
drpdwndec.DataTextField =决定; //更改列名
drpdwndec.DataValueField =决策; //更改列名
drpdwndec.DataBind();
//在DropDownList中添加默认项目
drpdwndec.Items.Insert(0,new ListItem(Please select) );
//选择决定
drpdwndec.SelectedValue = DataBinder.Eval(e.Row.DataItem,Decision); //更改列名
}
}
You cannot add DropDownList to a DataTable, It is not for adding controls but for dealing with data.
You have to change your 6th column to a template column and add dropdown to it.
<asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="sno" HeaderText="Serial.No" />
<asp:BoundField DataField="aspno" HeaderText="ADP.No" />
<asp:BoundField DataField="scheme" HeaderText="Nomenclature" />
<asp:BoundField DataField="adpcost" HeaderText="ADP Cost" />
<asp:BoundField DataField="pc1cost" HeaderText="PC-1 Cost" />
<asp:TemplateField HeaderText="Decision">
<ItemTemplate>
<asp:DropDownList ID="drpdwndec" runat="server" AutoPostBack="True">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
code behind:
//'declare this as page level variable to avoid repeated database reads
DataTable dtbl;
protected DataTable GetDecisions()
{
if (dtbl == null) {
dtbl = new DataTable();
//Add Code to fill the datatable
}
return dtbl;
}
protected void gvCustomers_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow) {
//Find the DropDownList in the Row
DropDownList drpdwndec = (DropDownList)e.Row.FindControl("drpdwndec");
drpdwndec.DataSource = GetDecisions();
drpdwndec.DataTextField = "Decision"; //Change the column name
drpdwndec.DataValueField = "Decision"; //Change the column name
drpdwndec.DataBind();
//Add Default Item in the DropDownList
drpdwndec.Items.Insert(0, new ListItem("Please select"));
//Select the Decision
drpdwndec.SelectedValue = DataBinder.Eval(e.Row.DataItem, "Decision"); //Change the column name
}
}
这篇关于在asp.net gridview中添加datatable.row的下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文