vb.net datagridview 控件上编辑后保存 200
我想实现在datagridview控件界面上添加修改删除再一个button里写上保存。点保存存入数据库。我的数据中有很多空值。不同类型的数据格式。。。...
我想实现在datagridview 控件界面上 添加 修改 删除 再一个button里写上保存。点保存存入数据库。
我的数据中有很多空值。不同类型的数据格式。。。 展开
我的数据中有很多空值。不同类型的数据格式。。。 展开
4个回答
展开全部
做过一个类似的C#的,以下是C#的代码供参考
private DataTable dbconn(string strSql) //creat a method for datatable model
{
string Con5 = string.Format(@"provider=Microsoft.ACE.oledb.12.0;Jet oledb:Database Password =Jabil123456;User ID =Admin;Data source ={0}\Ele.accdb",System.Windows.Forms.Application.StartupPath);
OleDbConnection conn = new OleDbConnection(Con5);
this.adapter = new OleDbDataAdapter(strSql, conn); //instantiation SqlDataAdapter object
DataTable dtSelect = new DataTable(); //instantiation DataTable object
int rnt = this.adapter.Fill(dtSelect); //Fill DataTable object
return dtSelect; //return DataTable object
}
private Boolean dbUpdate() //function dbUpdate
{
string strSql = "select * from [Materials]"; //statement SQL
DataTable dtUpdate = new DataTable(); //instantiation DataTable
dtUpdate = this.dbconn(strSql);
dtUpdate.Rows.Clear(); //Clear datatable
DataTable dtShow = new DataTable(); //instantiation DataTable
dtShow = (DataTable)this.dataGridView2.DataSource;
for (int i = 0; i < dtShow.Rows.Count; i++)
{
dtUpdate.ImportRow(dtShow.Rows[i]);//use ImportRow method to paste the value of dtShow
}
try
{
OleDbCommandBuilder CommandBuiler; //statement SqlCommandBuilder variable
CommandBuiler = new OleDbCommandBuilder(this.adapter);
this.adapter.Update(dtUpdate); //update data
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString()); //pop-up prompt for unnormal situation
return false;
}
dtUpdate.AcceptChanges(); //change successful
return true;
}
private void dataGridView2_CellClick (object sender, DataGridViewCellEventArgs e)
{
i = e.RowIndex;//record current row index
}
public bool ExportcurrentData()
{
if (dataGridView2.Rows.Count == 0)
{
return false;
}
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application(); //creat excel object
excel.Application.Workbooks.Add(true);
for (int i = 0; i < dataGridView2.ColumnCount; i++) //creat field name
{
excel.Cells[1, i + 1] = dataGridView2.Columns[i].HeaderText;
}
for (int i = 0; i < dataGridView2.RowCount; i++) //fill data
//row cycle
{
for (int j = 0; j < dataGridView2.ColumnCount; j++)
//Column cycle
{
if (dataGridView2[j, i].ValueType == typeof(string))
{
excel.Cells[i + 2, j + 1] = "'" + dataGridView2.Rows[i].Cells[j].Value.ToString();
}
else
{
excel.Cells[i + 2, j + 1] = dataGridView2.Rows[i].Cells[j].Value.ToString();
}
}
}
excel.Visible = false;
excel.DisplayAlerts = false;
excel.AlertBeforeOverwriting = false;
excel.Save();
excel.Quit();
return true;
}
void Timer1Tick(object sender, EventArgs e)
{
this.Opacity += 0.2;
label1.Left += -2;
if (label1.Right<=0)
{
label1.Left= this.Width;
}
}
private void exportDataToExcelToolStripMenuItem_Click(object sender, EventArgs e)
{
ExportcurrentData();
}
private void deleteDataDToolStripMenuItem_Click(object sender, EventArgs e)
{
dataGridView2.AllowUserToDeleteRows = true; //allow user to delete row
foreach (DataGridViewRow r in dataGridView2.SelectedRows)
{
if (MessageBox.Show("是否删除数据?", "系统消息", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.OK)
{
if (!r.IsNewRow)
{
dataGridView2.Rows.Remove(r);
ChangeData_Click(sender, e);
}
}
}
}
private void backBToolStripMenuItem_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
this.Close();
form2.Show();
}
private void ChangeData_Click(object sender, EventArgs e)
{
if (MessageBox.Show("是否保存数据?", "系统消息", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.OK)
{
if (dbUpdate()) //judgement function dbUpdate execute to true or false, if true then.....
{
MessageBox.Show("修改成功!"); //pop-up prompt
dataGridView2.Update();
}
search_Click(sender, e); //re-load
}
}
private DataTable dbconn(string strSql) //creat a method for datatable model
{
string Con5 = string.Format(@"provider=Microsoft.ACE.oledb.12.0;Jet oledb:Database Password =Jabil123456;User ID =Admin;Data source ={0}\Ele.accdb",System.Windows.Forms.Application.StartupPath);
OleDbConnection conn = new OleDbConnection(Con5);
this.adapter = new OleDbDataAdapter(strSql, conn); //instantiation SqlDataAdapter object
DataTable dtSelect = new DataTable(); //instantiation DataTable object
int rnt = this.adapter.Fill(dtSelect); //Fill DataTable object
return dtSelect; //return DataTable object
}
private Boolean dbUpdate() //function dbUpdate
{
string strSql = "select * from [Materials]"; //statement SQL
DataTable dtUpdate = new DataTable(); //instantiation DataTable
dtUpdate = this.dbconn(strSql);
dtUpdate.Rows.Clear(); //Clear datatable
DataTable dtShow = new DataTable(); //instantiation DataTable
dtShow = (DataTable)this.dataGridView2.DataSource;
for (int i = 0; i < dtShow.Rows.Count; i++)
{
dtUpdate.ImportRow(dtShow.Rows[i]);//use ImportRow method to paste the value of dtShow
}
try
{
OleDbCommandBuilder CommandBuiler; //statement SqlCommandBuilder variable
CommandBuiler = new OleDbCommandBuilder(this.adapter);
this.adapter.Update(dtUpdate); //update data
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString()); //pop-up prompt for unnormal situation
return false;
}
dtUpdate.AcceptChanges(); //change successful
return true;
}
private void dataGridView2_CellClick (object sender, DataGridViewCellEventArgs e)
{
i = e.RowIndex;//record current row index
}
public bool ExportcurrentData()
{
if (dataGridView2.Rows.Count == 0)
{
return false;
}
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application(); //creat excel object
excel.Application.Workbooks.Add(true);
for (int i = 0; i < dataGridView2.ColumnCount; i++) //creat field name
{
excel.Cells[1, i + 1] = dataGridView2.Columns[i].HeaderText;
}
for (int i = 0; i < dataGridView2.RowCount; i++) //fill data
//row cycle
{
for (int j = 0; j < dataGridView2.ColumnCount; j++)
//Column cycle
{
if (dataGridView2[j, i].ValueType == typeof(string))
{
excel.Cells[i + 2, j + 1] = "'" + dataGridView2.Rows[i].Cells[j].Value.ToString();
}
else
{
excel.Cells[i + 2, j + 1] = dataGridView2.Rows[i].Cells[j].Value.ToString();
}
}
}
excel.Visible = false;
excel.DisplayAlerts = false;
excel.AlertBeforeOverwriting = false;
excel.Save();
excel.Quit();
return true;
}
void Timer1Tick(object sender, EventArgs e)
{
this.Opacity += 0.2;
label1.Left += -2;
if (label1.Right<=0)
{
label1.Left= this.Width;
}
}
private void exportDataToExcelToolStripMenuItem_Click(object sender, EventArgs e)
{
ExportcurrentData();
}
private void deleteDataDToolStripMenuItem_Click(object sender, EventArgs e)
{
dataGridView2.AllowUserToDeleteRows = true; //allow user to delete row
foreach (DataGridViewRow r in dataGridView2.SelectedRows)
{
if (MessageBox.Show("是否删除数据?", "系统消息", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.OK)
{
if (!r.IsNewRow)
{
dataGridView2.Rows.Remove(r);
ChangeData_Click(sender, e);
}
}
}
}
private void backBToolStripMenuItem_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
this.Close();
form2.Show();
}
private void ChangeData_Click(object sender, EventArgs e)
{
if (MessageBox.Show("是否保存数据?", "系统消息", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.OK)
{
if (dbUpdate()) //judgement function dbUpdate execute to true or false, if true then.....
{
MessageBox.Show("修改成功!"); //pop-up prompt
dataGridView2.Update();
}
search_Click(sender, e); //re-load
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
说个思路,这个是datatable,添加一列状态(是否添加,修改,删除),点击保存的时候,循环变量这一列的值,再做相应的操作。
撤销编辑,只要重新绑定datatable就好,或者把状态清空。
撤销编辑,只要重新绑定datatable就好,或者把状态清空。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
最简单的方法就是添加datagridview控件后可以把插入、删除选中就有这些功能了,封装好的
追问
选中了,但是不修改到数据库
追答
那估计就是你数据库的问题了,有报错吗?好好对对数据库字段看看有没有定义错的地方
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你把你文件放在我改好给你!
追问
做了一点都删了,现在查资料呢
追答
有源代码,你放上面,我下载下来,改好放上面!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询