如何取出gridview中的某行的某个值?
这是我写的代码:但是在点击“选择”按钮里总是提示:
异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。
源错误:
行 50: protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
行 51: {
行 52: string stag = this.GridView1.SelectedRow.Cells[0].Text.Trim().ToString();
行 53: ViewState["vsta"] = stag;
行 54: //this.TextBox1.Text = ViewState["vsta"].ToString();
---------我的后台代码是:-------------
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Bind();
}
}
/// <summary>
/// 初始绑定gridview
/// </summary>
public void Bind()
{
string sql = "select distinct News_Tage from News_Table where News_Tage<>'{0}' and News_State='{1}'";
sql = string.Format(sql, "NULL", 1);
Comm(sql);
}
/// <summary>
/// 传入sql语句完成gridview的数据绑定
/// </summary>
/// <param name="Sql">传入sql语句完成gridview的数据绑定</param>
public void Comm(string Sql)
{
SqlConnection scon = new SqlConnection(DS.DataServer.ConnString);
//SqlCommand scom = new SqlCommand(Sql, scon);
scon.Open();
SqlDataAdapter da = new SqlDataAdapter(Sql, scon);
DataSet ds = new DataSet();
da.Fill(ds, "News_Table");
this.GridView1.DataSource = ds;
this.GridView1.DataBind();
scon.Close();
}
protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
string stag = this.GridView1.SelectedRow.Cells[0].Text.Trim().ToString();
ViewState["vsta"] = stag;
//this.TextBox1.Text = ViewState["vsta"].ToString();
}
protected void Button1_Click(object sender, EventArgs e)
{
//将要删除的标签更改值为 NULL
string sql = "update News_Table set News_Tage='{0}' where News_Tage='{1}'";
//update News_Table set News_Tage='' where News_Tage=''
sql = string.Format(sql, "NULL", ViewState["vstag"]);
this.TextBox1.Text = sql;
SqlConnection scon = new SqlConnection(DS.DataServer.ConnString);
SqlCommand scom = new SqlCommand(sql, scon);
scon.Open();
scom.ExecuteNonQuery();
scon.Close();
Response.Write("<script>alert('删除成功')</script>");
Bind();//替换标签后重新绑定gridview
}
请各位能够帮帮我。。我真的十分感谢你的帮助。。