目前程式會呼叫dagRequestItem_H_bind()綁定資料到Datagrid ID=dagRequestItem_H上,現在想將dataGrid中的Brand/品牌欄位,透過抓欄位Category/資產的ID=lblCategory,當參數呼叫SQL,綁定下拉選單至同個Datagrid上。
有嘗試在OnRowDataBound寫Func但只會產生下拉選單,裡面並不會有值,想問有甚麼方式可以綁定選單;因為日後還會用成連動的下拉選單
但如果直接在前端的DropDownList加上DataSource='<%# GetBrandDataSource() %>'可以成功綁定下拉選單讓它有選項,但此方法好像沒辦法去抓Category欄位的值當參數去呼叫sql綁定下拉選單,自己沒有寫過甚麼Webform對這塊不太熟,想問有甚麼方式能成功。
前端的.aspx
<asp:DataGrid ID="dagRequestItem_H" runat="server" DataKeyField="id" CellPadding="0"
AutoGenerateColumns="False" ShowHeader="true" GridLines="None" Width="100%"
OnRowDataBound="myGridView_RowDataBound" >
<Columns>
<asp:TemplateColumn HeaderText="Category/資產" >
<ItemTemplate>
<asp:Label ID="lblCategory" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.category") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Brand/品牌" >
<ItemTemplate>
<asp:DropDownList id="myDropDown" runat="server" >
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Model/機型" >
<ItemTemplate>
<asp:Label ID="lblModel" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.model") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
後端的dagRequestItem_H_bind()綁定datagrid
private void dagRequestItem_H_bind()
{
string formNo = Request["FORM_NO"].ToString();
DataSet ds = EQDetailBL.getHelpDeskRequestDetail(formNo.ToString().Trim());
this.dagRequestItem_H.DataSource = ds.Tables[0].DefaultView;
this.dagRequestItem_H.DataBind();
}
後端的myGridView_RowDataBound()
protected void myGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList myDropDown = (DropDownList)e.Row.FindControl("myDropDown");
DataSet brandTable = GetBrandDataSource();
myDropDown.DataSource = brandTable;
myDropDown.DataTextField = "BRAND";
myDropDown.DataValueField = "BRAND";
myDropDown.DataBind();
}
}
protected DataSet GetBrandDataSource()
{
DataSet dsBrand = RequestListBL.getBrandList();
return dsBrand;
}
你看看這樣可不可以
protected void myGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label lblCategory = (Label)e.Row.FindControl("lblCategory");
string category = lblCategory.Text;
DropDownList myDropDown = (DropDownList)e.Row.FindControl("myDropDown");
DataSet brandTable = GetBrandDataSourceByCategory(category);
myDropDown.DataSource = brandTable;
myDropDown.DataTextField = "BRAND";
myDropDown.DataValueField = "BRAND";
myDropDown.DataBind();
}
}
protected DataSet GetBrandDataSourceByCategory(string category)
{
DataSet dsBrand = RequestListBL.getBrandListByCategory(category);
return dsBrand;
}