想获得GridView里某行里的值,怎么获得

willma123 2012-03-10 11:31:44
姓名 单位 职务 类别 领域 学科 联系电话 手机 邮箱 备注
张三 北京 经理 1 1 1 110 123 111111 qqqqqqqqqq
a 北京 a 1 1 1 a a a a
a1 北京 a1 2 2 2 a1 a1 a1 a1
a1 北京 a1 2 2 2 a1 a1 a1 a1
a1 北京 a1 2 2 2 a1 a1 a1 a1
1233 北京 1231 1 1 1 3123123 123123 123123 123123
我想获得,类别和领域和学科的值,然后根据这个值去查另一张表对应的数据,应该怎么获得
for (int i = 0; i < GGridView.Rows.Count; i++)
{
Response.Write(GGridView.Rows[i].Cells[3].ToString());
Response.Write(GGridView.Rows[i].Cells[4].ToString());
Response.Write(GGridView.Rows[i].Cells[5].ToString());

}
输出的结果是System.Web.UI.WebControls.DataControlFieldCellSystem.Web.UI.WebControls.DataControlFieldCellSystem.Web.UI.WebControls.DataControlFieldCellSystem.Web.UI.WebControls.这堆东西,请问哪里写错了

...全文
224 25 打赏 收藏 转发到动态 举报
写回复
用AI写文章
25 条回复
切换为时间正序
请发表友善的回复…
发表回复
willma123 2012-03-10
  • 打赏
  • 举报
回复
表之间现在没有加上主键的关系[Quote=引用 7 楼 ziyouli 的回复:]
你为啥查询的时候不直接用表间关联把他显示出来,还要进行二次查询?
[/Quote]
ziyouli 2012-03-10
  • 打赏
  • 举报
回复
你为啥查询的时候不直接用表间关联把他显示出来,还要进行二次查询?
willma123 2012-03-10
  • 打赏
  • 举报
回复
类别 领域 学科这3项分别在别的别里,如果我想根据GridView里这3项显示出来的ID查出来,这3张表里对应的名字应该怎么在后台写
ziyouli 2012-03-10
  • 打赏
  • 举报
回复
你把这段代码写哪了
willma123 2012-03-10
  • 打赏
  • 举报
回复
for (int i = 0; i < GGridView.Rows.Count; i++)
{
Response.Write(GGridView.Rows[i].Cells[3].Text.ToString());
Response.Write(GGridView.Rows[i].Cells[4].Text.ToString());
}


改成这个之后什么没输出啊
ziyouli 2012-03-10
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 chinajiyong 的回复:]
Gridview.rows[0].cells[0].Text.tostring
[/Quote]

+1

GGridView.Rows[i].Cells[3]仅仅只获取到这个单元格而已,要获取内容用Text属性。
EnForGrass 2012-03-10
  • 打赏
  • 举报
回复
Gridview.rows[0].cells[0].Text.tostring
willma123 2012-03-10
  • 打赏
  • 举报
回复
求助求助
ziyouli 2012-03-10
  • 打赏
  • 举报
回复
[Quote=引用 20 楼 willma123 的回复:]
<asp:GridView Width="100%" ID="GGridView" runat="server" CellPadding="4"
AutoGenerateColumns="False" BackColor="White" BorderColor="#3366CC"
BorderStyle="None" BorderWidth="1px" onrowdeleting=……
[/Quote]
你把内容用span显示了,后台当然读取不到了
willma123 2012-03-10
  • 打赏
  • 举报
回复
不行这么写还是获取不到数据[Quote=引用 23 楼 taomanman 的回复:]
引用 20 楼 willma123 的回复:

<asp:GridView Width="100%" ID="GGridView" runat="server" CellPadding="4"
AutoGenerateColumns="False" BackColor="White" BorderColor="#3366CC"
BorderStyle="None" BorderWidth………
[/Quote]
暖枫无敌 2012-03-10
  • 打赏
  • 举报
回复
[Quote=引用 20 楼 willma123 的回复:]

<asp:GridView Width="100%" ID="GGridView" runat="server" CellPadding="4"
AutoGenerateColumns="False" BackColor="White" BorderColor="#3366CC"
BorderStyle="None" BorderWidth……
[/Quote]
你使用的span啊,那肯定找不着了,试着将所有的span换成Label看看

<asp:TemplateField HeaderText="类别">
<ItemTemplate>
<span style="float: left"><%#Eval("fenleiid") %></span>
</ItemTemplate>
</asp:TemplateField>
=============》
<asp:TemplateField HeaderText="类别">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%#Eval("fenleiid") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>


willma123 2012-03-10
  • 打赏
  • 举报
回复
点删除后删除数据,但是后台 输出的时候老是得不得数据
protected void GGridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
//int i = Int32.Parse(GGridView.Rows[e.RowIndex].Cells[0].Text);
// string i = ((TextBox)(GGridView.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim();
Response.Write(((Label)(GGridView.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim());

//con.Open();
//string delGGziyuan = "delete from GGziyuan where id=" + i;
//SqlCommand cmd = new SqlCommand(delGGziyuan, con);
//// SqlDataReader sdr = cmd.ExecuteReader();
//cmd.ExecuteNonQuery();
//con.Close();
}
willma123 2012-03-10
  • 打赏
  • 举报
回复
<asp:GridView Width="100%" ID="GGridView" runat="server" CellPadding="4"
AutoGenerateColumns="False" BackColor="White" BorderColor="#3366CC"
BorderStyle="None" BorderWidth="1px" onrowdeleting="GGridView_RowDeleting">
<RowStyle BackColor="White" ForeColor="#003399" />
<Columns>
<asp:TemplateField HeaderText="姓名">
<ItemTemplate>
<span style="float: left"><%#Eval("Gmingzi") %></span>
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="单位">
<ItemTemplate>
<span style="float: left"><%#Eval("Gdanwei") %></span>
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="职务">
<ItemTemplate>
<span style="float: left"><%#Eval("Gzhiwu") %></span>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="类别">
<ItemTemplate>
<span style="float: left"><%#Eval("fenleiid") %></span>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="领域">
<ItemTemplate>
<span style="float: left"><%#Eval("lingyuid") %></span>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="学科">
<ItemTemplate>
<span style="float: left" id="<%#Eval("xuekeid") %>"><%#Eval("xuekeid") %></span>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="联系电话">
<ItemTemplate>
<span style="float: left"><%#Eval("Gdianhua") %></span>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="手机">
<ItemTemplate>
<span style="float: left"><%#Eval("Gshouji") %></span>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="邮箱">
<ItemTemplate>
<span style="float: left"><%#Eval("Gyouxiang") %></span>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="备注">
<ItemTemplate>
<span style="float: left"><%#Eval("Gbeizhu") %></span>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="操作">
<ItemTemplate>
<asp:Button ID="Button2" runat="server" Text="删除" CommandName="Delete" OnClientClick='return confirm("你是否确认要删除该行数据!!!")' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#99CCCC" ForeColor="#003399" />
<PagerStyle BackColor="#99CCCC" ForeColor="#003399" HorizontalAlign="Left" />
<SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
<HeaderStyle BackColor="#003399" Font-Bold="True" ForeColor="#CCCCFF" />
</asp:GridView>
willma123 2012-03-10
  • 打赏
  • 举报
回复
[Quote=引用 18 楼 taomanman 的回复:]
引用 17 楼 willma123 的回复:

引用 16 楼 taomanman 的回复:
试试看

C# code


for (int i = 0; i < GGridView.Rows.Count; i++)
{
Response.Write(((Label)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.……
[/Quote]
暖枫无敌 2012-03-10
  • 打赏
  • 举报
回复
[Quote=引用 17 楼 willma123 的回复:]

引用 16 楼 taomanman 的回复:
试试看

C# code


for (int i = 0; i < GGridView.Rows.Count; i++)
{
Response.Write(((Label)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim());
Resp……
[/Quote]
你前台GridView中是怎么绑定值的???
使用模板列,然后放一个Label来显示值即可。

不行你贴出前台GridView代码,帮你改吧
willma123 2012-03-10
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 taomanman 的回复:]
试试看

C# code


for (int i = 0; i < GGridView.Rows.Count; i++)
{
Response.Write(((Label)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim());
Response.Write(((Label)(G……
[/Quote]
无法将类型为“System.Web.UI.DataBoundLiteralControl”的对象强制转换为类型“System.Web.UI.WebControls.Label”报错少写东西了?
暖枫无敌 2012-03-10
  • 打赏
  • 举报
回复
试试看

for (int i = 0; i < GGridView.Rows.Count; i++)
{
Response.Write(((Label)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim());
Response.Write(((Label)(GridView1.Rows[e.RowIndex].Cells[4].Controls[0])).Text.ToString().Trim());
Response.Write(((Label)(GridView1.Rows[e.RowIndex].Cells[5].Controls[0])).Text.ToString().Trim());
}

ziyouli 2012-03-10
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 willma123 的回复:]
应该怎么写

引用 9 楼 ziyouli 的回复:
引用 8 楼 willma123 的回复:
表之间现在没有加上主键的关系

引用 7 楼 ziyouli 的回复:
你为啥查询的时候不直接用表间关联把他显示出来,还要进行二次查询?


没加上主键一样可以查,只要里面的字段是和另一个表关系的
[/Quote]
和平常的关联查询一样。
willma123 2012-03-10
  • 打赏
  • 举报
回复
protected void GGridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string i =GGridView.Rows[e.RowIndex].Cells[0].Text.ToString();
Response.Write(i);
}

这样写i的内容是空的 是怎么回事
netstudy0105 2012-03-10
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 ziyouli 的回复:]
引用 2 楼 chinajiyong 的回复:
Gridview.rows[0].cells[0].Text.tostring


+1

GGridView.Rows[i].Cells[3]仅仅只获取到这个单元格而已,要获取内容用Text属性。
[/Quote]
正解
加载更多回复(4)

62,268

社区成员

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

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

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

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