概述
1、 保存DataGridView所做修改方法
/// <summary>
/// 保存DataGridView所做修改
/// </summary>
/// <param name="sql"></param>
/// <param name="myConn"></param>
/// <param name="dg"></param>
/// <returns></returns>
public bool UpdateDataGridView(string sql, SqlConnection myConn, DataGridView dg)
{
bool result = true;
try
{
DataTable dt = new DataTable();
SqlDataAdapter ada = new SqlDataAdapter(sql, myConn);
SqlCommandBuilder cb = new SqlCommandBuilder(ada);
dt = ((DataTable)dg.DataSource).Copy();
ada.Update(dt);
dg.Update();
dt.AcceptChanges();
}
catch
{
result = false;
}
return result;
}
2、调用
public partial class frmExpInfoMangent : Form
{
eCMy.DataAccessLibrary.Data.Database ecMy;
SqlDataAdapter adpOrders;
SMSMangent mySms = new SMSMangent();
System.Data.Common.DbConnection myDbConn;
string strSql = "select [ExpenseID] as f_id,substring(cast([ExpenseID] as varchar),1,len([ExpenseID])-3) as f_front,expensemc as f_name,userid from eCBMExpense ";
public frmExpInfoMangent()
{
InitializeComponent();
ecMy = eCMy.DataAccessLibrary.Data.DatabaseFactory.CreateDatabase();
myDbConn = ecMy.CreateConnection();
}
private void BindData() //帮定数据库数据到DataGridView显示
{
this.dataGridView1.DataSource = ecMy.SimpleExecuteDataSet(" select [ExpenseID] as f_id,substring(cast([ExpenseID] as varchar),1,len([ExpenseID])-3) as f_front,expensemc as f_name,userid from eCBMExpense ").Tables[0];
}
private void frmExpInfoMangent_Load(object sender, EventArgs e)
{
BindData();
}
}
最后
以上就是体贴芒果为你收集整理的利用DataGridView增加,修改,删除数据的全部内容,希望文章能够帮你解决利用DataGridView增加,修改,删除数据所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复