Problem in working with list(collections)

  • Thread starter Thread starter Vijay
  • Start date Start date
V

Vijay

Hi,
I ve created a webpage with a data grid view and a textbox and a button.
When I enter a text and click the button, the value must be added to the
list (collection) and the list is binded with the data grid view.
for this I created and initialized a list. In the button click event I added
the textbox value to the list and binded it to the grid view.
The problem is whenever I click the button, the last value that I entered
is only displayed.
Anyone please help me...
 
public class PurchaseItems
{
public string SampleNo { get; set; }
public string Description { get; set; }
public string Quantity { get; set; }
public string Rate { get; set; }
public string Amount { get; set; }
}

public partial class PurchaseOrder : System.Web.UI.Page
{
List<PurchaseItems> purchaseItm = new List<PurchaseItems>();
public PurchaseItems p;

protected void Page_Load(object sender, EventArgs e)
{
gvPurchase.DataSource = purchaseItm;
lblStatus.Text += "Pageload ";
}


protected void cmdAdd_Click(object sender, EventArgs e)
{
p = new PurchaseItems
{
SampleNo = ddlSampleNo.SelectedItem.Text,
Description = txtDescription.Text,
Quantity = txtQuantity.Text,
Rate = txtRate.Text,
Amount = txtAmount.Text,
};
purchaseItm.Add(p);
gvPurchase.DataBind();
}
}

This is the code snippet I've used in my program...
 
Vijay,

Use the below code snippet. it works. Use List<T> as STATIC . There will be
better way to hanlde this by using Generic collections



public partial class _Default : System.Web.UI.Page

{

static List<PurchaseItems> purchaseItm;

public PurchaseItems p;

protected void Page_Load(object sender, EventArgs e)

{


Label1.Text += "Pageload ";

}

protected void Button1_Click(object sender, EventArgs e)

{

if (purchaseItm==null)

purchaseItm = new List<PurchaseItems>();

//purchaseItm = new CustomCollection<PurchaseItems>();

p = new PurchaseItems

{

SampleNo = "bb",

Description = TextBox1.Text,

Quantity = "1",

Rate = "2",

Amount = "22.00",

};

purchaseItm.Add(p);

GridView1.DataSource = purchaseItm;

GridView1.DataBind();

}

}

public class PurchaseItems

{

public string SampleNo { get; set; }

public string Description { get; set; }

public string Quantity { get; set; }

public string Rate { get; set; }

public string Amount { get; set; }

}
 
Back
Top