![](/img/trans.png)
[英]Row Databound issue when changing Container.DataItem value in a gridview
[英]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.