问题是这样的,我的界面有3个按钮,save ,add row ,del row.
我想先从数据库把数据取出显示在gridview中,gridView的第一列是checkbox,
然后点击add row的时候可以添加一行,点击del row的时候可以删除checkbox选中的行。
但是这两个操作都不更新数据库,直到最后点save才更新到数据库。
希望高手们能帮小弟尽快解决此问题!!感激涕零!!
我想先从数据库把数据取出显示在gridview中,gridView的第一列是checkbox,
然后点击add row的时候可以添加一行,点击del row的时候可以删除checkbox选中的行。
但是这两个操作都不更新数据库,直到最后点save才更新到数据库。
希望高手们能帮小弟尽快解决此问题!!感激涕零!!
这问题我都发了3个贴了,还是没有解决,真让人失望!!!
当然这些按钮可能都要自己来写了
或者做个事务了,点击按钮后才提交事务。
private void btnSelect_Click(object sender, EventArgs e)
{
DataSet ds = new DataSet();
string connStr = "user id=manager;password=foton;data source=ecudb";
string selectSql = "select vehicletoecusid as ID号,vehiclecode16 as 十六位码,filename as 文件名,filetype as 文件类型 from vehicletoecus where vehicletoecusid<779";
using (System.Data.OracleClient.OracleConnection conn = new System.Data.OracleClient.OracleConnection(connStr))
{
System.Data.OracleClient.OracleDataAdapter da = new System.Data.OracleClient.OracleDataAdapter(selectSql, connStr);
da.Fill(ds, "Table");
this.dataGridView1.DataSource = ds.Tables["Table"];
}
}
System.Collections.Hashtable addHt = new System.Collections.Hashtable();
private void btnAdd_Click(object sender, EventArgs e)
{
object[] cells ={1,"1234567890123456","FileName","FileType", };
DataTable dt=(DataTable)this.dataGridView1.DataSource;
dt.Rows.Add(cells);
this.dataGridView1.DataSource = dt;
if (addHt.ContainsKey(cells[0]))
{
addHt.Remove(cells[0]);
}
addHt.Add(cells[0], cells[1]);
}
System.Collections.Hashtable delHt = new System.Collections.Hashtable();
private void btnDel_Click(object sender, EventArgs e)
{
this.dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
for (int i = 0; i < this.dataGridView1.Rows.Count; i++)
{
if (this.dataGridView1.Rows[i].Cells[0].Value!=null)
{
if (delHt.Contains(this.dataGridView1.Rows[i].Cells["ID号"].Value.ToString()))
{
delHt.Remove(this.dataGridView1.Rows[i].Cells["ID号"].Value.ToString());
} delHt.Add(this.dataGridView1.Rows[i].Cells["ID号"].Value.ToString(),"kj");
this.dataGridView1.Rows.RemoveAt(i);
}
}
} private void btnSave_Click(object sender, EventArgs e)
{ foreach (object var in addHt)
{
string insertSql = "";
InsertDataBase();
}
foreach (object var in delHt)
{
string deleteSql = "";
DeleteRecord();
}
} private void DeleteRecord()
{
throw new Exception("The method or operation is not implemented.");
} private void InsertDataBase()
{
throw new Exception("The method or operation is not implemented.");
}
如果你数据库记录对应的是某个类那么就更好了,你在添加一行的时候创建个对象,然后给它的属性赋值,再对应到你数据库里边的字段。
太谢谢你了!