1、定义窗体级全局变量
DataSet ds=new DataSet();
SqlDataAdapter sda;
2、定义查询SQL
string strSql = "select tb.spbm ,tb.spsmm,tb.spmc,tb.gg,tb.dw,cast(tb.zxbzs as decimal(10,2)) as zxbzs,tb.wldw,cast(tb.wlbzs as decimal(10,2)) as wlbzs ,cast(ta.wmweight as decimal(10,2)) as wmweight ,cast(ta.wmlength as decimal(10,2)) as wmlength ,cast(ta.wmwidth as decimal(10,2)) as wmwidth ,cast(ta.wmhigh as decimal(10,2)) as wmhigh,cast(ta.SalverPieceNum as decimal(10,2)) as SalverPieceNum ,cast(ta.SalverLayerNum as decimal(10,2)) as SalverLayerNum, cast(ta.SafetyLayerNum as decimal(10,2)) as SafetyLayerNum from eCWMGoodsInfo ta,eCV_WMSpBaseinfo tb where ta.spbm=tb.spbm and 1=1 ";
3、绑定DataGridView,sda
sda = new SqlDataAdapter(strSql , myConn );
sda.Fill(ds);
this.dGVeCSpYWXX.DataSource = ds.Tables[0];
dGVeCSpYWXX.BeginEdit(true); //允许手工编辑
4、保存代码
try
{
SqlCommandBuilder scb = new SqlCommandBuilder(sda);
//由于涉及多张表,所以需要指定修改哪个表的SQL
sda.UpdateCommand = new SqlCommand("update eCWMGoodsInfo set SalverPieceNum=@SalverPieceNum,SalverLayerNum=@SalverLayerNum where spbm=@spbm",myConn );
sda.UpdateCommand.Parameters.Add("@SalverPieceNum", SqlDbType.Decimal, 10, "SalverPieceNum");
sda.UpdateCommand.Parameters.Add("@SalverLayerNum", SqlDbType.Decimal, 10, "SalverLayerNum");
SqlParameter parameter =sda.UpdateCommand.Parameters .Add("@spbm", SqlDbType.VarChar , 10, "spbm");
parameter.SourceVersion = DataRowVersion.Original;
sda.UpdateCommand.UpdatedRowSource = UpdateRowSource.None;
sda.UpdateBatchSize = 10;
this.dGVeCSpYWXX .DataSource = ds.Tables[0];
dGVeCSpYWXX.EndEdit();
sda.Update(ds);
}
catch
{
result = false;
}
本文介绍如何使用C#进行窗体级全局变量定义、SQL查询字符串设置、DataGridView的数据绑定及数据保存的方法。通过具体代码示例展示了如何实现数据表格的动态填充与更新。
3692

被折叠的 条评论
为什么被折叠?



