在项模板中选择所选值
本文关键字:选择 | 更新日期: 2023-09-27 18:09:36
我有一个名为BindItems()的方法,该方法获取数据表的值,然后将GridView绑定到它。
然后我在gridview中有一个itemtemplate,其中包含一个下拉菜单,这个下拉菜单从1 -14从Gridview_RowDataBound下的代码填充,现在我需要找到一种方法来获得数据表上的另一个函数"BindItems()"的数量值,对于gridview中的每一行,所以SelectedValue = DataTable["Quantity"]或其他东西。我该怎么做呢?
protected void BinItems(int myId)
{
//this data table contains a value "Quantity"
DataTable dt = MyClass.getItems(myId);
uxGrid.DataSource = dt;
uxGrid.DataBind();
}
protected void Gridview1_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType.Equals(DataControlRowType.DataRow))
{
DropDownList Myddl = e.Row.FindControl("MyQuantity") as DropDownList;
if (Myddl != null)
{
for (int i = 1; i < 15; i++)
{
Myddl.Items.Add(i.ToString());
}
}
}
//Some how i need to do a SelectedValue = datatable where field = Quantity for each row
}
您需要访问当前被绑定行的DataItem
,在您的情况下,它是DataRowView
(因为您绑定到DataTable
):
protected void Gridview1_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType.Equals(DataControlRowType.DataRow))
{
DropDownList Myddl = e.Row.FindControl("MyQuantity") as DropDownList;
if (Myddl != null)
{
for (int i = 1; i < 15; i++)
{
Myddl.Items.Add(i.ToString());
}
}
}
//here goes the "magic"
DataRowView dRow = e.Row.DataItem as DataRowView;
if(dRow != null)
{
Myddl.SelectedValue = dRow["Quantity"].ToString();
}
}
您应该在DataTable dt之外,然后您可以在Function