I am wondering if i can change the Container.DataItem in codebehind in a gridview. I mean, if i want to change the Dataitem from “SellingPaymentMethod” to “DeliveryPaymentmethod” depending on a condition.
I am using the same stored procedure for getting the information both for selling and delivery. So if it is a selling it should be SellingPaymentmethod, else it should be DeliveryPaymentmethod. This is in ASP.Net using C#.
<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
}
EDIT:
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();
}
}
This is how i used your code. And i am not sure why all the rows are having the last row values in the paymentmethod column.
And i have the databinding code in the btnSubmit_OnClick function
DataView myDataView = new DataView();
myDataView = dsnew.Tables[0].DefaultView;
grdDetail.DataSource = myDataView;
grdDetail.DataBind();
Create an
RowDataBoundevent handler and change it there.Aspx:
Code Behind: