您好我的数据表有问题。我想在每次用户点击按钮时在数据表中插入新行。我有10个按钮,当用户点击按钮1时,它会插入新行,但是当用户点击按钮2时,现有的行被替换为新行。帮助!
////这里我调用了从按钮点击添加新行的方法
private void RemoveDuplicates (Data_Table dt)
{
if (ViewState["Markici"] != null)
{
dtCurrentTable = (DataTable)ViewState["Markici"];
if (dtCurrentTable.Rows.Count > 0)
{
for (int i = dtCurrentTable.Rows.Count - 1; i >= 0; i--)
{
if (i == 0)
{
break;
}
for (int j = i - 1; j >= 0; j--)
{
if (dtCurrentTable.Rows[i]["Cena1"].ToString() == dtCurrentTable.Rows[j]["Cena1"].ToString())
{
dtCurrentTable.Rows[i].Delete();
break;
}
}
}
}
dtCurrentTable.AcceptChanges();
// ViewState["Markici"] = dtCurrentTable;
// Repeater1.DataSource = dtCurrentTable;
// Repeater1.DataBind();
AddNewRecordRowToGrid();
AddNewRecordRowToGridf();
decimal vkupno = 0m;
vkupno += Convert.ToDecimal(Label6.Text);
decimal vkupno2 = 0m;
vkupno2 += Convert.ToDecimal(Label17.Text);
vkupno += vkupno2;
Label26.Text = vkupno.ToString();
//xml table
// DataSet ds = new DataSet();
// ds.Tables.Add(dtCurrentTable);
//binding Gridview with New Row
GridView2.DataSource = dtCurrentTable;
GridView2.DataBind();
}
}
//添加行的方法,在按钮单击9
中调用它 private void AddNewRecordRowToGrid()
{
int counter;
if (Request.Cookies["kasa"] == null)
counter = 0;
else
{
counter = int.Parse(Request.Cookies["kasa"].Value);
}
counter++;
Response.Cookies["kasa"].Value = counter.ToString();
Response.Cookies["kasa"].Expires = DateTime.Now.AddYears(2);
if (ViewState["Markici"] != null)
{
DataTable dtCurrentTable = (DataTable)ViewState["Markici"];
DataRow drCurrentRow = null;
if (dtCurrentTable.Rows.Count > 0)
{
for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
{
drCurrentRow = dtCurrentTable.NewRow();
drCurrentRow["FirmaID"] = Request.Cookies["firma"].Value;
drCurrentRow["Godina"] = Request.Cookies["godina"].Value;
drCurrentRow["KasaID"] = Request.Cookies["kasa"].Value;
drCurrentRow["MarkicaID"] = counter;
drCurrentRow["Datum"] = DateTime.Now;
drCurrentRow["Masa"] = Session["masa39"];
drCurrentRow["VrabotenID"] = Session["New"];
drCurrentRow["Artikal"] = Label3.Text;
drCurrentRow["Cena1"] = Label4.Text;
drCurrentRow["Kolicina"] = Label5.Text;
drCurrentRow["Smena"] = Session["smena1"];
drCurrentRow["VkIznos"] = Label6.Text;
drCurrentRow["VkDanok"] = Label8.Text;
drCurrentRow["SySDatum"] = DateTime.Now;
drCurrentRow["Vid"] = Label23.Text;
drCurrentRow["Edmera"] = Label10.Text;
drCurrentRow["ArtikalID"] = Label33.Text;
}
/*
for (int i = 0; i < dtCurrentTable.Rows.Count; i++)
{
if (dtCurrentTable.Rows[i].IsNull(0) == true)
{
dtCurrentTable.Rows[i].Delete();
dtCurrentTable.AcceptChanges();
}
}*/
//Removing initial blank row
if (dtCurrentTable.Rows[0][0].ToString() == "")
{
dtCurrentTable.Rows[0].Delete();
dtCurrentTable.AcceptChanges();
}
//Added New Record to the DataTable
dtCurrentTable.Rows.Add(drCurrentRow);
//storing DataTable to ViewState
ViewState["Markici"] = dtCurrentTable;
//binding Gridview with New Row
GridView2.DataSource = dtCurrentTable;
GridView2.DataBind();
}
}
}
// call this in button 10 click
private void AddNewRecordRowToGridf()
{
if (ViewState["Markici"] != null)
{
DataTable dtCurrentTable = (DataTable)ViewState["Markici"];
DataRow drCurrentRow = null;
if (dtCurrentTable.Rows.Count > 0)
{
for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
{
//Creating new row and assigning values
drCurrentRow = dtCurrentTable.NewRow();
drCurrentRow["FirmaID"] = Request.Cookies["firma"].Value;
drCurrentRow["Godina"] = Request.Cookies["godina"].Value;
drCurrentRow["KasaID"] = Request.Cookies["kasa"].Value;
drCurrentRow["MarkicaID"] = Request.Cookies["kasa"].Value;
drCurrentRow["Datum"] = DateTime.Now;
drCurrentRow["Masa"] = Session["masa39"];
drCurrentRow["VrabotenID"] = Session["New"];
drCurrentRow["Artikal"] = Label12.Text;
drCurrentRow["Cena1"] = Label13.Text;
drCurrentRow["Kolicina"] = Label11.Text;
drCurrentRow["Smena"] = Session["smena1"];
drCurrentRow["VkIznos"] = Label17.Text;
drCurrentRow["VkDanok"] = Label18.Text;
drCurrentRow["SySDatum"] = DateTime.Now;
drCurrentRow["Vid"] = Label24.Text;
drCurrentRow["Edmera"] = Label16.Text;
drCurrentRow["ArtikalID"] = Label34.Text;
}
for (int i = 0; i < dtCurrentTable.Rows.Count; i++)
{
if (dtCurrentTable.Rows[i].IsNull(0) == true)
{
dtCurrentTable.Rows[i].Delete();
dtCurrentTable.AcceptChanges();
}
}
/*
//Removing initial blank row
if (dtCurrentTable.Rows[0][0].ToString() == "")
{
dtCurrentTable.Rows[0].Delete();
dtCurrentTable.Rows[1].Delete();
dtCurrentTable.AcceptChanges();
}
*/
//Added New Record to the DataTable
dtCurrentTable.Rows.Add(drCurrentRow);
//storing DataTable to ViewState
ViewState["Markici"] = dtCurrentTable;
//binding Gridview with New Row
GridView2.DataSource = dtCurrentTable;
GridView2.DataBind();
}
}
protected void Button9_Click(object sender, EventArgs e)
{
RemoveDuplicates(dt);
}
protected void Button10_Click(object sender, EventArgs e)
{
RemoveDuplicates(dt);
}
// create DataTable
private void AddDefaultFirstRecord()
{
//creating DataTable
DataTable dt = new DataTable();
DataRow dr;
dt.TableName = "Markici";
//creating columns for DataTable
dt.Columns.Add(new DataColumn("FirmaID", typeof(System.Int32)));
dt.Columns.Add(new DataColumn("Godina", typeof(System.Int32)));
dt.Columns.Add(new DataColumn("KasaID", typeof(System.Int32)));
dt.Columns.Add(new DataColumn("MarkicaID", typeof(System.Int64)));
dt.Columns.Add(new DataColumn("Datum", typeof(System.DateTime)));
dt.Columns.Add(new DataColumn("Masa", typeof(System.Int32)));
dt.Columns.Add(new DataColumn("VrabotenID", typeof(System.Int32)));
dt.Columns.Add(new DataColumn("Artikal", typeof(System.String)));
dt.Columns.Add(new DataColumn("Cena1", typeof(System.String)));
dt.Columns.Add(new DataColumn("Kolicina", typeof(System.Decimal)));
dt.Columns.Add(new DataColumn("Smena", typeof(System.Int32)));
dt.Columns.Add(new DataColumn("VkIznos", typeof(System.Decimal)));
dt.Columns.Add(new DataColumn("VkDanok", typeof(System.Decimal)));
dt.Columns.Add(new DataColumn("SysDatum", typeof(System.DateTime)));
dt.Columns.Add(new DataColumn("Vid", typeof(System.String)));
dt.Columns.Add(new DataColumn("EdMera", typeof(System.String)));
dt.Columns.Add(new DataColumn("ArtikalID", typeof(System.String)));
dr = dt.NewRow();
dt.Rows.Add(dr);
ViewState["Markici"] = dt;
GridView2.DataSource = dt;
GridView2.DataBind();
}
答案 0 :(得分:0)
添加dtCurrentTable.AcceptChanges();在dtCurrentTable.Rows.Add(drCurrentRow)之后;
不要将DataTable保存到ViewState中。 DataTable是一个很大的对象。它会影响页面性能。您可以保留缓存或会话。
我的重新命名是缓存数据表数据源而不是数据表对象。