如何在gridview中编辑行 [英] how to edit row in gridview

查看:173
本文介绍了如何在gridview中编辑行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用此代码更新gridview中的值。

我的设计代码是:

< asp:GridView ID =GridView1runat =server AutoGenerateEditButton =True

DataKeyNames =idonrowcancelingedit =GridView1_RowCancelingEdit1

onrowediting =GridView1_RowEditing1

onrowupdating =GridView1_RowUpdating1 >





和我的源代码:

protected void GridView1_RowUpdating1(object sender,GridViewUpdateEventArgs e)

{

con.Open();

int id = 0;

string status =;

string query =选择id,书中的状态;

SqlCommand cmd = new SqlCommand(query,con);

SqlDataReader dr = cmd .ExecuteReader();

while(dr.Read())

{

id =(int)dr [0];

status = dr [1] .ToString();

}

id = Convert.ToInt32(GridView1.DataKeys [e.RowIndex] .Values [0] .ToString());

status = Convert.ToString(((T. extBox)GridView1.Rows [e.RowIndex] .FindControl(txtEmployeeCode))。Text);

SqlDataAdapter da = new SqlDataAdapter(update book set status ='+ status +'where id ='+ Convert.ToInt32(id)+',con);

DataSet ds = new DataSet();

da.Fill(ds);

GridView1.EditIndex = -1;

display();

con.Close();

回复.Write(Ho Gaya);

}

但此代码显示错误

对象引用未设置为对象的实例。



描述:执行当前Web请求期间发生了未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。



异常详细信息:System.NullReferenceException:对象引用未设置为对象的实例。



来源错误:



第61行:}第62行:id = Convert.ToInt32(GridView1.DataKeys [e.RowIndex] .Values [0] .ToString()) ;第63行:status = Convert.ToString(((TextBox)GridView1.Rows [e.RowIndex] .FindControl(txtEmployeeCode))。Text);第64行:SqlDataAdapter da = new SqlDataAdapter(update book set status ='+ status +'where id ='+ Convert.ToInt32(id)+',con);第65行:DataSet ds = new DataSet();



源文件:d:\Education\Project \Air India \WebSite3 \ Bstatus.aspx .cs行:63

请帮我解决代码。

I m using this code to update a value in gridview .
my design code is :
<asp:GridView ID="GridView1" runat="server" AutoGenerateEditButton="True"
DataKeyNames="id" onrowcancelingedit="GridView1_RowCancelingEdit1"
onrowediting="GridView1_RowEditing1"
onrowupdating="GridView1_RowUpdating1">


and my source code:
protected void GridView1_RowUpdating1(object sender, GridViewUpdateEventArgs e)
{
con.Open();
int id = 0;
string status = "";
string query = "Select id,status from book";
SqlCommand cmd = new SqlCommand(query, con);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
id =(int) dr[0];
status = dr[1].ToString();
}
id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values[0].ToString());
status= Convert.ToString(((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtEmployeeCode")).Text);
SqlDataAdapter da = new SqlDataAdapter("update book set status='" + status + "' where id='" + Convert.ToInt32(id) + "'", con);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.EditIndex = -1;
display();
con.Close();
Response.Write("Ho Gaya");
}
but this code show error that
Object reference not set to an instance of an object.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:

Line 61: } Line 62: id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values[0].ToString()); Line 63: status= Convert.ToString(((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtEmployeeCode")).Text); Line 64: SqlDataAdapter da = new SqlDataAdapter("update book set status='" + status + "' where id='" + Convert.ToInt32(id) + "'", con); Line 65: DataSet ds = new DataSet();

Source File: d:\Education\Project\Air India\WebSite3\Bstatus.aspx.cs Line: 63
please help me to solve code .

推荐答案

检查GridView1.DataKeys [e.RowIndex]的值。值[0]。

它为null,只要使用 .toString()将其转换为字符串,就会抛出错误。



按照他们的方式,你不应该把它转换为字符串,因为你接下来要做的就是把它转换成一个整数。
Check the value of GridView1.DataKeys[e.RowIndex].Values[0].
Its null and as soon as you convert it to a string using .toString() it will throw an error.

By they way, you should not convert it to string anyway, since the next thing you do is convert it to an integer.


这篇关于如何在gridview中编辑行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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