发哥的日记

交流NET,C#,三层。。
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

实现DataGrid控件中的编辑更新

Posted on 2005-09-08 01:46  平淡人生  阅读(387)  评论(0)    收藏  举报

名称空间
using System.Data.SqlClient;
using System.Configuration;
页面:

<asp:TemplateColumn HeaderText="City">
                        
<ItemTemplate>
                            
<asp:Label runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.City") %>' ID="Label3" NAME="Label3">
                            </asp:Label>
                        
</ItemTemplate>
                        
<EditItemTemplate>
                            
<asp:TextBox runat="server" ID="txtCity" Text='<%# DataBinder.Eval(Container, "DataItem.City") %>'>
                            </asp:TextBox>
                        
</EditItemTemplate>
                    
</asp:TemplateColumn>

cs文件核心代码
    private void DataGridDataBind()
        
{
            
//定义数据连接对象,其中数据库连接字符串是在Web.Config文件中定义的
            SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionSqlServer"].ToString());
            
//创建数据适配器对象
            SqlDataAdapter da = new SqlDataAdapter("select top 5 EmployeeID,LastName,FirstName,City from Employees",conn);
            
//创建DataSet对象
            DataSet ds = new DataSet();
            
try
            
{
                
//填充数据集
                da.Fill(ds,"testTable");
                
//进行数据绑定
                dgEdit.DataSource = ds.Tables["testTable"];
                dgEdit.DataBind();
            }

            
catch(Exception error)
            
{
                
//输出异常信息
                Response.Write(error.ToString());
            }
        
        }



        
private void dgEdit_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
        
{
            
//设置DataGrid控件的编辑项的索引为选择的当前项
            dgEdit.EditItemIndex = e.Item.ItemIndex;
            
//数据绑定
            DataGridDataBind();
        }


        
private void dgEdit_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
        
{
            
//取得编辑行的关键字段的值
            int empID = (int)dgEdit.DataKeys[e.Item.ItemIndex];
            
//取得文本框中输入的内容
            TextBox newCity = (TextBox)e.Item.FindControl("txtCity");
        
            
//定义SQL语句
            string sqlCom = "update Employees set City='" + newCity.Text + "' where EmployeeID="+empID.ToString();
            
//定义数据连接对象,其中数据库连接字符串是在Web.Config文件中定义的
            SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionSqlServer"].ToString());
            
//定义命令对象
            SqlCommand cmd = new SqlCommand(sqlCom,conn);
            
//打开数据连接
            conn.Open();
            
try
            
{
                
//执行SQL命令
                cmd.ExecuteNonQuery();
                
//取消编辑
                dgEdit.EditItemIndex = -1;
                DataGridDataBind();
            }

            
catch(Exception err)
            
{
                
//输出异常信息
                Response.Write(err.ToString());
            }

            
finally
            
{
                
//关闭连接对象
                conn.Close();
            }

        }