繁体   English   中英

在GridView的代码隐藏中更改Container DataItem

[英]Changing the Container DataItem in codebehind for GridView

我想知道是否可以在gridview中的代码隐藏中更改Container.DataItem。 我的意思是,如果我想根据条件将数据项从“ SellingPaymentMethod”更改为“ DeliveryPaymentmethod”。

我正在使用相同的存储过程来获取要出售和交付的信息。 因此,如果是卖出,则应为SellingPayment方法,否则应为DeliveryPayment方法。 这是使用C#在ASP.Net中。

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



if(Condition1 = "Selling")
{
    use sellingpaymentmethod container
}
else
{
    use deliverypaymentmethod
}

编辑:

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;
        Label lblBalance = e.Row.FindControl("lblTotalSold") as Label;
        Label lblBalanceCollected = e.Row.FindControl("lblTotalCollected") as Label;

        if (lblTypeofDay.Text == "Selling")
        {
            lblPaymentMethod.Text = dtrCurrentRow["SellingPaymentMethod"].ToString();
        }
        else if (lblTypeofDay.Text == "Delivery")
        {
            lblPaymentMethod.Text = dtrCurrentRow["DeliveryPaymentmethod"].ToString();
        }
     }

这就是我使用您的代码的方式。 而且我不确定为什么所有行在paymentmethod列中都具有最后一行的值。

我在btnSubmit_OnClick函数中有数据绑定代码

DataView myDataView = new DataView();
myDataView = dsnew.Tables[0].DefaultView;
grdDetail.DataSource = myDataView;
grdDetail.DataBind();

创建一个RowDataBound事件处理程序,然后在其中进行更改。

Aspx:

<asp:GridView id="gridView1" runat="server" 
    OnRowDataBound="gridView1_RowDataBound" />

背后的代码:

public void gridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if(e.Row.RowType != DataControlRowType.DataRow)
    {
        return;
    }

    DataRow row = (DataRow)e.Row.DataItem;

    Label lblSellingPaymentMethod = 
        e.Row.FindControl("lblSellingPaymentMethod") as Label;

    if(condition == true)
    {
        lblSellingPaymentMethod.Text = row["sellingpaymentmethod"].ToString();
    }
    else
    {
        lblSellingPaymentMethod.Text = row["deliverypaymentmethod"].ToString();
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM