如何解决这个错误:gridview'gridview1'触发了未处理的事件rowediting。 [英] How to solve this error:the gridview 'gridview1' fired event rowediting which wasn't handled.
本文介绍了如何解决这个错误:gridview'gridview1'触发了未处理的事件rowediting。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
namespace EmpGrid
{
public partial class WebForm1 : System.Web.UI.Page
{
static DataTable ds = new DataTable();
private SqlConnection conn = new SqlConnection(@"Data Source=ADMIN-PC\SQLEXPRESS;Initial Catalog=task;User ID=sa;Password=ups");
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
if (!ds.Columns.Contains("id"))
ds.Columns.Add("id");
if (!ds.Columns.Contains("name"))
ds.Columns.Add("name");
if (!ds.Columns.Contains("salary"))
ds.Columns.Add("salary");
// GridView1.DataSource = dt;
// GridView1.DataBind();
gvbind();
}
}
protected void gvbind() {
conn.Open();
SqlCommand cmd = new SqlCommand("Select * from empdet", conn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
conn.Close();
if (ds.Tables[0].Rows.Count > 0) {
GridView1.DataSource = ds;
GridView1.DataBind();
} else {
//ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
GridView1.DataSource = new String[] { };
GridView1.DataBind();
}
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
Label lbldeleteid = (Label)row.FindControl("lblID");
conn.Open();
SqlCommand cmd = new SqlCommand("delete FROM empdet where id='" + Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString()) + "'", conn);
cmd.ExecuteNonQuery();
conn.Close();
gvbind();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
gvbind();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value);
GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
Label lblID = (Label)row.FindControl("lblID");
TextBox textName = (TextBox)row.Cells[0].Controls[0];
TextBox textadd = (TextBox)row.Cells[1].Controls[0];
// TextBox textc = (TextBox)row.Cells[2].Controls[0];
GridView1.EditIndex = -1;
GridView1.DataBind();
conn.Open();
SqlCommand cmd = new SqlCommand("update empdet set name='" + textName.Text + "',salary='" + textadd.Text + "'where id='" + id + "'", conn);
cmd.ExecuteNonQuery();
conn.Close();
gvbind();
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
gvbind();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
gvbind();
}
protected void Add_Click(object sender, EventArgs e)
{
GridView1.Visible = true;
//createnewrow();
ds.Rows.Add(TextBox1.Text, TextBox2.Text, TextBox3.Text);
TextBox1.Text = "";
TextBox2.Text = "";
TextBox3.Text = "";
GridView1.DataSource = ds;
GridView1.DataBind();
GridView1.DataSource = null;
}
protected void Save_Click(object sender, EventArgs e)
{
// foreach (DataGridViewRow row in GridView1.Rows)
for (int i = 0; i < GridView1.Rows.Count; i++)
{
string constring = @"Data Source=ADMIN-PC\SQLEXPRESS;Initial Catalog=task;User ID=sa;Password=ups";
using (SqlConnection con = new SqlConnection(constring))
{
using (SqlCommand cmd = new SqlCommand("INSERT INTO empdet VALUES( @id,NEWID(),@name, @salary,getdate())", con))
{
cmd.Parameters.AddWithValue("@id", GridView1.Rows[i].Cells[0].Text);
cmd.Parameters.AddWithValue("@name", GridView1.Rows[i].Cells[1].Text);
cmd.Parameters.AddWithValue("@salary", GridView1.Rows[i].Cells[2].Text);
//cmd.Parameters.AddWithValue("@Name", row.Cells["Name"].Value);
//cmd.Parameters.AddWithValue("@Address", row.Cells["Address"].Value);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
}
}
}
我尝试了什么:
i尝试编辑行并更新,删除。但在编辑和删除遇到的错误时遇到错误。请帮我解决问题
What I have tried:
i try to edit the row and update,delete. but am getting error while editing and deleting errors encountered. please help me to solve the issue
推荐答案
TextBox textName = (TextBox)row.Cells[0].Controls[0];
TextBox textadd = (TextBox)row.Cells[1].Controls[0];//change index
//after code updated
TextBox textName = (TextBox)row.Cells[1].Controls[0];
TextBox textadd = (TextBox)row.Cells[2].Controls[0];
这篇关于如何解决这个错误:gridview'gridview1'触发了未处理的事件rowediting。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文