1

我正在使用 gridview 使用 c# 在 asp.net 中的 buttonClick 上显示。

 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 上,根据条件,我想更改数据项容器值。我正在按照以下方式进行操作,但问题是所有行都具有数据集中最后一行的支付方法。

<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 回答