走過的大神請留步,下面是我的代碼,是gridview動態緩存實現增刪減的,但是我發現我裏面的刪除功能可以實現,但是新增和更新不行,更新時修改後也變成刪除了數據!

qq123321pig 2012-09-17 10:55:43
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
bind();
fill();
}

}

string tbname = "MT_STOCKLOC_CONTACT";
void fill()
{
DataTable table = new DataTable();
table.Columns.Add(new DataColumn("ID"));
table.Columns.Add(new DataColumn("ST_STAFFNO"));
table.Columns.Add(new DataColumn("ST_STAFFNAME"));
table.Columns.Add(new DataColumn("ST_POST"));
table.Columns.Add(new DataColumn("ST_DEPT"));


string sql = "select ST_STAFFNO,ST_STAFFNAME,ST_POST,ST_DEPT from " + tbname + " order by ST_STAFFNO";
LinkDatabase link = new LinkDatabase();
DataTable dt = link.SelectDatabase(sql);
for (int i = 0; i < dt.Rows.Count; i++)
{
DataRow row = table.NewRow();
row["ID"] = Guid.NewGuid().ToString();
row["ST_STAFFNO"] = dt.Rows[i]["ST_STAFFNO"].ToString();
row["ST_STAFFNAME"] = dt.Rows[i]["ST_STAFFNAME"].ToString();
row["ST_POST"] = dt.Rows[i]["ST_POST"].ToString();
row["ST_DEPT"] = dt.Rows[i]["ST_DEPT"].ToString();
table.Rows.Add(row);
}
GridView1.DataSource = table;
GridView1.DataBind();
ViewState["inidt"] = table;

}

protected void lbtnAddRow_Click(object sender, EventArgs e)
{
DataTable table = GetGridViewData();
DataRow newRow = table.NewRow();
newRow["ID"] = Guid.NewGuid().ToString();
table.Rows.Add(newRow);
GridView1.DataSource = table;
GridView1.DataBind();
}

private DataTable GetGridViewData()
{
DataTable table = new DataTable();
table.Columns.Add(new DataColumn("ID"));
table.Columns.Add(new DataColumn("ST_STAFFNO"));
table.Columns.Add(new DataColumn("ST_STAFFNAME"));
table.Columns.Add(new DataColumn("ST_POST"));
table.Columns.Add(new DataColumn("ST_DEPT"));
foreach (GridViewRow row in GridView1.Rows)
{
DataRow sourseRow = table.NewRow();
sourseRow["ID"] = row.Cells[2].Text;
sourseRow["ST_STAFFNO"] = ((TextBox)row.Cells[3].FindControl("txtno")).Text;
sourseRow["ST_STAFFNAME"] = ((TextBox)row.Cells[4].FindControl("txtname")).Text;
sourseRow["ST_POST"] = ((TextBox)row.Cells[5].FindControl("txtpost")).Text;
sourseRow["ST_DEPT"] = ((TextBox)row.Cells[6].FindControl("txtdept")).Text;
table.Rows.Add(sourseRow);
}
return table;
}

protected void btnDeleteRow_Click(object sender, EventArgs e)
{
DataTable table = GetGridViewData();
foreach (GridViewRow row in GridView1.Rows)
{
if (((HtmlInputCheckBox)row.Cells[1].FindControl("chkRow")).Checked)
{
foreach (DataRow dtRow in table.Rows)
{
if (dtRow["ID"].ToString() == row.Cells[2].Text)
{
table.Rows.Remove(dtRow);
break;
}
}
}
}
GridView1.DataSource = table;
GridView1.DataBind();
}
protected void btn_save_Click(object sender, EventArgs e)
{
DataTable dt_status = new DataTable();
dt_status.Columns.Add("ST_STAFFNO");
dt_status.Columns.Add("ST_STAFFNAME");
dt_status.Columns.Add("ST_POST");
dt_status.Columns.Add("ST_DEPT");
dt_status.Columns.Add("STATUS");

string str_sta = "";
LinkDatabase link = new LinkDatabase();

foreach (GridViewRow row in GridView1.Rows)
{
string st_no = ((TextBox)row.Cells[3].FindControl("txtno")).Text;
string st_name = ((TextBox)row.Cells[4].FindControl("txtname")).Text;
string st_post = ((TextBox)row.Cells[5].FindControl("txtpost")).Text;
string st_dept = ((TextBox)row.Cells[6].FindControl("txtdept")).Text;
DataRow[] drr = ((DataTable)ViewState["inidt"]).Select("ST_STAFFNO like '" +st_no+ "'");
if (drr.Length == 0)
{
try
{
link.UpdateDatabase("insert into " + tbname + " (ST_STAFFNO,ST_STAFFNAME,ST_POST,ST_DEPT)values('" + st_no + "','" + st_name + "','" + st_post + "','" + st_dept + "')");
}
catch (Exception ee) { Response.Write(ee.Message); }
}
else
{

if (st_name != drr[0].ItemArray[2].ToString() || st_post != drr[0].ItemArray[3].ToString() || st_dept !=drr[0].ItemArray[4].ToString())
{
try
{
link.UpdateDatabase("update " + tbname + " set ST_STAFFNO='" + st_no + "', ST_STAFFNAME='" + st_name + "', ST_POST='" + st_post + "', ST_DEPT='" + st_dept + "' where ST_STAFFNO='" + st_no + "'");
}
catch (Exception ee) { Response.Write(ee.Message); }

}

DataRow row1 = dt_status.NewRow();
row1["ST_STAFFNO"] = st_no;
row1["ST_STAFFNAME"] = st_name;
row1["ST_POST"] = st_post;
row1["ST_DEPT"] = st_dept;
row1["STATUS"] = str_sta;
dt_status.Rows.Add(row1);
row1.Delete();
}
}

for (int i = 0; i < ((DataTable)ViewState["inidt"]).Rows.Count; i++)
{
string stcode = ((DataTable)ViewState["inidt"]).Rows[i]["ST_STAFFNO"].ToString();
str_sta = "D";
foreach (GridViewRow row2 in GridView1.Rows)
{
if (stcode == ((TextBox)row2.Cells[3].FindControl("txtno")).Text)
{
str_sta = "";
continue;
}
}
if (str_sta == "D")
{
link.UpdateDatabase("delete from " + tbname + " where ST_STAFFNO='" + stcode + "'");
}

}
ViewState.Clear();
bind();
Response.Write("OK!");
}
...全文
136 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq123321pig 2012-09-17
  • 打赏
  • 举报
回复
沒人啦啊,自己頂一下先
qq123321pig 2012-09-17
  • 打赏
  • 举报
回复
額,一眼就看出來是抄的啦,但是我就是要這個功能!誰幫我查下錯啊
huayy 2012-09-17
  • 打赏
  • 举报
回复
哪抄来的代码,还得看书!

62,103

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧