GRIDVIEW 得不到绑定列的文本值

wang520d 2008-09-19 04:11:43
我给girdview绑定了23列 其中前几列是固定的,其余的列是通过直接取数据绑定的 从数据库中取出来是什么样就绑定什么样
代码如下:
问题:除了这几个固定列能取到页眉的文本值 后面动态绑定的页眉值都为空 可是显示的时候确实显示了页眉 ,请高手赐教!

if (e.Row.RowType == DataControlRowType.Header)
{
for (int iColums = 0; iColums < dt.Rows.Count; iColums++)
{//循环在列表中要隐藏显示的列
strColums = dt.Rows[iColums][0].ToString();
for (int iCells = 0; iCells < e.Row.Cells.Count; iCells++)
{//循环当前绑定列表,从中找到与当前要隐藏的列名同名的表头列,将该cells置为false以达到隐藏显示列的目的
if (e.Row.Cells[iCells].Text == strColums)
{//这个地只有前面几个固定列能取到页眉的文本值 其余都为空 e.Row.Cells[iCells].Visible = false;
lInt.Add(iCells);
break;//找到后跳出内层循环继续循环要隐藏的下一列
}
}
}
}


<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:GridView ID="GridView1" runat="server" OnSorting="GridView1_Sorting" AllowSorting="True"
Width="100%" OnRowCommand="GridView1_RowCommand" OnRowDataBound="GridView1_RowDataBound"
CssClass="border" OnRowCreated="GridView1_RowCreated">
<AlternatingRowStyle HorizontalAlign="Center" CssClass="dbtable_data1" />
<RowStyle HorizontalAlign="Center" CssClass="dbtable_data2" VerticalAlign="Middle"></RowStyle>
<HeaderStyle CssClass="dbtable_title"></HeaderStyle>
<FooterStyle HorizontalAlign="Center" VerticalAlign="Middle"></FooterStyle>
<Columns>
<asp:TemplateField HeaderText="编号">
<HeaderStyle Width="2px" />
<ItemStyle Width="2px" />
<ItemTemplate>
<asp:Label ID="lblNum" runat="server" Text='<%# Container.DataItemIndex + 1 %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="编辑">
<HeaderStyle Width="40px" />
<ItemTemplate>
<asp:LinkButton ID="LinkButtonEdit" runat="server" CommandName="edititem" CommandArgument='<%# Eval(PKField) %>'>
<asp:Image runat="server" AlternateText="编辑" ID="Image1" ImageUrl="~/img/modify.gif">
</asp:Image>
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="删除">
<HeaderStyle Width="40px"></HeaderStyle>
<ItemTemplate>
<asp:LinkButton ID="LinkButtonDelete" runat="server" CommandName="deleteitem" CommandArgument='<%# Eval(PKField) %>'>
<asp:Image ID="Image2" runat="server" ImageUrl="~/img/delete.gif" AlternateText="删除">
</asp:Image>
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="查看">
<HeaderStyle Width="40px"></HeaderStyle>
<ItemTemplate>
<asp:LinkButton ID="LinkButtonLook" runat="server" CommandName="lookitem" CommandArgument='<%# Eval(PKField) %>'>
<asp:Image ID="Image3" runat="server" ImageUrl="~/img/look.gif" AlternateText="查看详细">
</asp:Image>
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="<input type='checkbox' name='allbox' onclick='javascript:selectAll();'"
Visible="false">
<HeaderStyle Width="2%"></HeaderStyle>
</asp:TemplateField>
</Columns>
<SelectedRowStyle BackColor="Cyan" CssClass="dbtable_data3" />
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
...全文
188 28 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
28 条回复
切换为时间正序
请发表友善的回复…
发表回复
takako_mu 2008-09-20
  • 打赏
  • 举报
回复
不要自動生成,自己將那些數據庫的都寫出來。這樣隱藏會很容易
amandag 2008-09-19
  • 打赏
  • 举报
回复
看了这么长还是不能确定楼主打算具体做什么
紫气东来_999 2008-09-19
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 yagebu1983 的回复:]
你单步调试一下,看看里面数据!!
[/Quote]
UP
hy_lihuan 2008-09-19
  • 打赏
  • 举报
回复
你的列第一动态绑定,那就应该是在RowDataBound才会绑定吧,也就是说if (e.Row.Cells[iCells].Text == strColums)你这句话的比较可能达不到你所要的效果。
还有一个问题:if (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Header)
你不显示的时候才会把整个列隐藏吧。
ws_hgo 2008-09-19
  • 打赏
  • 举报
回复
看下你的sql语句
namhyuk 2008-09-19
  • 打赏
  • 举报
回复
干脆记下第几列要隐藏,然后
GridView1.Columns[第n列].Visible = false;
不行吗?
wang520d 2008-09-19
  • 打赏
  • 举报
回复
这是我完整的隐藏代码

protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
try
{
string strColums = "";
if (e.Row.RowType == DataControlRowType.Header)
{
for (int iColums = 0; iColums < dt.Rows.Count; iColums++)
{//循环在列表中要隐藏显示的列
strColums = dt.Rows[iColums][0].ToString();
for (int iCells = 0; iCells < e.Row.Cells.Count; iCells++)
{//循环当前绑定列表,从中找到与当前要隐藏的列名同名的表头列,将该cells置为false以达到隐藏显示列的目的
if (e.Row.Cells[iCells].Text == strColums)
{
e.Row.Cells[iCells].Visible = false;
lInt.Add(iCells);
break;//找到后跳出内层循环继续循环要隐藏的下一列
}
}
}
}
else if (e.Row.RowType == DataControlRowType.DataRow)
{
for (int j = 0; j < lInt.Count; j++)
{
for (int iCells = 0; iCells < e.Row.Cells.Count; iCells++)
{//循环在列表中要隐藏显示的列
if (iCells == lInt[j])
{
e.Row.Cells[iCells].Visible = false;
break;
}
}
}
}
}
catch (Exception err)
{
AjaxAlert("隐藏显示列失败,请联系管理员 表" + TableName + "的字典配置 错误描述:" + ConvertTohtml(err.Message.ToString()));
}
}
namhyuk 2008-09-19
  • 打赏
  • 举报
回复
我今天才知道Row_Created里可以取到数据库表的列名的。呵呵。我还以为直到RowDataBound里才能取得到呢。当然数据库表里的对应数据是肯定得等到RowDataBound时才能取得到。
namhyuk 2008-09-19
  • 打赏
  • 举报
回复
把上面那个代码整个复制到Row_Created,没问题,隐藏是隐藏了。

不过。。。
namhyuk 2008-09-19
  • 打赏
  • 举报
回复
这样又有什么意义呢?

比如有3列表头要显示, 正常是这样:
<tr>
<th scope="col">列1</th>
<th scope="col">列2</th>
<th scope="col">列3</th>
</tr>
结果,你通过设置对应列的Visible=false,那最终出现的是:
<tr>
<th scope="col">列3</th>
</tr>

你是想先把表头搞定,然后再搞数据列?
wang520d 2008-09-19
  • 打赏
  • 举报
回复
[Quote=引用 17 楼 namhyuk 的回复:]
[Quote=引用 14 楼 wang520d 的回复:]
在这个事件里面隐藏的列就得不到值了 ,,,我知道 GRIDVIEW有个新属性 GridView1.DataKeyNames = new string[] { "id" };
可是不想这样用。
----------------
我说的和DataKeyNames没什么关系。

我是问你这样隐藏是能隐藏,其实说白了就是asp.net不会为Visible=false的元素输出任何html代码。
[/Quote]

==========
你说的不会输出HTML代码 是在ROWDATABONND事件里面吧。。在RowCeated事件里面应该是会输出的吧?
namhyuk 2008-09-19
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 wang520d 的回复:]
在这个事件里面隐藏的列就得不到值了 ,,,我知道 GRIDVIEW有个新属性 GridView1.DataKeyNames = new string[] { "id" };
可是不想这样用。
----------------
我说的和DataKeyNames没什么关系。

我是问你这样隐藏是能隐藏,其实说白了就是asp.net不会为Visible=false的元素输出任何html代码。
namhyuk 2008-09-19
  • 打赏
  • 举报
回复
可以。
namhyuk 2008-09-19
  • 打赏
  • 举报
回复
RowCeated时,你觉得表头文字是否可以通过e.Row.Cells[index]这样得到?
wang520d 2008-09-19
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 namhyuk 的回复:]
测试了一个类似的。不过。。。算了,你先试下。

C# code
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
string[] arrayHide = {"ProductID", "ProductName"}; //要隐藏的列名,这里用string[]代替了你的DataTable.
if (e.Row.RowType == DataControlRowType.Header)
{
for (int iColums = 0; iColums < arrayHide.Length; iColums++)

[/Quote]

=========
在这个事件里面隐藏的列就得不到值了 ,,,我知道 GRIDVIEW有个新属性 GridView1.DataKeyNames = new string[] { "id" };
可是不想这样用。
wang520d 2008-09-19
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 shadowjl 的回复:]
试下在bound里
[/Quote]

========
RowCreated 我之所以要这样隐藏 就是因为在bound事件里面 跟DATAGIRD不一样了 放那里面就用不了这个值了
namhyuk 2008-09-19
  • 打赏
  • 举报
回复
测试了一个类似的。不过。。。算了,你先试下。

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
string[] arrayHide = {"ProductID", "ProductName"}; //要隐藏的列名,这里用string[]代替了你的DataTable.
if (e.Row.RowType == DataControlRowType.Header)
{
for (int iColums = 0; iColums < arrayHide.Length; iColums++)
{
string strColums = arrayHide[iColums];
for (int iCells = 0; iCells < e.Row.Cells.Count; iCells++)
{//循环当前绑定列表,从中找到与当前要隐藏的列名同名的表头列,将该cells置为false以达到隐藏显示列的目的
if (e.Row.Cells[iCells].Text == strColums)
{
e.Row.Cells[iCells].Visible = false;
break;
}
}
}
}

}


不过,还是不明白你要达到什么目的。隐藏列?照这个逻辑不象啊!
yagebu1983 2008-09-19
  • 打赏
  • 举报
回复
试试protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)方法
shadowjl 2008-09-19
  • 打赏
  • 举报
回复
试下在bound里
libangchuan 2008-09-19
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 wang520d 的回复:]
GridView1_RowCreated
[/Quote]这里应该得不到也
因为gridview还没创建
加载更多回复(8)

62,243

社区成员

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

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

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

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