如何取出gridview中的某行的某个值?

mymamxfx 2008-07-16 11:42:52
这是我写的代码:但是在点击“选择”按钮里总是提示:


异常详细信息: 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
}

请各位能够帮帮我。。我真的十分感谢你的帮助。。
...全文
292 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
sntyy 2008-07-16
  • 打赏
  • 举报
回复

this.GridView1.SelectedIndex = e.NewSelectedIndex;
string stag = this.GridView1.SelectedRow.Cells[0].Text.Trim().ToString();
sntyy 2008-07-16
  • 打赏
  • 举报
回复
你可以在
"string stag = this.GridView1.SelectedRow.Cells[0].Text.Trim().ToString(); "
上面加一行:
this.GridView1.SelectedIndex = e.NewSelectedIndex;
也可以行得通
原因就是SelectedIndex在这里还没有得到你选择的行
sfssmiss 2008-07-16
  • 打赏
  • 举报
回复
1.
if (e.Row.RowType == DataControlRowType.DataRow)
{
//鼠标滑过整行背景变色
e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#B0E2FF',this.style.fontWeight='';");
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor,this.style.fontWeight='';");

HiddenField hfi_id = (HiddenField)e.Row.Cells[3].FindControl("hf_i_id");

if (e.Row.Cells[1].Text != "")
{
if (e.Row.Cells[1].Text == "1")
{
e.Row.Cells[1].Text = "厂家";
}
if (e.Row.Cells[1].Text == "2")
{
e.Row.Cells[1].Text = "供应商";
}
if (e.Row.Cells[1].Text == "3")
{
e.Row.Cells[1].Text = "厂家/供应商";
}
}

}


2.protected void gv_Factory_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
BC_FACTORYEntity FCEntity = new BC_FACTORYEntity();
string id = this.gv_Factory.DataKeys[e.RowIndex].Value.ToString();

}
3.
protected void gv_Factory_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "up")
{
Response.Redirect("FEdit.aspx?fid=" + e.CommandArgument.ToString() + "&id=" + Request.QueryString["id"] + "");
}
}
4.
protected void DealWithData()
{

foreach (GridViewRow grvRow in gv_Factory.Rows)
{
Label lbl_AuditingName = (Label)grvRow.Cells[5].FindControl("lblAuditingName");//审核人

//审核人
if (lbl_AuditingName.Text != null && lbl_AuditingName.Text != "" && lbl_AuditingName.Text != "0")
{
lbl_AuditingName.Text = DF.getUserFullName(lbl_AuditingName.Text);
}
else
{
lbl_AuditingName.Text = "";
}
}

namhyuk 2008-07-16
  • 打赏
  • 举报
回复
莫名其妙,SelectedIndexChaing, 还没Change呢,你怎么能取到GridView1.SelectedRow啊?

Chaing和Changed搞清楚哦。
William_tim 2008-07-16
  • 打赏
  • 举报
回复
不妨试下这个:
string stag = GridView1.SelectedRow.Cells[数据库列的序号].Text.ToString();
Ring850215 2008-07-16
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 brooklyng60 的回复:]
string stag = this.GridView1.Rows[e.NewSelectedIndex].Cells[0].Text.Trim().ToString();
[/Quote]

同意
xjbx 2008-07-16
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 namhyuk 的回复:]
莫名其妙,SelectedIndexChaing, 还没Change呢,你怎么能取到GridView1.SelectedRow啊?

Chaing和Changed搞清楚哦。
[/Quote]

同意
dengchenlu 2008-07-16
  • 打赏
  • 举报
回复
谁说放Changing里没用的啊

[Quote=引用 5 楼 brooklyng60 的回复:]
string stag = this.GridView1.Rows[e.NewSelectedIndex].Cells[0].Text.Trim().ToString();
[/Quote]
JeffChung 2008-07-16
  • 打赏
  • 举报
回复
同意2楼的,要Change以后才能去获取,不要在Changing里做
brooklyng60 2008-07-16
  • 打赏
  • 举报
回复
string stag = this.GridView1.Rows[e.NewSelectedIndex].Cells[0].Text.Trim().ToString();
内容概要:本文围绕不确定环境下的多式联运路径优化问题展开研究,提出并实现了基于AFO算法、遗传算法(GA)和粒子群优化算法(PSO)的三种智能优化方法,并借助Matlab平台完成算法编程与仿真。研究构建了考虑时间、成本、转运风险等多重不确定因素的路径优化模型,系统比较了AFO、GA、PSO三种算法在收敛速度、全局寻优能力和稳定性方面的表现,同时引入Matlab自带的全局优化搜索器作为基准对照,深入分析各算法在复杂物流网络的适用边界与性能差异。研究表明,AFO算法在解决此类组合优化问题时展现出更快的收敛效率和更强的局部规避能力。; 适合人群:具备一定Matlab编程基础与运筹优化知识,从事物流工程、交通运输规划、智能算法开发等相关领域的研究生、科研人员及工程技术人员。; 使用场景及目标:①应用于多式联运、综合货运网络的路径决策支持系统构建;②为不确定性条件下复杂路径规划问题提供智能算法选型依据与技术实现方案;③支持科研人员复现主流优化算法并开展横向性能对比实验,推动算法改进与实际落地。; 阅读建议:建议读者结合提供的Matlab代码逐模块分析算法实现流程,重点理解目标函数设计、约束条件处理及参数敏感性分析部分,可通过调整问题规模与算法参数进行对比实验,进一步拓展至动态路径规划或大规模网络优化等延伸场景。
内容概要:本文研究了基于QLearning自适应强化学习的PID控制器在自主水下航行器(AUV)运动控制的应用,通过Matlab代码实现了控制算法的仿真验证。该方法融合强化学习的在线自适应能力与传统PID控制的稳定性优势,利用QLearning算法动态优化PID控制器的比例、积分、微分参数,以应对水下复杂流体环境、模型不确定性及外部干扰等挑战,从而提升AUV轨迹跟踪的精度、鲁棒性与动态响应性能。文系统阐述了AUV的六自由度非线性动力学建模过程、QLearning算法的状态空间与动作空间设计、奖励函数构造及训练机制,并详细说明了PID参数自整定的闭环控制架构。仿真结果表明,相较于传统固定参数PID控制器,该智能控制策略在多种工况下均展现出更优的控制效果,有效抑制了超调,加快了响应速度,并增强了抗干扰能力。; 适合人群:具备自动控制理论、强化学习基础及Matlab/Simulink仿真能力,从事水下机器人、智能控制、海洋工程、自动化等领域的研究生、科研人员及工程技术人员。; 使用场景及目标:①应用于AUV、UUV等无人水下平台的高精度自主导航与运动控制;②为解决非线性、强耦合、时变系统的控制器参数自适应整定问题提供智能化解决方案;③作为强化学习与经典控制理论深度融合的技术范例,推动智能控制算法在海洋装备的工程化应用。; 阅读建议:建议读者结合提供的Matlab代码深入理解算法实现细节,重点剖析QLearning的状态-动作-奖励机制设计、PID参数更新逻辑及仿真对比实验结果,有条件者可在更复杂的动力学模型或实际硬件平台上进一步验证与优化算法性能。

62,272

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧