开发者

choose the selected value in a item template

I have a method called BindItems() and this method gets the values to a DataTable and then a GridView is bound to it.

Then I have an itemtemplate in that gridview which contains a drop down, this drop down gets populated from 1 -14 from code behind under Gridview_RowDataBound, now I need to figure a way to get the Quantity value in the DataTable on the other function "BindItems()" and for each Row in the gridview so a SelectedValue = datatable["Quantity"] or something.. how can I do this?

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 Sele开发者_JAVA百科ctedValue = datatable where field = Quantity for each row
}


You need to access the current-being-bound-row's DataItem, which in your case, is a DataRowView (since you are binding to a 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();
   }
}


you should outside the DataTable dt and then youll be able to access it in the Function

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜