图3.2 车间参数设置页面 3.2.2 编辑功能 paper51.com 为此DataGrid增加编辑、删除功能列,并将删除列转换为模板列,在页脚中添加“新建”链接按钮,指向本页面,用于实现新建客户资料记录功能。 copyright paper51.com 为DataGrid配置grid_DeleteCommand, grid_UpdateCommand, DataGrid1_EditCommand,grid_CancelCommand,分别实现删除记录,更新记录,转到编辑模式,取消更新等功能。本系统中的编辑功能采用即地编辑模式。新建界面如下图3.3所示: copyright paper51.com 内容来自论文无忧网 www.paper51.com 图3.3 车间参数设置的编辑页面 copyright paper51.com
删除操作的处理过程,只要点击【删除】按钮,就将调用grid_DeleteCommnad,删除所选择的行,更新数据库并刷新页面。 paper51.com 编辑操作的处理过程如下:点击【编辑】按钮,调用DataGrid1_EditCommand将选中行转换到编辑模式如图3.3,在编辑框中输入数据后点击【更新】,将调用grid_UpdateCommand,将输入数据更新到数据库,并刷新页面显示。若点击【取消】按钮,将调用grid_CancelCommand取消更新,回到原页面。 内容来自论文无忧网 www.paper51.com
下面详细介绍grid_DeleteCommnad和grid_UpdateCommand,其原理都是使用SqlCommand对数据库进行操作。而SqlCommand的参数由DataGrid传递。源代码如下(注意DataGrid的DataKeyField应设置为CLIENT表的主键CLIENTID,以用于增删改查): copyright paper51.com private void grid_DeleteCommand(object source,System.Web.UI.WebControls.DataGridCommandEventArgs e) 内容来自www.paper51.com { paper51.com StringstrCmd = "DELETE FROM parameter WHERE setupid=@ID"; http://www.paper51.com SqlCommandcmd = new SqlCommand(strCmd, this.sqlConnection1); http://www.paper51.com SqlParameterp1 = new SqlParameter("@ID",SqlDbType.Int); copyright paper51.com
p1.Direction= ParameterDirection.Input; paper51.com p1.Value= DataGrid1.DataKeys[e.Item.ItemIndex]; paper51.com cmd.Parameters.Add(p1); 内容来自www.paper51.com
copyright paper51.com
copyright paper51.com this.sqlConnection1.Open(); paper51.com
cmd.ExecuteNonQuery(); 内容来自论文无忧网 www.paper51.com this.sqlConnection1.Close(); 内容来自论文无忧网 www.paper51.com
copyright paper51.com
LoadData(); copyright paper51.com
UpdateView(); 内容来自论文无忧网 www.paper51.com
} http://www.paper51.com grid_UpdateCommand的源代码如下: http://www.paper51.com private void grid_UpdateCommand(objectsource, System.Web.UI.WebControls.DataGridCommandEventArgs e) 内容来自www.paper51.com { paper51.com 内容来自论文无忧网 www.paper51.com TextBoxtxtworkshopid = (TextBox) e.Item.Cells[1].Controls[0]; paper51.com
TextBoxtxtcargoid = (TextBox) e.Item.Cells[2].Controls[0]; http://www.paper51.com TextBoxtxtoutperday = (TextBox) e.Item.Cells[3].Controls[0]; copyright paper51.com paper51.com // MUST decide whether to UPDATE or to INSERT. The decisionis made based on paper51.com // the fact that the DataSet has added rows copyright paper51.com
DataSetds = (DataSet) Session["parameter"]; 内容来自www.paper51.com DataTabledt = ds.Tables["parameter"]; http://www.paper51.com DataRowdrLast = dt.Rows[dt.Rows.Count-1]; copyright paper51.com if (drLast.RowState == DataRowState.Added) paper51.com { 内容来自论文无忧网 www.paper51.com // Drop the newly added row from memory 内容来自www.paper51.com
//drLast.RejectChanges(); paper51.com StringstrCmd = "INSERT parameter ( workshopid,cargoid,outperday ) VALUES( @workshopid,@cargoid,@outperday)"; 内容来自www.paper51.com SqlCommandcmd = new SqlCommand(strCmd, this.sqlConnection1); 内容来自www.paper51.com //ID is an identity column http://www.paper51.com
SqlParameterp1 = new SqlParameter("@workshopid",SqlDbType.Int, 4); copyright paper51.com p1.Direction= ParameterDirection.Input; copyright paper51.com p1.Value= Convert.ToInt32(txtworkshopid.Text); paper51.com
cmd.Parameters.Add(p1); 内容来自www.paper51.com SqlParameterp2 = new SqlParameter("@cargoid",SqlDbType.Int, 4); paper51.com
p2.Direction= ParameterDirection.Input; http://www.paper51.com p2.Value= Convert.ToInt32(txtcargoid.Text); copyright paper51.com cmd.Parameters.Add(p2); 内容来自论文无忧网 www.paper51.com SqlParameterp3 = new SqlParameter("@outperday",SqlDbType.Int,4); http://www.paper51.com p3.Direction= ParameterDirection.Input; http://www.paper51.com p3.Value=Convert.ToInt32(txtoutperday.Text); http://www.paper51.com cmd.Parameters.Add(p3); copyright paper51.com
copyright paper51.com this.sqlConnection1.Open(); 内容来自www.paper51.com cmd.ExecuteNonQuery(); http://www.paper51.com this.sqlConnection1.Close(); 内容来自www.paper51.com
} http://www.paper51.com else copyright paper51.com
{ 内容来自www.paper51.com // Prepare the command text 内容来自论文无忧网 www.paper51.com
StringstrCmd = "UPDATE parameter SET workshopid=@workshopid,cargoid=@cargoid,outperday=@outperday WHERE setupid=@ID"; 内容来自论文无忧网 www.paper51.com SqlCommandcmd = new SqlCommand(strCmd, this.sqlConnection1); http://www.paper51.com SqlParameterp1 = new SqlParameter("@ID",SqlDbType.Int); 内容来自www.paper51.com
p1.Direction= ParameterDirection.Input; http://www.paper51.com p1.Value= DataGrid1.DataKeys[e.Item.ItemIndex]; http://www.paper51.com cmd.Parameters.Add(p1); copyright paper51.com
http://www.paper51.com SqlParameterp2 = new SqlParameter("@workshopid",SqlDbType.Int, 4); copyright paper51.com p2.Direction= ParameterDirection.Input; 内容来自www.paper51.com
p2.Value= Convert.ToInt32(txtworkshopid.Text); 内容来自论文无忧网 www.paper51.com cmd.Parameters.Add(p2); paper51.com SqlParameterp3 = new SqlParameter("@cargoid",SqlDbType.Int, 4); 内容来自www.paper51.com p3.Direction= ParameterDirection.Input; copyright paper51.com p3.Value= Convert.ToInt32(txtcargoid.Text); paper51.com
cmd.Parameters.Add(p3); copyright paper51.com SqlParameterp4 = new SqlParameter("@outperday",SqlDbType.Int,4); 内容来自论文无忧网 www.paper51.com p4.Direction= ParameterDirection.Input; 内容来自论文无忧网 www.paper51.com
p4.Value=Convert.ToInt32(txtoutperday.Text); 内容来自论文无忧网 www.paper51.com cmd.Parameters.Add(p4); copyright paper51.com
内容来自www.paper51.com
内容来自www.paper51.com this.sqlConnection1.Open(); paper51.com cmd.ExecuteNonQuery(); 内容来自论文无忧网 www.paper51.com this.sqlConnection1.Close(); copyright paper51.com
} 内容来自www.paper51.com // Reset the edit mode for the current item copyright paper51.com
DataGrid1.EditItemIndex= -1; paper51.com // Refresh the grid 内容来自论文无忧网 www.paper51.com
LoadData(); http://www.paper51.com
UpdateView(); 内容来自www.paper51.com } http://www.paper51.com } paper51.com
|