想要从gridview中删除行 [英] want to delete row from gridview

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

问题描述

我正在尝试从gridview中删除行但是有一个execption即将到来并且无法理解这是什么问题。

  if (e.CommandName.Equals( 删除))
{
int index = Convert.ToInt32 (e.CommandArgument);
var id =((Label)GridView1.Rows [index] .Cells [ 0 ] .FindControl( lblid))。Text.Trim(); // 此行上的执行

SqlConnection con = new SqlConnection(connstring);
string qry = 从['删除ISB VAS节点$']其中ID = + id + ;
SqlCommand com = new SqlCommand(qry,con);

// open connection
con.Open();
int numberDeleted = com.ExecuteNonQuery();

if (numberDeleted > 0
{
Label2.Text = 已删除;
binddata();
}
else
{
Label2.Text = 未删除;
}

// 关闭连接
con 。关();
}

解决方案

']其中ID = + id + ;
SqlCommand com = new SqlCommand(qry,con );

// 打开连接
con.Open( );
int numberDeleted = com.ExecuteNonQuery();

if (numberDeleted > 0
{
Label2.Text = < span class =code-string> 已删除;
binddata();
}
else
{
Label2.Text = 未删除;
}

// 关闭连接
con.Close();
}


答案在这里:对象引用未设置为对象错误的实例 [ ^ ]


通过命令源的父级获取GridViewRow,然后尝试查找标签。

试试这个:

 GrieViewRow row =(GridViewRow)((LinkBut​​ton)e.CommandSource)。Parent.Parent; 
var id =((Label)row.FindControl( lblid))。Text.Trim();







- 阿米特

i''m trying to delete row from gridview but there is an execption coming and cant understand what is the problem.

if (e.CommandName.Equals("Delete"))
{
    int index = Convert.ToInt32(e.CommandArgument);
    var id = ((Label)GridView1.Rows[index].Cells[0].FindControl("lblid")).Text.Trim(); // execption on this line

    SqlConnection con = new SqlConnection(connstring);
    string qry = "Delete from ['ISB VAS Nodes$'] where ID = " + id + "";
    SqlCommand com = new SqlCommand(qry, con);

    // open connection
    con.Open();
    int numberDeleted = com.ExecuteNonQuery();

    if (numberDeleted > 0)
    {
        Label2.Text = "Deleted";
        binddata();
    }
    else
    {
        Label2.Text = "Not Deleted";
    }

    // close connection
    con.Close();
}

解决方案

'] where ID = " + id + ""; SqlCommand com = new SqlCommand(qry, con); // open connection con.Open(); int numberDeleted = com.ExecuteNonQuery(); if (numberDeleted > 0) { Label2.Text = "Deleted"; binddata(); } else { Label2.Text = "Not Deleted"; } // close connection con.Close(); }


The answer is here: Object reference not set to an instance of an object error[^]


Get the GridViewRow through the parent of your command source and then try finding the label.
Try this:

GrieViewRow row = (GridViewRow)((LinkButton)e.CommandSource).Parent.Parent;
var id = ((Label)row.FindControl("lblid")).Text.Trim();




--Amit


这篇关于想要从gridview中删除行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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