0

I have to show users entered parameters in Asp.net Gridview example some values from dropdownlist textboxes and startDate EndDate etc . I am getting these values from user and add them to a Temporary dataTable . i am adding each row to DatTable on every add button call it goes fine for first time means for first row when i try to enter another row on Add button Click i overWrites on previous row and shows me only one row.

Here is my code :

BusinessLayer.LaunchPortfolioTest objAddParametersToDataTable = new BusinessLayer.LaunchPortfolioTest();
DataTable TempTable;

if (Session["TempTable"] == null)
{
    TempTable = objAddParametersToDataTable.TempTableView();
    Session["IDcolumn"] = 1;
}
else
{
    Session["IDcolumn"] = Convert.ToInt16(Session["IDcolumn"]) + 1;
    TempTable = (DataTable)Session["TempTable"];
}
int ID = Convert.ToInt16(Session["IDcolumn"]);
string nome = Utilities.formatShortDate(Convert.ToDateTime(txtEndDate.Text.ToString()));
string priority = drpPriority.SelectedValue.ToString();
string portfolioCode = drpPortfolioCode.SelectedValue.ToString();
DateTime startDate = Convert.ToDateTime(txtStartDate.Text.ToString());
DateTime endDate = Convert.ToDateTime(txtEndDate.Text.ToString());
string currency = drpCalculationCurrency.SelectedValue.ToString();
string author = lblLoginnedUser.Text.ToString();
TimeSpan Difference = endDate.Date.AddDays(1) - startDate.Date;
//Adding Parameters to DataTable:
objAddParametersToDataTable.AddRow(ID, priority, author, nome, portfolioCode, Convert.ToString(startDate), Convert.ToString(endDate), currency, TempTable);

grdViewReport.DataSource = TempTable;
grdViewReport.DataBind();

Session.Add("TempTable", objAddParametersToDataTable.TempTableView());

TempTableView method:

public DataTable TempTableView()
{
    DataTable TempAnalysisTable = new DataTable();
    DataColumn identity = new DataColumn("IDcolumn", typeof(int));
    TempAnalysisTable.Columns.Add(identity);
    TempAnalysisTable.Columns.Add("Priority", typeof(string));
    TempAnalysisTable.Columns.Add("Author", typeof(string));
    TempAnalysisTable.Columns.Add("Name", typeof(string));
    TempAnalysisTable.Columns.Add("PortfolioCode", typeof(string));
    TempAnalysisTable.Columns.Add("StartDate", typeof(string));
    TempAnalysisTable.Columns.Add("EndDate", typeof(string));
    TempAnalysisTable.Columns.Add("Currency", typeof(string));        
    return TempAnalysisTable;
}

Add Method Code IS here:

public void AddRow(int id,string priority, string author, string name, string portfolioCode, string startDate, string endDate, string currency, DataTable TempAnalysisTable)
{
    TempAnalysisTable.Rows.Add(new object[] {id, priority, author, name, portfolioCode, startDate, endDate, currency });
}
Dev
  • 307
  • 1
  • 2
  • 12
  • _"no server side trip everything which i am doing on Client side i put all in Session"_ [`Session`](http://msdn.microsoft.com/en-us/library/system.web.sessionstate.httpsessionstate.aspx) lives on the server. Session values are stored in memory on the Web server, by default. – Tim Schmelter Apr 29 '14 at 09:22
  • you are right Tim Schmelter infect i should say no dataBase call instead of server side trip – Dev Apr 29 '14 at 09:29
  • `AddButton_Click` code ? – Ali Umair Apr 29 '14 at 10:19

2 Answers2

1

Am not sure for you scenario. But here is the thing for adding grid view dynamically without using database.

 protected void Page_Load(object sender, EventArgs e)
{
    if (IsPostBack == false)
    {
        DataTable date = new DataTable();
        date.Columns.Add("Column !", typeof(string));
        date.Columns.Add("Column 2", typeof(string));
        Session["dte"] = date;
     }
 }

protected void addbutton_Click(object sender, ImageClickEventArgs e)
{
    DataTable date = (DataTable)Session["dte"];
    DataRow dr = date.NewRow();
    dr["Column 1"] = TextBox1.Text.Trim();// Your Values
    dr["Column 2"] = TextBox2.Text.Trim();// Your Values
    date.Rows.Add(dr);
    GridView1.DataSource = date;
    GridView1.DataBind();
}

It may help you to over come this hurdle. Please let me know your further queries in this case.

gkrishy
  • 756
  • 1
  • 8
  • 33
  • Thanks for your reply scnerio is here my user will select some parameters form different controls like dropdownlist and text boxes . i want to show him his selected values in grid view he can select one or more row . He can also delete row from gridView just selecting a row and clicking on remove button . – Dev Apr 29 '14 at 09:58
  • Welcome. Change your values(TextBox, DropDown etc.,) instead of TextBox1.Text.Trim() and TextBox2.Text.Trim(). You can do for many values as much as you want. For gridview select and delete you need to add template field in grid view. Here is the [link](http://stackoverflow.com/questions/16977283/how-to-delete-a-row-on-gridview-on-button-click-and-generate-javacript-validatio), go through it and let me know. – gkrishy Apr 29 '14 at 10:11
  • Thanks again your proposed solution worked for me infect i was doing it in wrong i should add DataTable in Load instead Add method event. Thanks again for instant help. – Dev Apr 29 '14 at 10:19
0

You need to get the previous datatable from session and read it and put rows in it. This should be done at addrow AddButton_Click event.

Ali Umair
  • 1,386
  • 1
  • 21
  • 42