如何获得GridView里的值?

z825 2010-10-26 02:37:12
我在C#程序里加了GridView和绑定了数据,在作删除数据库里的选定行动作的时候,取不出选定的ID,我的代码是以下

protected void Delete_Sel_Click(object sender, EventArgs e)
{
foreach (GridViewRow gr in GridView1.Rows)
{

CheckBox chk = (CheckBox)gr.Cells[16].FindControl("itemSel");
if (chk.Checked)
{
TextBox txt = (TextBox)gr.Cells[2].FindControl("ProjectName");
string aa = txt.Text;
//string aa = ((TextBox)gr.Cells[2].FindControl("ProjectName")).Text;
WebMessageBox(aa);
}
}

}
public void WebMessageBox(string values)
{
HttpContext.Current.Response.Write("<script>alert('" + values + "');history.back()</script>");
HttpContext.Current.Response.End();
}

,但老是提示出错“Object reference not set to an instance of an object” ,在string aa = txt.Text;的一句出错。我换了“string aa = ((TextBox)gr.Cells[2].FindControl("ProjectName")).Text; ”请帮我看看我的问题出在哪?如何取得GridView里的ProjectName段的值?
...全文
156 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
z825 2010-10-26
  • 打赏
  • 举报
回复
谢谢各位的帮忙,rwm5366745的方法已解决我的问题,谢谢rwm5366745! Thanks
rwm5366745 2010-10-26
  • 打赏
  • 举报
回复
protected void Delete_Sel_Click(object sender, EventArgs e)
{
foreach (GridViewRow gr in GridView1.Rows)
{

CheckBox chk = (CheckBox)gr.Cells[16].FindControl("itemSel");
if (chk.Checked)
{
string aa =gr.Cells[2].Text ; WebMessageBox(aa);
}
}

  • 打赏
  • 举报
回复

for (int i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox chk = (CheckBox)GridView1.Rows[i].FindControl("itemSel");
if (chk .Checked)
{
string aa = GridView1.Rows[i].Cells[2].Text.ToString();
......
}
}
air123456789 2010-10-26
  • 打赏
  • 举报
回复
哦,原来问的是WEBFORM啊,我不会,哈哈, WINFORM 做习惯了。不好意思哈。
不懂装懂 2010-10-26
  • 打赏
  • 举报
回复
...问的是webform回答的是winform.......
air123456789 2010-10-26
  • 打赏
  • 举报
回复

string Id=dataGridView1.SelectedRows[0].Cells[1]; //假若 你的ID在第二列。 索引从0开始,
air123456789 2010-10-26
  • 打赏
  • 举报
回复
dataGridView1.SelectedRows[0].Cells[1]; //选中的第一行的第二列的值。
z825 2010-10-26
  • 打赏
  • 举报
回复
请问你是怎样在后台读取GridView的值的呢?
air123456789 2010-10-26
  • 打赏
  • 举报
回复
然后 string aa=gr.Cells[2].FindControl("ProjectName")).Text; ?
这个控件我还没用过。

111,129

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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