iT邦幫忙

0

Webform 抓當前的DataGrid欄位值當作參數呼叫sql,在同個datagrid綁定下拉選單

  • 分享至 

  • xImage

目前程式會呼叫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;
        }
jack8900 iT邦新手 2 級 ‧ 2023-05-24 15:37:40 檢舉
在dagRequestItem_H_bind裡面增加修改下拉選單的程式碼就可以了吧?
改DataGrid的時候一起修改下拉選單的內容
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

0
Nash
iT邦新手 4 級 ‧ 2024-02-23 10:28:24

你看看這樣可不可以



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

我要發表回答

立即登入回答