先贴两幅图片问题是这样,我连续新增几条没问题,然后选中新增的记录再修改,就报错,我跟踪内存,发现内存的dataset中,
根本没有新增的几条记录。两个界面我是这样处理的,form1里面有dataGridView和一个dataSet,初始化form2时,把dataGridView和dataSet
都按地址传递过去,form2里增加完或修改完,直接操作dataGridView和dataSet来改变form1。肯定我处理方式有问题,网上看了很多数据绑定,有说静态方法,静态变量,都晕了
根本没有新增的几条记录。两个界面我是这样处理的,form1里面有dataGridView和一个dataSet,初始化form2时,把dataGridView和dataSet
都按地址传递过去,form2里增加完或修改完,直接操作dataGridView和dataSet来改变form1。肯定我处理方式有问题,网上看了很多数据绑定,有说静态方法,静态变量,都晕了
{
Bill.Bill MyDataObj = new Bill.Bill();
DataSet _ds = new DataSet(); public Form2()
{
InitializeComponent();
} /// <summary>
/// 车辆列表绑定
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Form2_Load(object sender, EventArgs e)
{
BindDataGridView();
} public void BindDataGridView()
{
try
{
_ds = MyDataObj.getCarInfoDataSet();
dataGridView1.DataSource = _ds;
dataGridView1.DataMember = "Table";
}
catch (Exception ee)
{
lab_Error.Text = ee.Message;
MessageBox.Show(ee.Message);
}
} //添加一条记录
private void btn_Add_Click(object sender, EventArgs e)
{
Form3 f3 = new Form3(true);
f3.Show(); //初始页面
f3.setDataGridView(ref dataGridView1,"");
} //编辑一行数据
private void btn_Edit_Click(object sender, EventArgs e)
{
try
{
int strIndex = dataGridView1.SelectedRows[0].Index;
string strID = _ds.Tables[0].Rows[strIndex]["CarID"].ToString(); Form3 f3 = new Form3(false); //初始页面
f3.setDataGridView(ref dataGridView1, strID); //界面赋值
f3.SetFormValue();
f3.setValueLience(_ds.Tables[0].Rows[strIndex]["license"].ToString());
f3.setValueCarName(_ds.Tables[0].Rows[strIndex]["CarName"].ToString());
f3.setValueMan(_ds.Tables[0].Rows[strIndex]["Man"].ToString());
f3.setValueCreateTime(_ds.Tables[0].Rows[strIndex]["CreateTime"].ToString());
f3.setValueLookUpDate(_ds.Tables[0].Rows[strIndex]["LookUpDate"].ToString()); f3.SetEnable(); f3.ShowDialog(); }
catch (Exception ee)
{
string strError = ee.Message;
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace WindowsApplication2
{
public partial class Form3 : Form
{
private bool bIsNew; private Bill.Bill userData= new Bill.Bill();
private DataGridView _DataGridViewTemp;//父一级页面dataGridView
private string _strID; public void setDataGridView(ref DataGridView dg,string strID)//赋值
{
_DataGridViewTemp = dg;
_strID = strID;
} public Form3()
{
bIsNew = false;
InitializeComponent();
}
public Form3(bool NewState)
{
bIsNew = NewState;
InitializeComponent(); //窗口填充数据 }
/// <summary>
/// //提交保存车辆信息
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btn_commit_Click(object sender, EventArgs e)
{
try
{
string strID = System.Guid.NewGuid().ToString();
string strLience = text_license.Text;
string strCarName = text_CarName.Text;
string strMan = text_Man.Text; if (bIsNew)//新增
{
string strSql = "insert into CarInfo([CarID],[license],[Man],[CarName])";
strSql += " values('" + strID + "','" + strLience + "','" + strMan + "','" + strCarName + "')";
userData.AddCarInfo(strSql);
}
else //更新
{
string strSql = "update CarInfo set [license]='" + strLience + "',[Man]='" + strMan + "',[CarName]='" + strCarName + "' ";
strSql += " where CarID = '" + _strID + "';";
userData.UpdataCarInfo(strSql);
} DataGridViewAddRow();
}
catch (Exception ee)
{
string strError = ee.Message;
lab_Error.Text = strError;
}
this.Close();
}
/// <summary>
/// //刷新页面DataGridView
/// </summary>
private void DataGridViewAddRow()
{
try
{
_DataGridViewTemp.DataSource = userData.getCarInfoDataSet();
_DataGridViewTemp.DataMember = "Table";
}
catch (Exception ee)
{
lab_Error.Text = ee.Message;
MessageBox.Show(ee.Message);
}
} //取消
private void btn_reset_Click(object sender, EventArgs e)
{
this.Close();
} //界面赋值
public void SetFormValue()
{
} public void setValueLience(string value)
{
text_license.Text = value;
} public void setValueCarName(string value)
{
text_CarName.Text = value;
} public void setValueMan(string value)
{
text_Man.Text = value;
} public void setValueCreateTime(string value)
{
text_CreateTime.Text = value;
}
public void setValueLookUpDate(string value)
{
text_LookUpDate.Text = value;
} //界面按钮可见性
public void SetEnable()
{
} }
}
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.IO;
using System.Windows.Forms;namespace Bill
{
public class Bill
{
private static string strConnection = "Provider=Microsoft.Jet.OleDb.4.0;Data Source=" + Application.StartupPath + @"\manage.mdb";
/// <summary>
/// 返回车辆信息DataSet
/// </summary>
/// <returns></returns>
public DataSet getCarInfoDataSet()
{
try
{
OleDbConnection Conn = new OleDbConnection(strConnection);
Conn.Open();
OleDbDataAdapter adp = new OleDbDataAdapter("Select * from CarInfo", Conn);
Conn.Close();
DataSet ds = new DataSet();
adp.Fill(ds, "Table");
return ds;
}
catch (Exception e)
{
throw e;
}
}
/// <summary>
/// 返回定位记录信息DataSet
/// </summary>
/// <returns></returns>
public DataSet getRecordInfoDataSet()
{
try
{
OleDbConnection Conn = new OleDbConnection(strConnection);
OleDbDataAdapter adp = new OleDbDataAdapter("Select * from RecordInfo", Conn);
DataSet ds = new DataSet();
adp.Fill(ds, "Table");
return ds;
}
catch (Exception e)
{
throw e;
}
} /// <summary>
/// 返回一条 车辆信息
/// </summary>
/// <returns></returns>
public DataRow getCarInfo(string strID)
{
try
{
OleDbConnection Conn = new OleDbConnection(strConnection);
OleDbCommand cmd = new OleDbCommand("Select * from CarInfo where CarID ='" + strID + "';",Conn);
OleDbDataAdapter adp = new OleDbDataAdapter(cmd);
DataSet ds = new DataSet();
adp.Fill(ds, "Table");
DataRow dr =ds.Tables[0].Rows[0];
return dr;
}
catch (Exception e)
{
throw e;
}
} /// <summary>
/// 返回一条 记录信息
/// </summary>
/// <returns></returns>
public DataRow getRecordInfo(string strID)
{
try
{
OleDbConnection Conn = new OleDbConnection(strConnection);
OleDbCommand cmd = new OleDbCommand("Select * from RecordInfo where RecordID ='" + strID + "';", Conn);
OleDbDataAdapter adp = new OleDbDataAdapter(cmd);
DataSet ds = new DataSet();
adp.Fill(ds, "Table");
DataRow dr = ds.Tables[0].Rows[0];
return dr;
}
catch (Exception e)
{
throw e;
}
} /// <summary>
/// 添加一条 车辆信息
/// </summary>
/// <returns></returns>
public bool AddCarInfo(string strSql)
{
try
{
OleDbConnection Conn = new OleDbConnection(strConnection);
OleDbCommand cmd = new OleDbCommand(strSql,Conn);
Conn.Open(); bool bSeccuss = System.Convert.ToBoolean(cmd.ExecuteNonQuery()); Conn.Close();
return bSeccuss;
}
catch (Exception e)
{
throw e;
}
}
/// <summary>
/// 添加一条 定位记录信息
/// </summary>
/// <returns></returns>
public bool AddRecordInfo(string strSql)
{
try
{
OleDbConnection Conn = new OleDbConnection(strConnection);
OleDbCommand cmd = new OleDbCommand(strSql, Conn);
Conn.Open(); bool bSeccuss = System.Convert.ToBoolean(cmd.ExecuteNonQuery()); Conn.Close();
return bSeccuss;
}
catch (Exception e)
{
throw e;
}
}
/// <summary>
/// 更新一条 车辆信息
/// </summary>
/// <returns></returns>
public bool UpdataCarInfo(string strSql)
{
try
{
OleDbConnection Conn = new OleDbConnection(strConnection);
OleDbCommand cmd = new OleDbCommand(strSql, Conn);
Conn.Open(); bool bSeccuss = System.Convert.ToBoolean(cmd.ExecuteNonQuery()); Conn.Close();
return bSeccuss;
}
catch (Exception e)
{
throw e;
}
} /// <summary>
/// 删除一条 车辆信息
/// </summary>
/// <returns></returns>
public bool DelCarInfo(string strID)
{
OleDbCommand cmd = new OleDbCommand();
OleDbConnection Conn = new OleDbConnection(strConnection);
try
{
cmd.Connection = Conn;
Conn.Open(); cmd.Transaction = Conn.BeginTransaction();//开启事务
cmd.CommandText = "delete from CarInfo where CarID'" + strID + "';";//删车辆信息
cmd.ExecuteNonQuery();
cmd.CommandText = "delete from RecordInfo where CarID = '" + strID + "'";//删所有与该车辆有关的定位记录
cmd.ExecuteNonQuery(); cmd.Transaction.Commit();//提交 Conn.Close();
return true;
}
catch (Exception e)
{
cmd.Transaction.Rollback();//回滚
throw e;
}
} /// <summary>
/// 删除一条 定位记录信息
/// </summary>
/// <returns></returns>
public bool DelRecordInfo(string strID)
{
try
{
OleDbConnection Conn = new OleDbConnection(strConnection);
OleDbCommand cmd = new OleDbCommand("delete from RecordInfo where RecordID ='" + strID + "';",Conn);
bool bSuccess = System.Convert.ToBoolean(cmd.ExecuteNonQuery());
return bSuccess;
}
catch (Exception e)
{
throw e;
}
}
}
}
{
dataGridView2.Rows.Add();
dataGridView2.Rows[rows].Cells[2].Value = d1.Rows[rows][0].ToString().Trim();
你要修改的通过对dataGridView焦点的设置。我觉得不需要这样,倒不如在FORM1中直接读取数据库中已经修改的字段。通过数据库,不要直接在dataGridView指尖传递数据。
希望可以帮助到你。