1

C#を使用してasp.netのボタンクリックに表示するためにグリッドビューを使用しています。

 DataSet dsnew  = new businessLogic.Biz().getData();
 if (dsnew != null && dsnew.Tables[0].Rows.Count > 0)
 {
     DataView myDataView = new DataView();
     myDataView = dsnew.Tables[0].DefaultView;
     grdDetail.DataSource = myDataView;
     grdDetail.DataBind();
 }

そして、rowDataBound では、条件に応じて、dataitem コンテナの値を変更したいと考えています。私は次のようにしていますが、問題は、すべての行にデータセットの最後の行の支払い方法があることです。

<asp:TemplateField ItemStyle-Width="70" ItemStyle-HorizontalAlign="Center">
   <ItemTemplate>
       <asp:Label ID="lblPaymentMethod" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"SellingPaymentMethod") %>'></asp:Label>
   </ItemTemplate>
</asp:TemplateField>


protected void grdDetail_RowDataBound(object sender, GridViewRowEventArgs e)
{

    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        return;
    }

    foreach (DataRow dtrCurrentRow in (((System.Data.DataView)grdDetail.DataSource)).Table.Rows)
    {
        //DataRow row = (DataRow)e.Row.DataItem;
        Label lblPaymentMethod = e.Row.FindControl("lblPaymentMethod") as Label;

        if (Condition1))
        {
            lblPaymentMethod.Text = dtrCurrentRow["SellingPaymentMethod"].ToString();
        }
        else if (Condition 2)
        {
            lblPaymentMethod.Text = dtrCurrentRow["DeliveryPaymentmethod"].ToString();
        }         

    }    
4

1 に答える 1

0

rowDataBound イベントを使用して表示する値を確認する代わりに、可視性を直接制御したり、列のテキスト プロパティを直接設定したりできます。私はあなたのために簡単な例を作りました.これはあなたのニーズに簡単に適応することができます!

ASPX

<asp:GridView ID="gvProducts" runat="server" AutoGenerateColumns="False">
    <Columns>
        <asp:TemplateField HeaderText="ID">
            <ItemTemplate>
                <asp:Label ID="Label1" runat="server" Visible='<%# CheckCondition1(Eval("ID")) %>' Text='<%# Bind("ID") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="ProductName">
            <ItemTemplate>
                <asp:Label ID="Label2" runat="server" Visible='<%# CheckCondition2(Eval("ProductName")) %>' Text='<%# Bind("ProductName") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="thirdColumn">
            <ItemTemplate>
                <asp:Label ID="Label3" runat="server" Text='<%# GetValue(Eval("ID")) %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
            <asp:TemplateField HeaderText="forthColumn">
            <ItemTemplate>
                <asp:Label ID="Label4" runat="server" Text='<%# GetValue(Eval("ID"), "staticValue", Eval("ProductName")) %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

コードビハインド

public bool CheckCondition1(object ID)
{
    return ID.ToString() != "2";
}
public bool CheckCondition2(object ProductName)
{
    return ProductName.ToString() != "jklö";
}
public string GetValue(object ID)
{
    // check condition here       
    //    ...
    // and return according value
    return ID.ToString() + " is your ID";

    // eg
    //if (Condition1))
    //    return dtrCurrentRow["SellingPaymentMethod"].ToString();
    //else if (Condition 2)
    //    return dtrCurrentRow["DeliveryPaymentmethod"].ToString();
}
public string GetValue(object ID, object firstValue, object secondValue)
{      
    if (ID.ToString() == "2")
        return firstValue.ToString();
    else
        return secondValue.ToString();
}

デモ値を表示する CodeBehind

// assume there is a class Products with id and ProductName
protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        List<Product> products = new List<Product>();
        products.Add(new Product() { ID = 1, ProductName = "asdf" });
        products.Add(new Product() { ID = 2, ProductName = "asdf" });
        products.Add(new Product() { ID = 3, ProductName = "jklö" });
        products.Add(new Product() { ID = 4, ProductName = "asdf" });
        gvProducts.DataSource = products;
        gvProducts.DataBind();
    }
}

結果は次のようになります。

指定されたコードの期待される結果

于 2012-09-24T19:21:11.637 に答える