且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将数据集值绑定到gridview中的itemtemplate下拉控件

更新时间:2022-06-04 02:45:27

许多建议避免错误。




Drp.DataSources = ds;



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



喜欢 ds.Tables [sometablename];


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();
}