Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > Other .NET > General .NET
|
General .NET For general discussion of MICROSOFT .NET topics that don't fall within any of the other .NET forum subcategories or .NET language forums.  If your question is specific to a language (C# or Visual Basic) or type of application (Windows Forms or ASP.Net) try an applicable forum category. ** PLEASE BE SPECIFIC WITH YOUR QUESTION ** When posting here, provide details regarding the Microsoft .NET language you are using and/or what type of application (Windows/Web Forms, etc) you are working in, if applicable to the question. This will help others answer the question without having to ask.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the General .NET section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 23rd, 2004, 02:36 AM
Authorized User
 
Join Date: Aug 2004
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Default ASP.NET DataGrid Question

I am having a problem of adding data into a grid. wherever i click a button to add an item into a grid, it always add remain the last item i add only. Here is some of my code, i am thikning is the post back problem but i do not know where to place the post back.

Here is some of my code:

DataTable dt = new DataTable();
DataView dv = new DataView();
DataRow dr;

protected System.Web.UI.WebControls.DataGrid dg;
protected System.Web.UI.WebControls.DropDownList ddlOption;
protected System.Web.UI.WebControls.Button Button1;

private void Page_Load(object sender, System.EventArgs e)
{
  OpenTable();
  InitializeDropdown();
  if (!IsPostBack)
  {
    InitializeDataGrid();
  }
}

public void InitializeDropdown()
{
  if (!IsPostBack)
  {
    ArrayList values = new ArrayList();
      values.Add ("id 1");
      values.Add ("id 2");
      values.Add ("id 3");
      values.Add ("id 4");
      values.Add ("id 5");

    ddlOption.DataSource = values;
    ddlOption.DataBind();
  }
}

public void OpenTable()
{
  dt = new DataTable();
  dt.Columns.Add(new DataColumn("id", typeof(string)));
}

public void InitializeDataGrid()
{
  dv = new DataView(dt);

  dg.DataSource = dv;
  dg.DataBind();
}

private void Button1_Click(object sender, System.EventArgs e)
{
  dr = dt.NewRow();

  dr[0] = ddlOption.SelectedItem;
  dt.Rows.Add(dr);

  InitializeDataGrid();
  dg.DataBind();
}

~ Human Knowledge Belongs to the World !
__________________
~ Human Knowledge Belongs to the World !
 
Old August 25th, 2004, 01:51 AM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 326
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to Santhi Send a message via MSN to Santhi
Default

The reason you are getting the last value selected because each time on click of button you are creating one row with the selected dropdown list item only.You are not retaining the previous row values.
To retain the previous row values which you have added ,create an static string variable and concat the selected value to it.See the logic below..


private void Button1_Click(object sender, System.EventArgs e)
{
if(s.Length==0) {
    s = Convert.ToString(DropDownList1.SelectedItem);
}
else{
     s = s +","+DropDownList1.SelectedItem;
}
string[] sr=s.Split(',');
for(int j=0;j<sr.Length ;j++){
    dr = dt.NewRow();
    dr[0] = sr[j];
    dt.Rows.Add(dr);
}
InitializeDataGrid();
DataGrid1.DataBind();
}

    Here i have concatenated the selected items with the ',' delimiter
and i created the sr array with it.I have created rows with that collection.This is one way of retaining the rows.





Similar Threads
Thread Thread Starter Forum Replies Last Post
vb.net asp.net DataGrid krantips VS.NET 2002/2003 0 June 28th, 2006 01:22 AM
Asp.net question asp.netUser ASP.NET 1.0 and 1.1 Basics 1 September 13th, 2005 03:23 PM
ASP datagrid template textbox question chas036 ASP.NET 1.x and 2.0 Application Design 1 December 29th, 2003 04:01 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.