gridview ASP.NET中的行更新事件 [英] Row updating event in gridview ASP.NET

查看:116
本文介绍了gridview ASP.NET中的行更新事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的aspx页面中有gridview ....我已经将值从mysql绑定到网格视图...我想更新gridview中的值,我的更新值在mysql中得到更新...我试过很多时间,但我的价值观没有得到更新在mysql ..这是我的aspx和aspx.cs编码pleaselet我知道wat错误我做了.....



i hav gridview in my aspx page ....i already bind values from mysql to grid view ...i want to update the values in gridview and my updated values are get updated in mysql...i tried of many times but my values did't get updated in mysql..this is my aspx and aspx.cs coding pleaselet me know wat mistake i hav done.....

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using MySql.Data.MySqlClient;
using System.IO;


public partial class _Default : System.Web.UI.Page
{
    MySqlConnection con = new MySqlConnection("user id=root; password=admin; database=qms; server=localhost");

   protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            bind();
    }
   public void bind()
   {

       con.Open();
       string q = "select * from exam";
       MySqlDataAdapter da = new MySqlDataAdapter(q, con);
       DataSet ds = new DataSet();
       da.Fill(ds, "emp");
       GridView1.DataSource = ds;
       GridView1.DataBind();
       //con.Close();
   }

    protected void Button1_Click(object sender, EventArgs e)
    {
       bind();
    }

    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        bind();
    }

    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        bind();
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
        int id = Int32.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());
        TextBox tname = (TextBox)row.FindControl("nam");
        TextBox tques = (TextBox)row.FindControl("que");
        MySqlCommand cmd = new MySqlCommand("update exam set name1=@name,ques=@ques where id = @id", con);
        cmd.Parameters.Add("@id", MySqlDbType.Int16).Value = id;
        cmd.Parameters.Add("@name", MySqlDbType.VarChar, 30).Value = tname.Text.Trim();
        cmd.Parameters.Add("@ques", MySqlDbType.VarChar,40).Value = tques.Text.Trim();
        con.Open();
        cmd.ExecuteNonQuery();
        GridView1.EditIndex = -1;
        bind();
    }

推荐答案





调试你的代码?



设一个跟踪点

Hi,

Are debugge your code?

Put a trace point at
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)







还要确认您已在gridview中分配DataKey




Also verify that you have assign DataKey in gridview


您好,



请在下面的行更新活动中使用此代码..



Hello,

Please use this code in row updating event below..

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
        int id = Int32.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());
        string tname = ((TextBox)row.Cells[0].Controls[0]).Text;
        string tques = ((TextBox)row.Cells[1].Controls[0]).Text;
        MySqlCommand cmd = new MySqlCommand("update exam set name1=@name,ques=@ques where id = @id", con);
        cmd.Parameters.Add("@id", MySqlDbType.Int16).Value = id;
        cmd.Parameters.Add("@name", MySqlDbType.VarChar, 30).Value = tname.Text.Trim();
        cmd.Parameters.Add("@ques", MySqlDbType.VarChar, 40).Value = tques.Text.Trim();
        con.Open();
        cmd.ExecuteNonQuery();
        GridView1.EditIndex = -1;
        bind();
}





如果你得到结果然后请投票给答案..



if you get result then pls give vote for answer..


标签lablid = GridView1.Rows [e.RowIndex] .FindControl(lblid)作为标签;

TextBox txtdes = GridView1.Rows [e.RowIndex] .FindControl(txtdescription)as TextBox ;

TextBox txtpric = GridView1.Rows [e.RowIndex] .FindControl(txtprice)as TextBox;

TextBox txtproduct = GridView1.Rows [e.RowIndex]。 FindControl(txtproductname)为TextBox;

TextBox txtquant = GridView1.Rows [e.RowIndex] .FindControl(txtquantity)为TextBox;

TextBox txttype = GridView1 .Rows [e.RowIndex] .FindControl(txttype)为TextBox;
Label lablid = GridView1.Rows[e.RowIndex].FindControl("lblid") as Label;
TextBox txtdes = GridView1.Rows[e.RowIndex].FindControl("txtdescription") as TextBox;
TextBox txtpric = GridView1.Rows[e.RowIndex].FindControl("txtprice") as TextBox;
TextBox txtproduct = GridView1.Rows[e.RowIndex].FindControl("txtproductname") as TextBox;
TextBox txtquant = GridView1.Rows[e.RowIndex].FindControl("txtquantity") as TextBox;
TextBox txttype = GridView1.Rows[e.RowIndex].FindControl("txttype") as TextBox;


这篇关于gridview ASP.NET中的行更新事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆