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

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


}
}
...全文
131 1 打赏 收藏 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
a13532725880 2016-07-24
  • 打赏
  • 举报
回复
当资料的状态为 删除 时,获取删除行的资料时要加上 origin
【期刊论文复现】多元宇宙算法解电力系统多目标优化问题(Matlab实现)内容概要:本文复现了期刊论文中提出的多元宇宙算法(Multi-Verse Optimizer, MVO)用于解电力系统中的多目标优化调度问题,采用Matlab进代码实现。该算法受宇宙中黑洞、白洞和虫洞等天文现象启发,通过数学建模模拟多目标搜索过程,具有较强的全局寻优能力和收敛性能。文中将其应用于典型的电力系统经济调度场景,如考虑燃料成本、排放量等多个优化目标的微电网或多能源系统调度问题,通过构建合理的数学模型和约束条件,实现对多个冲突目标的协同优化。同时,文章提供了完整的代码框架与仿真结果,便于读者理解和复现。; 适合人群:具备一定Matlab编程基础和优化算法基础知识的研究生、科研人员及从事电力系统优化调度工作的工程师。; 使用场景及目标:①学习和掌握新型智能优化算法——多元宇宙算法的基本原理与实现方法;②将其应用于电力系统经济调度、微电网优化、多目标决策等领域的模型解;③为科研工作提供可复现的代码参考和技术支持。; 阅读建议:建议读者结合Matlab代码理解算法实现细节,重点关注目标函数建模、约束处理及MVO算法核心迭代逻辑,并尝试在不同测试系统中进参数调优与性能对比,以深入掌握其应用技巧。
内容概要:本文针对计及光伏波动性的主动配电网有功无功协调优化问题,提出一种基于多目标粒子群优化算法(MOPSO)的协同优化方案,并通过Matlab代码实现仿真验证。研究充分考虑光伏发电的随机性与间歇性特征,构建了一个综合有功功率调节、无功功率补偿、电压质量提升及网络损耗最小化的多目标优化模型,旨在提升高渗透率光伏接入下主动配电网的稳定性与经济性。通过优化算法解实现有功与无功资源的协调调度,有效缓解因光伏出力波动引起的电压越限与潮流分布不均等问题,增强了系统的调节能力与鲁棒性。; 适合人群:具备电力系统分析基础和Matlab编程能力的科研人员,以及电气工程、自动化等相关专业的研究生和高年级本科生。; 使用场景及目标:①用于研究高比例可再生能源接入背景下主动配电网的优化策略;②为电力系统仿真教学与科研提供可复现的算法模型与代码实例,目标是掌握多目标智能优化算法在电力系统优化调度中的建模方法、实现流程及其工程应用价值。; 阅读建议:建议读者结合Matlab代码逐段理解算法实现细节,重点关注目标函数的设计、约束条件的处理方式以及粒子群算法的改进策略,同时可通过调整参数设置、测试不同场景以深入理解优化机制与系统响应特性。

111,128

社区成员

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

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

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