如何取出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
}
请各位能够帮帮我。。我真的十分感谢你的帮助。。
异常详细信息: 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
}
请各位能够帮帮我。。我真的十分感谢你的帮助。。
...全文
相关推荐
asp.net如何得到GRIDVIEW中某行某列值的方法
在ASP.NET中,GridView控件是一种常用的数据展示组件,它允许开发者以表格的形式显示数据源中的信息。
获取GridView中Checkbox的值
在“获取GridView中Checkbox的值”这个场景下,我们通常关注的是如何在用户交互后,如点击按钮时,获取到GridView中被选中的Checkbox对应的值。以下将详细解释如何实现这一功能。
GridView中获取被点击行中的DropDownList和TextBox中的值
`tr.find("td").eq(3).find("select").val();` 这行代码选取了第四列(因为索引从0开始,所以第四个td是索引3)中的DropDownList,并返回用户当前选择的值
winform中获取gridview选定行标识列的值
### GridView 控件介绍GridView 控件是WinForms中的一个重要组成部分,用于显示数据表格。它可以展示来自不同数据源的数据,例如数据库表、数据视图等。
怎么获得gridview 字段值
### 后台代码获取GridView中的值接下来我们将探讨如何在后台代码中获取GridView中的值。


