Suppose I have the below class
public partial class invoice
{
public list<lineitem> lineitem;
}
public partial class lineitem
{
private Quantity quantity = new Quantity();
public Quantity Quantity
{
get { return quantity; }
set { quantity = value; }
}
}
How do I bind the value to the gridview.
public void lbluom_OnDataBinding(object sender, System.EventArgs e)
{
for (int i = 0; i < invoicetransmit.Invoice.Count; i++)
{
Label lbl = (Label)sender;
lbl.Text =
invoicetransmit.Invoice[0].LineItem[i].Quantity.Value.ToString();
}
If I do this the values in the gridview are being overwritten with the latest values...and this one below
public void lbluom_OnDataBinding(object sender, System.EventArgs e)
{
for (int i = 0; i < invoicetransmit.Invoice.Count; i++)
{
Label lbl = (Label)sender;
lbl.Text = Eval("Value")
} property does not exist.
}
}
This is how i added values to the grid
public void Addtogrid()
{
//var lineItems = (Session["BillXML"] as InvoiceTransmission).Invoice[0].LineItem;
invoicetransmit.Invoice[0].LineItem.Add(new LineItem {MaterialCode = MaterialCode.Text, ChargeCode = ChargeCode.Text, CostCenter = CostCenter.Text, GLAccount = GLAccount.Text });
//lineItems.Add(new LineItem { MaterialCode=MaterialCode.Text,ChargeCode=ChargeCode.Text,CostCenter=CostCenter.Text,GLAccount=GLAccount.Text});
for (int i = 0; i < invoicetransmit.Invoice[0].LineItem.Count; i++)
{
invoicetransmit.Invoice[0].LineItem[i].Quantity.UOMCode = UOM.Text;
invoicetransmit.Invoice[0].LineItem[i].Quantity.Value = Convert.ToDecimal(Quantity.Text);
invoicetransmit.Invoice[0].LineItem[i].UnitPrice.Value = Convert.ToDecimal(Price.Text);
//invoicetransmit.Invoice[0].LineItem[i].TotalNetAmount = (invoicetransmit.Invoice[0].LineItem[i].Quantity.Value) * (invoicetransmit.Invoice[0].LineItem[i].UnitPrice.Value);
invoicetransmit.Invoice[0].LineItem[i].TotalNetAmount = ( Convert.ToDecimal(Quantity.Text) )* (Convert.ToDecimal(Price.Text));
GridView1.DataSource = invoicetransmit.Invoice[0].LineItem;
// GridView1.DataSource = lineItems;
GridView1.Dat开发者_StackOverflowaBind();
//}
Please help me resolve this question
Try the RowDataBound event
<asp:GridView ID="GridView1" runat="server"
onrowdatabound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
and
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label lbl = (Label) e.Row.FindControl("Label1");
if(lbl!=null)
{
lbl.Text = invoicetransmit.Invoice[0].LineItem[e.Row.RowIndex].Quantity.Value.ToString();
}
}
}
精彩评论