如何将数据集值绑定到gridview中的itemtemplate下拉控件 [英] How to bind dataset values into itemtemplate dropdown control in gridview

查看:53
本文介绍了如何将数据集值绑定到gridview中的itemtemplate下拉控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

项目模板列控件是gridview中的下拉列表。数据集值将绑定到该下拉控件中

我已将代码写入页面加载事件

The item template column control is dropdown in gridview. dataset values will binded into that dropdown control
i have written code into the page load Event

DataSet ds = new DataSet();
ds = GetDataValue();//This line get values from the database
GrdEmploye.DataSource = ds;
GrdEmploye.DataBind();
int i = 0;
DropDownList Drp;
foreach (GridViewRow RowItem in GrdEmploye.Rows)
{ 
    Drp = (DropDownList)(RowItem.Cells[0].FindControl    ("DrpEmployee"));//This line is denote the item template column control
    Drp.DataSources= ds;
    Drp.DataTextField  = ds.Tables[0].Rows[i]["Empsap"].ToString();
    Drp.DataValueField = ds.Tables[0].Rows[i]["Empsap"].ToString();
    Drp.DataBind();//This line got error
}

推荐答案

许多建议避免错误。




Drp.DataSources = ds;



而不是绑定整个数据集到Drp。只需绑定特定的表格。



喜欢 ds.Tables [sometablename];
< br>

Drp.DataTextField = ds.Tables [0] .Rows [i] [Empsap]。ToString();

Drp.DataValueField = ds.Tables [0] .Rows [i] [Empsap]。ToString();




在这里,您只需要指定特定的表列名称,而不是动态行值数据。< clear>






例如,如果你确定该表有列,假设费用然后将其绑定。

Many suggestion to avoiding error.


Drp.DataSources= ds;


Rather then binding whole dataset to Drp. just bind the specific table inside.

Like ds.Tables["sometablename"];


Drp.DataTextField = ds.Tables[0].Rows[i]["Empsap"].ToString();
Drp.DataValueField = ds.Tables[0].Rows[i]["Empsap"].ToString();



Here you just need to assign specific table column name rather then dynamic row value data.<clear>



For Example are if you're sure that table have column, suppose "Fees" then bind it with.
Drp.DataTextField = "Fees";



同样适用于DataValueField。



如果您不确定列名,那么只需从表中获取列名


Same holds true for DataValueField.

And If you aren't sure with the column name, then just fetch column name from table Like

Drp.DataTextField = dt.Tables[0].Columns[2].ColumnName



投票 接受答案 如果有帮助。


Please vote and Accept Answer if it Helped.


试试这个



DropDown列表ddl;

int i = 0;

foreach(grdmenu.Rows中的GridViewRow grdrow)

{

ddl =(DropDownList)grdmenu.Rows [grdrow.RowIndex] .FindControl(ddloffer);

ddl.SelectedIndex = Convert.ToInt16(ds.Tables [0] .Rows [i] [提供]);

ddl =(DropDownList)grdmenu.Rows [grdrow.RowIndex] .FindControl(ddloffers);

ddl.SelectedIndex = Convert.ToInt16( ds.Tables [0] .Rows [i] [OfferType]);

i ++;

ddl.DataBind();

}
Try This

DropDownList ddl;
int i = 0;
foreach (GridViewRow grdrow in grdmenu.Rows)
{
ddl = (DropDownList)grdmenu.Rows[grdrow.RowIndex].FindControl("ddloffer");
ddl.SelectedIndex = Convert.ToInt16(ds.Tables[0].Rows[i]["Offer"]);
ddl = (DropDownList)grdmenu.Rows[grdrow.RowIndex].FindControl("ddloffers");
ddl.SelectedIndex = Convert.ToInt16(ds.Tables[0].Rows[i]["OfferType"]);
i++;
ddl.DataBind();
}


DropDownList ddl;

foreach(GridViewRow gvRow in gvProf.Rows)

{

ddl =(DropDownList)(gvRow.Cells [5] .FindControl(ddlDcode));

ddl.DataSource = ds;

ddl.DataTextField = ds.Tables [0] .Columns [dis_name]。ToString();

ddl.DataValueField = ds。 Tables [0] .Columns [dis_code]。ToString();

ddl.DataBind();

}
DropDownList ddl;
foreach (GridViewRow gvRow in gvProf.Rows)
{
ddl = (DropDownList)(gvRow.Cells[5].FindControl("ddlDcode"));
ddl.DataSource = ds;
ddl.DataTextField = ds.Tables[0].Columns["dis_name"].ToString();
ddl.DataValueField = ds.Tables[0].Columns["dis_code"].ToString();
ddl.DataBind();
}


这篇关于如何将数据集值绑定到gridview中的itemtemplate下拉控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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