{不能通过已删除的行访问该行的信息}运行下面代码编辑删除按钮都会出现图中提示,求大神!

FLORANCY 2016-07-18 04:21:57
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using MasterStock;

namespace MasterStock
{
public partial class Form2 : Form
{

DB db = new DB();
public int rcd;
public Boolean bl = true;
SqlDataAdapter MasterAdapter = new SqlDataAdapter();
SqlCommand MasterCmd = new SqlCommand();
DataTable MasterDt = new DataTable();
SqlCommandBuilder MasterCbd;
SqlTransaction Sqltran;
DataRow DR;
public int state;

private void OpenMasterTable()
{
string Sql = "select * from 路灯数据 order by ID asc";
//创建一个Command
MasterCmd = new SqlCommand(Sql, db.thisSqlconnection);
//创建一个Adapter(数据适配器)
MasterAdapter = new SqlDataAdapter();
//Apapter使用Command的方法
MasterAdapter.SelectCommand = MasterCmd;
//创建一个dataTable。
MasterDt = new DataTable();
MasterAdapter.Fill(MasterDt);
//把Adapter中的数据填充到DataTable中
dataGridView1.AutoGenerateColumns = false;//不会自动增加列。
//dataGridview的数据源连接DataTable
dataGridView1.DataSource = MasterDt;
//datagridview设为只读
dataGridView1.ReadOnly = true;
//datatable中的列绑定到datagridview的列中
dataGridView1.Columns[0].DataPropertyName = "ID";
dataGridView1.Columns[1].DataPropertyName = "type";
dataGridView1.Columns[2].DataPropertyName = "height";
dataGridView1.Columns[3].DataPropertyName = "light";
dataGridView1.Columns[4].DataPropertyName = "breakdown";
dataGridView1.Columns[5].DataPropertyName = "NDVI";
//datagrid不自动增加。
dataGridView1.AllowUserToAddRows = false;
// datagrid不能自动删除。
dataGridView1.AllowUserToDeleteRows = false;
LoadShow();//加上
}

private void LoadShow()
{
if (MasterDt.Rows.Count > 0)
{
textBox1.Text = MasterDt.Rows[rcd]["ID"].ToString();
textBox2.Text = MasterDt.Rows[rcd]["type"].ToString();
textBox3.Text = MasterDt.Rows[rcd]["height"].ToString();
textBox4.Text = MasterDt.Rows[rcd]["light"].ToString();
textBox5.Text = MasterDt.Rows[rcd]["breakdown"].ToString();
textBox6.Text = MasterDt.Rows[rcd]["NDVI"].ToString();
this.dataGridView1.CurrentCell = this.dataGridView1.Rows[rcd].Cells[0];
}
else
{
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
textBox4.Text = "";
textBox5.Text = "";
textBox6.Text = "";
}
}

private void btnState()
{
if (bl == true)
{
tbtNew.Enabled = true;
tbtEdit.Enabled = true;
tbtDel.Enabled = true;
tbtSave.Enabled = false;
tbtCancel.Enabled = false;
textBox1.ReadOnly = true;
textBox2.ReadOnly = true;
textBox3.ReadOnly = true;
textBox4.ReadOnly = true;
textBox5.ReadOnly = true;
textBox6.ReadOnly = true;
}
else
{
tbtNew.Enabled = false;
tbtEdit.Enabled = false;
tbtDel.Enabled = false;
tbtSave.Enabled = true;
tbtCancel.Enabled = true;
textBox1.ReadOnly = false;
textBox2.ReadOnly = false;
textBox3.ReadOnly = false;
textBox4.ReadOnly = false;
textBox5.ReadOnly = false;
textBox6.ReadOnly = false;
}
}





public Form2()
{
InitializeComponent();
OpenMasterTable();
state = 0;
bl = true;
btnState();
}

private void tbtNew_Click(object sender, EventArgs e)
{
bl = false; btnState();
state = 1;
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
textBox4.Text = "";
textBox5.Text = "";
textBox6.Text = "";

}

private void tbtEdit_Click(object sender, EventArgs e)
{
bl = false;
btnState();
state = 2;
DR = MasterDt.Rows[rcd];
}

private void tbtDel_Click(object sender, EventArgs e)
{
if (MasterDt.Rows.Count > 0)
{
MessageBoxButtons Mbbs = MessageBoxButtons.OKCancel;
DialogResult Result = MessageBox.Show("是否确认 删除此信息?", "提示", Mbbs);
if (Result == System.Windows.Forms.DialogResult.OK)
{
MasterCbd = new SqlCommandBuilder(MasterAdapter);
Sqltran = db.thisSqlconnection.BeginTransaction();
MasterCmd.Transaction = Sqltran; int introw = 0;
try
{
introw = rcd;
DR = MasterDt.Rows[rcd];
DR.Delete();
MasterAdapter.Update(MasterDt);
Sqltran.Commit();
}
catch (Exception Ex)
{
Sqltran.Rollback();
MessageBox.Show(Ex.ToString(), "提示");
}
finally
{
bl = true;
btnState();
state = 0;
if (introw == MasterDt.Rows.Count) { rcd = introw - 1; } else { rcd = introw; } LoadShow();
}
}
}
}

private void tbtSave_Click(object sender, EventArgs e)
{
MasterCbd = new SqlCommandBuilder(MasterAdapter);
Sqltran = db.thisSqlconnection.BeginTransaction();
MasterCmd.Transaction = Sqltran;
try
{
switch (state)
{
case 1:
AddNew();//此处是一个添加的数据过程
rcd = MasterDt.Rows.Count - 1;
this.dataGridView1.CurrentCell = this.dataGridView1.Rows[rcd].Cells[0];
break;
case 2:
Edit();//此处是一个编辑数据的过程
break;
}
Sqltran.Commit();
}
catch (Exception Ex)
{
Sqltran.Rollback();
MessageBox.Show(Ex.ToString(), "提示");
return;
}
finally
{
bl = true;
btnState();
state = 0;
}
}
private void AddNew()
{
if (textBox1.Text != "" && textBox2.Text != "")
{
DR = MasterDt.NewRow();
DR["ID"] = textBox1.Text;
DR["type"] = textBox2.Text;
DR["height"] = textBox3.Text;
DR["light"] = textBox4.Text;
DR["breakdown"] = textBox5.Text;
if (textBox6.Text == "")
{
DR["NDVI"] = "0";

}
else
{
DR["NDVI"] = textBox6.Text;

}
MasterDt.Rows.Add(DR);
MasterAdapter.Update(MasterDt);
}
else
{
MessageBox.Show("请完整输入信息", "提示");
}
}

private void Edit()
{
if (textBox1.Text != "" && textBox2.Text != "")
{
DR.BeginEdit();
DR["ID"] = textBox1.Text;
DR["type"] = textBox2.Text;
DR["height"] = textBox3.Text;
DR["light"] = textBox4.Text;
DR["breakdown"] = textBox5.Text;
if (textBox6.Text == "")
{
DR["NDVI"] = "0";
}
else
{
DR["NDVI"] = textBox6.Text;
}
DR.EndEdit();
MasterAdapter.Update(MasterDt);
MasterDt.AcceptChanges();
}
else
{
MessageBox.Show("请完整输入信息", "提示");
}
}


private void tbtCancel_Click(object sender, EventArgs e)
{
bl = true;
btnState();
state = 0;
OpenMasterTable();
rcd = 0; LoadShow();
}

private void tbtClose_Click(object sender, EventArgs e)
{

}

private void textBox6_KeyPress(object sender, KeyPressEventArgs e)
{
e.Handled = true;
if ((e.KeyChar >= '0' && e.KeyChar <= '9') || (e.KeyChar == (char)8))
{
e.Handled = false;
}
}


}
}
...全文
130 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
a13532725880 2016-07-24
  • 打赏
  • 举报
回复
当资料的状态为 删除 时,获取删除行的资料时要加上 origin

111,129

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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