zoukankan      html  css  js  c++  java
  • 使用DataSet更新GridView中内容

     private void Bind()
        
    {
            SqlConnection con 
    = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString);
            SqlDataAdapter sda 
    = new SqlDataAdapter("select * form name",con);
            DataSet ds 
    = new DataSet();
            sda.Fill(ds,
    "temp");
            con.Close();
            GridView1.DataSource 
    = ds.Tables["temp"].DefaultView;
            GridView1.DataBind();
        }
     
        
    private void fill(int id,string name,int age)
        
    {
            SqlConnection con 
    = new SqlConnection(ConfigurationManager.ConnectionStrings
                [
    "ConnStr"].ConnectionString);
            SqlDataAdapter sda 
    = new SqlDataAdapter("select * from name", con);

            SqlCommandBuilder scbld 
    = new SqlCommandBuilder(sda);
            
    //如果没看上面这句,那DataSet将只能Selete不能Update
            DataSet ds = new DataSet();
            
    try
            
    {
                sda.Fill(ds,
    "temp");

                ds.Tables[
    "temp"].DefaultView.Sort = "id";
                
    //按id排序
                int index = ds.Tables["temp"].DefaultView.Find(id);
                
    //找到我们要的数据所在行的索引
                ds.Tables["temp"].Rows[index]["name"= name;
                ds.Tables[
    "temp"].Rows[index]["age"= age;
                
    //更新DataSet里面的数据必须使用数组的方式。

                
    int rows = sda.Update(ds,"temp");
                Response.Write(
    "成功更新了" + rows + "行数据");
            }

            
    catch(Exception e)
            
    {
                Response.Write(
    "出现错误原因是:" + e.Message);
            }

        }

        
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        
    {
            
    int index = e.RowIndex;
            
    int id = Convert.ToInt32(GridView1.Rows[index].Cells[1].Text);
            
    //上面是说GridView1的行号要动态取,而列号是固定的
            string name = ((TextBox)GridView1.Rows[index].Cells[2].FindControl("TextBox1")).Text;
            
    int age = Convert.ToInt32(((TextBox)GridView1.Rows[index].Cells[3].FindControl("TextBox2")).Text);
            fill(id,name,age);
            GridView1.EditIndex 
    = -1;
             Bind();
        }


  • 相关阅读:
    ArcGIS API for Python
    ArcGIS Engine二次开发:从入门到精通
    OAuth2.0是什么?
    ArcGIS如何使用ArcToolbox新建要素类
    后端如何返回json
    Flask框架和Django框架
    ArcGIS时间——ArcToolbox
    ArcGIS时区设置2-编辑器追踪属性
    使用ABAP代码生成二维码(QR Code)
    如何自行分析SAP WebClient UI开发环境里抛出的错误消息根源
  • 原文地址:https://www.cnblogs.com/yeagen/p/1330926.html
Copyright © 2011-2022 走看看