ASP.Net Session中的DataTable

时间:2014-04-29 09:20:58

标签: asp.net

我必须在Asp.net Gridview示例中向用户显示一些来自下拉列表文本框和startDate EndDate等的值。我从用户那里获取这些值并将它们添加到Temporary dataTable。我在每个添加按钮调用时将每行添加到DatTable它第一次正常意味着第一行当我尝试在添加按钮上输入另一行时单击上一行中的over overWrites并仅向我显示一行。

这是我的代码:

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方法:

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方法代码在这里:

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 });
}

2 个答案:

答案 0 :(得分:1)

我不确定你的情况。但是,这是在不使用数据库的情况下动态添加网格视图的事情。

 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();
}

它可能会帮助你克服这个障碍。在这种情况下,请告诉我您的进一步询问。

答案 1 :(得分:0)

您需要从datatable获取之前的session并阅读并在其中添加行。这应该在addrow AddButton_Click事件中完成。