| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 3787 人关注过本帖
标题:求助GridView不能更新,哪位大虾帮我看一下
只看楼主 加入收藏
yph1331
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2008-11-6
收藏
 问题点数:0 回复次数:12 
求助GridView不能更新,哪位大虾帮我看一下
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using weblibrary.data;

public partial class _Default : System.Web.UI.Page
{
    private SqlDataAdapter da;
    private DataSet ds = new DataSet();
    protected void Page_Load(object sender, EventArgs e)
    {
        getInfo();
      
    }

    private void getInfo()
    {
        string str = "Data Source = .;Initial Catalog = library; User Id = sa;Password = 123";
        string sql = "select * from LB_FLXX";
        SqlConnection conn = new SqlConnection(str);
        da = new SqlDataAdapter(sql, conn);
        da.Fill(ds, "t");
        GridView1.DataSource = ds.Tables["t"];
        GridView1.DataBind();
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        getInfo();
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        SqlCommandBuilder cb = new SqlCommandBuilder(da);
        da.Update(ds, "t");
        GridView1.EditIndex = -1;
        getInfo();
    }

    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        getInfo();
    }
}

[[it] 本帖最后由 yph1331 于 2008-11-6 14:04 编辑 [/it]]
搜索更多相关主题的帖子: GridView 
2008-11-06 13:59
sunfishy
Rank: 3Rank: 3
等 级:论坛游侠
威 望:5
帖 子:311
专家分:163
注 册:2007-6-29
收藏
得分:0 
貌似发错地方了.这贴有危机.
2008-11-06 14:42
小仙
Rank: 7Rank: 7Rank: 7
来 自:光之谷
等 级:贵宾
威 望:39
帖 子:1812
专家分:1
注 册:2008-8-8
收藏
得分:0 
protected void Page_Load(object sender, EventArgs e)
    {
        if(!Ispostback)
        getInfo();
      
    }
分析论坛帖子,80%是因为这个原因。


仁者乐膳,智者乐睡。我都爱~
2008-11-06 19:48
yph1331
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2008-11-6
收藏
得分:0 
问题已解决谢谢
2008-11-07 09:39
yph1331
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2008-11-6
收藏
得分:0 
现在用的是:
string sql = "update LB_FLXX set L_FLMC = @L_FLMC where L_FLBH = @L_FLBH";
        SqlCommand cmd = new SqlCommand(sql, conn);
        string name = ((TextBox)(this.GridView1.Rows[e.RowIndex].Cells[1].FindControl("TextBox2"))).Text.Trim();
        int num = Convert.ToInt32(((TextBox)(this.GridView1.Rows[e.RowIndex].Cells[0].FindControl("TextBox1"))).Text.Trim());
        cmd.Parameters.AddWithValue("@L_FLBH", num);
        cmd.Parameters.AddWithValue("@L_FLMC", name);
        try
        {
            conn.Open();
            cmd.ExecuteNonQuery();
        }
        catch
        {
            Response.Write("数据库操作失败!");
        }
        finally
        {
            conn.Close();
        }
        GridView1.EditIndex = -1;
        getInfo();
不过用  SqlCommandBuilder cb = new SqlCommandBuilder(da);
        da.Update(ds, "t");
还是不行,错误提示:未将对象引用设置到对象的实例
2008-11-07 10:05
yanzikeai
Rank: 1
等 级:新手上路
帖 子:10
专家分:0
注 册:2008-11-5
收藏
得分:0 
看不懂你现在用得那个更新!
你知道你第一个那种GridView的更新哪错了么?
protected void myGridView_RowEditing(object sender, GridViewEditEventArgs e)
    {
        myGridView.EditIndex = e.NewEditIndex;
        GridViewBind();
    }

    protected void myGridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        myGridView.EditIndex = -1;
        GridViewBind();
    }
    protected void myGridView_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        myGridView.EditIndex = -1;
        GridViewBind();
    }
这样写对不对啊!
2008-11-09 00:06
yanzikeai
Rank: 1
等 级:新手上路
帖 子:10
专家分:0
注 册:2008-11-5
收藏
得分:0 
protected void myGridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        //这里更新的代码怎么写啊!!!!!!!!!!!!!!!
        myGridView.EditIndex = -1;
        GridViewBind();
    }
2008-11-09 00:40
yanzikeai
Rank: 1
等 级:新手上路
帖 子:10
专家分:0
注 册:2008-11-5
收藏
得分:0 
我找到这个  还是实现不了!
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        sqlcon = new SqlConnection(strCon);
        string sqlstr = "update 表 set 字段1='"
            + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim() + "',字段2='"
            + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim() + "',字段3='"
            + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim() + "' where id='"
            + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
        sqlcom=new SqlCommand(sqlstr,sqlcon);
        sqlcon.Open();
        sqlcom.ExecuteNonQuery();
        sqlcon.Close();
        GridView1.EditIndex = -1;
        bind();
    }
//id='"+ GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";这句话是什么意思!
2008-11-09 00:44
yanzikeai
Rank: 1
等 级:新手上路
帖 子:10
专家分:0
注 册:2008-11-5
收藏
得分:0 
熬到两点了,终于弄明白了!

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class GridViewbianji222 : System.Web.UI.Page
{
    SqlConnection mycon = new SqlConnection();
    System.Data.DataSet myset = new System.Data.DataSet();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GridViewBind();
        }
    }
    private void GridViewBind()
    {
        mycon.ConnectionString = ConfigurationManager.AppSettings["mykey"];
        string str1 = "select * from userbase";
        SqlDataAdapter myad = new SqlDataAdapter(str1,mycon);
        mycon.Open();
        myad.Fill(myset,"table1");
        GridView1.DataSource=myset.Tables["table1"];
        GridView1.DataKeyNames = new string[] { "name" };
        GridView1.DataBind();
        mycon.Close();
    }
   
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        mycon.ConnectionString = ConfigurationManager.AppSettings["mykey"];
        string sqlstr = "update userbase set city='"
            + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim() + "',home='"
            + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim() + "',telephone='"
            + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[4].Controls[0])).Text.ToString().Trim() + "',username='"
            + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[5].Controls[0])).Text.ToString().Trim() + "' where name='"
            + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
        SqlCommand sqlcom = new SqlCommand(sqlstr, mycon);
        mycon.Open();
        sqlcom.ExecuteNonQuery();
        mycon.Close();
        GridView1.EditIndex = -1;
        GridViewBind();
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        GridViewBind();
    }

    protected void myGridView_RowEditing(object sender, GridViewEditEventArgs e)
    {
        
        GridView1.EditIndex = e.NewEditIndex;
        GridViewBind();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        GridViewBind();
    }
2008-11-09 02:01
yanzikeai
Rank: 1
等 级:新手上路
帖 子:10
专家分:0
注 册:2008-11-5
收藏
得分:0 
}
唉 我总是粗心大意啊!
在Page_Load一定要这样写
if (!IsPostBack)
        {
            GridViewBind();
        }
2008-11-09 02:03
快速回复:求助GridView不能更新,哪位大虾帮我看一下
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.033209 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved