if (MessageBoxShow("是否保存数据?", " *** 作提示", MessageBoxButtonsOKCancel, MessageBoxIconQuestion, MessageBoxDefaultButtonButton2) == DialogResultOK)
{
dataGridView1EndEdit();
OleDbDataAdapter ada = new OleDbDataAdapter();
DataTable dt = (DataTable)dataGridView1DataSource;
//String connectionString = @"provider=MicrosoftJetOLEDB40;Data Source=" + ApplicationStartupPath + @"\db3mdb";
//OleDbConnection conn = new OleDbConnection(connectionString);
adaSelectCommand = new OleDbCommand("select from StudentInfo", conn);
OleDbCommandBuilder builder = new OleDbCommandBuilder(ada);
adaUpdateCommand = builderGetUpdateCommand();
try
{
adaUpdate(dt);
dtAcceptChanges();
MessageBoxShow(" *** 作成功!数据已更新至数据库", " *** 作提示", MessageBoxButtonsOK, MessageBoxIconInformation, MessageBoxDefaultButtonButton2);
}
catch (Exception ex)
{
MessageBoxShow(exMessage);
}
finally
{
connClose();
}
}
下面是实现的代码: DataTable table = new DataTable();//初始化一个DataTable对象 string sqlConnectionString = "Data Source=ComputerName-PC;Initial Catalog=charge_sys;User ID=sa;PWD=123456;"; SqlConnection sqlConnection = new SqlConnection(sqlConnectionString);//连接数据库 SqlCommand sqlCommand = new SqlCommand("select from testB", sqlConnection); SqlDataAdapter sqlAdap = new SqlDataAdapter(sqlCommand); SqlCommandBuilder sqlBuilder = new SqlCommandBuilder(sqlAdap);//这句挺重要的,好像是批量更新的时候用 //DataSet dtst = new DataSet(); 用dataSet也可以完成,下面有对应实现的代码 sqlConnectionOpen(); sqlAdapFill(table); //table = dtstTables["testB"]; DataRow dr = tableRows[0];//第一行 //drBeginEdit(); //drEndEdit(); 貌似这两句主要是数据绑定到控件的时候才用 for (int i = 0; i < tableRowsCount; i++) //修改table中每一行的内容 { dr = tableRows[i]; dr["f"] = 3; //字段名为f } //testB表中必须存在主键,否则无法更新// 这个很重要 sqlAdapUpdate(table); //sqlAdapUpdate(dtst, "testB"); sqlConnectionClose(); MessageBoxShow("aa"); 下面是DataTable与dataGridView 控件绑定,当dataGridView中的数据改变时,更新数据库的代码: DataTable和dataGridView控件绑定代码:private void Form1_Load(object sender, EventArgs e) { string sqlConnectionString = "Data Source=ComputerName-PC;Initial Catalog=charge_sys;User ID=sa;PWD=123456;"; SqlConnection sqlConnection = new SqlConnection(sqlConnectionString);//初始化sqlconnection DataTable table = new DataTable(); SqlDataAdapter sqlAdap = new SqlDataAdapter("select from testB ", sqlConnection); sqlConnectionOpen(); sqlAdapFill(table); sqlConnectionClose(); //将testB表中的数据显示在dataGridView中 thisdataGridView1DataSource = table; } 实现将dataGridView中的改动更新到数据库代码:private void butUpdate_Click(object sender, EventArgs e) { DataTable table = new DataTable(); table = (DataTable)thisdataGridView1DataSource; string sqlConnectionString = "Data Source=WangHaitao-PC;Initial Catalog=charge_sys;User ID=sa;PWD=123456;"; SqlConnection sqlConnection = new SqlConnection(sqlConnectionString); SqlCommand sqlCommand = new SqlCommand("select from testB", sqlConnection); SqlDataAdapter sqlAdap = new SqlDataAdapter(sqlCommand); SqlCommandBuilder sqlBuilder = new SqlCommandBuilder(sqlAdap);//必须有 sqlConnectionOpen(); sqlAdapFill(table); //testB表中必须存在主键,否则无法更新 sqlAdapUpdate(table); sqlConnectionClose(); MessageBoxShow("aa"); }
#endregion
连接数据库显示数据#region 连接数据库显示数据
private void form1_load(object sender, eventargs e)
{
sqlconnection conn = new sqlconnection("server=127001;database=pubs;uid=sa");
sqlcommand scd = new sqlcommand("select from tables", conn);
sdaselectcommand = scd;
sdafill(dt);
datagridview1datasource = dt;
}
#endregion
使用update更新数据库#region 使用update更新数据库
private void toolstripbutton1_click(object sender, eventargs e)
{
try
{
sqlcommandbuilder scb = new sqlcommandbuilder(sda);
sdaupdate(dt);
}
catch (systemexception ex)
{
messageboxshow(extostring());
return;
}
messageboxshow("更新成功!");
}
#endregion
建议不要绑数据源,手工添加数据,即使用
添加
int IntRowIndex = DataGridView1RowsAdd();
DataGridView1Rows[IntRowIndex]Cells["DvMID"]Value = DtRows[i]["MID"];
DataGridView1Rows[IntRowIndex]Cells["DvCode"]Value = DtRows[i]["Code"];
DataGridView1Rows[IntRowIndex]Cells["DvName"]Value = DtRows[i]["Name"];
删除
DataGridView1RowsRemoveAt(行号)
更新就直接更新指定单元格
等确认审核完成后,在更新数据库
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1EditIndex = eNewEditIndex;
Bind();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int id = intParse(GridView1DataKeys[eRowIndex]ValueToString());
FriendlyLink flink = FriendlyLinkManagerGetFriendlyLink(id);
flinkTitle = ((TextBox)GridView1Rows[eRowIndex]FindControl("txtTitle"))Text;
flinkUrl = ((TextBox)GridView1Rows[eRowIndex]FindControl("txtUrl"))Text;
FriendlyLinkManagerSaveFriendlyLink(flink);
string url = RequestUrlToString();
ResponseRedirect(url);
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1EditIndex = -1;
Bind();
}
不要用刷新,刷新会这样的,解决的办法:
方法1:记录移到下一条RecordsetMoveNext
方法2:在编辑状态移动到另一个单元格
方法3,直接退出程序,自动更新了
以上就是关于C#中datagridview绑定数据库的视图,如何实现更新数据库全部的内容,包括:C#中datagridview绑定数据库的视图,如何实现更新数据库、DataTable修改后,如何更新数据库、datagridview如何更新数据库等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)