在网格视图中,仅删除复选框项目 [英] In grid view delete the check box item only

查看:57
本文介绍了在网格视图中,仅删除复选框项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码无法正常工作,我想通过使用网格视图中模板字段中的复选框来删除选定的行

受保护的void Page_Load(对象发送者,EventArgs e)
{
如果(!Page.IsPostBack)
{
BindData();
}

}
DataTable表;

受保护的void GridView3_RowDeleting(对象发送者,GridViewDeleteEventArgs e)
{
int行= e.RowIndex;
table.Rows [row] .Delete();



//Temp.Rows [row] .Delete();



}
私有void BindData()
{
字符串constr = @数据源= RESTON-PC;初始目录= DCompany;集成安全性= True";
字符串查询= @"SELECT * FROM AircraftInfo";

SqlDataAdapter da =新的SqlDataAdapter(query,constr);
table = new DataTable();


da.Fill(table);
GridView3.DataSource =表;
GridView3.DataBind();
}

但这会给错误对象引用设置未设置为对象的实例提供错误.
说明:执行当前Web请求期间发生未处理的异常.请查看堆栈跟踪,以获取有关错误及其在代码中起源的更多信息.

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

源错误:


第28行:{
第29行:int行= e.RowIndex;
第30行:table.Rows [row] .Delete();
第31行:

this code not properly work i want to delete the selected row by using checkbox in template field in grid view

protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindData();
}

}
DataTable table;

protected void GridView3_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int row = e.RowIndex;
table.Rows[row].Delete();



// Temp.Rows[row].Delete();



}
private void BindData()
{
string constr = @"Data Source=RESTON-PC;Initial Catalog=DCompany;Integrated Security=True";
string query = @"SELECT * FROM AircraftInfo";

SqlDataAdapter da = new SqlDataAdapter(query, constr);
table = new DataTable();


da.Fill(table);
GridView3.DataSource = table;
GridView3.DataBind();
}

But it give error 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 28: {
Line 29: int row = e.RowIndex;
Line 30: table.Rows[row].Delete();
Line 31:

推荐答案


我建议当您单击复选框时,该事件应首先获取该行的ID或记录ID,然后将该ID传递给您的sql语句.
Hi
I suggest when you click the check box, the event should 1st get the ID or record ID of the row, then pass that ID to your sql statement.


浏览链接.这样可以解决您的问题.

在ASP.NET Gridview中插入,更新,删除,将数据源作为SQL服务器,MS Access(mdb/accdb),XML和Framework为2.0/3.0/3.5/4.0(VS 2005/2008/2010) [
Go through the link. This may solve your problem.

Insert, Update, Delete in ASP.NET Gridview, DataSource as SQL Server, MS Access (mdb/accdb), XML and Framework as 2.0 / 3.0 / 3.5 / 4.0 (VS 2005/2008/2010)[^]


这篇关于在网格视图中,仅删除复选框项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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