再发一贴:还是取gridview“列索引值” 为了把这个问题整明白 放100分

wang520d 2008-05-16 04:10:32
注意是列索引 为什么要获得列索引:因为我要在下面事件中根据列索引去隐藏列 可是我只知道 绑定列的字段名 不知道这个字段的cells索引值
(因为列是自动创建的所以无法去数他的cells是第几。。。)

protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow ||
e.Row.RowType == DataControlRowType.Header)
{
e.Row.Cells[这个值怎么根据字段名得到].Visible = false; //
}
}



由于绑定gridview的列是自动创建的 用下面方法试过了自动创建的列在下面这个事件里面是找不到e.Row.Cells[i].Text的文本值 这个值永远为空

protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
int index = -1;
if (e.Row.RowType == DataControlRowType.Header)
{
for (int i = 0; i < e.Row.Cells.Count; i++)
{
// 不是字段名,是字段对应的列的列名
if (e.Row.Cells[i].Text == "特定列名")
{
e.Row.Cells[i].Visible = false;
index = i;
}
}
}
else if (e.Row.RowType == DataControlRowType.DataRow)
{
for (int i = 0; i < e.Row.Cells.Count; i++)
{
if (i == index)
{
e.Row.Cells[i].Visible = false;
}
}
}
}
...全文
278 35 打赏 收藏 转发到动态 举报
写回复
用AI写文章
35 条回复
切换为时间正序
请发表友善的回复…
发表回复
灰哥 2008-05-17
  • 打赏
  • 举报
回复

在开始编辑列的时候你就应该把不显示的列不加进去啊!
那样还方便些!
Ny-6000 2008-05-17
  • 打赏
  • 举报
回复
学习了,即学习到知识,以学习到提问的技巧了.
钊xsun 2008-05-17
  • 打赏
  • 举报
回复
protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
for (int i = 0; i < e.Row.Cells.Count; i++)
{
string s=((TextBox)DataBinder.Eval(e.DataItem,"这里写你要隐藏的字段名").ToString()).Text;
if(e.row.cells[i].text==s)
{
e.Row.Cells[i].Visible = false;
}
}
}
你按我上面的方法做,应该是可以的.如果不可以,那就是
((TextBox)DataBinder.Eval(e.DataItem,"这里写你要隐藏的字段名").ToString())这句的语法有错,因为我是凭记忆写上去的.
sxmonsy 2008-05-17
  • 打赏
  • 举报
回复
好高的楼呀,我也来盖一层
zhoululu8888 2008-05-17
  • 打赏
  • 举报
回复
学习中
amandag 2008-05-16
  • 打赏
  • 举报
回复
这个帖子早就看过了,其实这类的问题大多如二楼在RowDataBound或者RowCreated事件中处理,但楼主开始对问题的描述不足够清楚, 比如现在又提到的UpdatePanel..
silwol 2008-05-16
  • 打赏
  • 举报
回复
没发现2楼代码有什么问题,正确
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" OnDataBound="GridView1_DataBound" OnRowDataBound="GridView1_RowDataBound" AutoGenerateColumns="true">
</asp:GridView>
</div>
</form>
</body>
</html>

    private int index = -1;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
string strCn = System.Configuration.ConfigurationManager.ConnectionStrings["myConnection"].ConnectionString;
string strSQL = @"SELECT * FROM [UserInfo]";
SqlDataAdapter sda = new SqlDataAdapter(strSQL, strCn);
DataTable dt = new DataTable();
sda.Fill(dt);
GridView1.DataSource = dt.DefaultView;
GridView1.DataBind();
}
}

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
//int index = -1;
if (e.Row.RowType == DataControlRowType.Header)
{
index = -1;
for (int i = 0; i < e.Row.Cells.Count; i++)
{
// 不是字段名,是字段对应的列的列名
if (e.Row.Cells[i].Text == "Sex")
{
e.Row.Cells[i].Visible = false;
index = i;
}
}
}
else if (e.Row.RowType == DataControlRowType.DataRow && index != -1)
{
e.Row.Cells[index].Visible = false;
}
}

这样确实可以隐藏“Sex”列,已验证
阿非 2008-05-16
  • 打赏
  • 举报
回复
汉化也一样的

可能你这次是

select * from [tableName] 这样全查

(ps: 不要写* 要写想查的列名,* 是不好的习惯,我偷个懒。)

之后需要隐藏几列,那你只要把想显示的列查询出来就可以了

select [columnName],[columnName],... from [tableName]


wang520d 2008-05-16
  • 打赏
  • 举报
回复
[Quote=引用 24 楼 Sandy945 的回复:]
实际上 你只要改变一下sql 语句 就能实现想要的效果~
没必要这样烦琐的
[/Quote]

我单独试了你的例子 确实是可以隐藏的 可我的页面上这样用却不行。。
e.Row.Cells[i].Text 从第4列开始就为空了。。我郁闷。。。
不知道是不是跟用了 updatepanel有关系。。。。或者是我哪里弄错了。。我再查一下。。
wang520d 2008-05-16
  • 打赏
  • 举报
回复
[Quote=引用 24 楼 Sandy945 的回复:]
实际上 你只要改变一下sql 语句 就能实现想要的效果~
没必要这样烦琐的
[/Quote]

我的数据源已经通过字典表 把表头汉化了。。。
阿非 2008-05-16
  • 打赏
  • 举报
回复
实际上 你只要改变一下sql 语句 就能实现想要的效果~
没必要这样烦琐的
wang520d 2008-05-16
  • 打赏
  • 举报
回复
希望你能试试“用上面这个gridview(18楼的)去绑定pubs数据库里面的titles表 然后去隐藏title列 你就会发现我说的问题。。”
用 e.Row.Cells[i].Text == "title"
阿非 2008-05-16
  • 打赏
  • 举报
回复

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

using System.Collections.Generic;

public partial class testGridView : System.Web.UI.Page
{
List<int> lInt = new List<int>();
List<string> lStr = new List<string>();

SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=pubs;User ID=sa;pwd=sa");

protected void Page_Load(object sender, EventArgs e)
{


if (!IsPostBack)
{
SqlDataAdapter da = new SqlDataAdapter("select * from titles", con);
DataSet ds = new DataSet();
da.Fill(ds, "titles");
//设置要隐藏列的列名
//lStr.Add("title");
//lStr.Add("title_id");
lStr.Add("price");
//columnStr = "Name,title_id";
//createColumn(gv);
//gv.DataSource = getDataTable();
gv.DataSource = ds.Tables["titles"].DefaultView;
gv.DataBind();
}
}
protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
{

if (e.Row.RowType == DataControlRowType.Header)
{
for (int i = 0; i < e.Row.Cells.Count; i++)
{
for (int j = 0; j < lStr.Count; j++)
{
if (lStr[j] == e.Row.Cells[i].Text)
{
e.Row.Cells[i].Visible = false;
lInt.Add(i);
}
}
//// 不是字段名,是字段对应的列的列名
//if (columnStr.Contains(e.Row.Cells[i].Text))// == "单位")
//{
// e.Row.Cells[i].Visible = false;
// index += "," + i;
//}
}
}
else if (e.Row.RowType == DataControlRowType.DataRow)
{
for (int i = 0; i < e.Row.Cells.Count; i++)
{
for (int j = 0; j < lInt.Count; j++)
{
if (lInt[j] == i)
{
e.Row.Cells[i].Visible = false;
}
}

//if (index.Contains(i.ToString()))
//{
// e.Row.Cells[i].Visible = false;
//}
}
}

}
}

wang520d 2008-05-16
  • 打赏
  • 举报
回复
[Quote=引用 19 楼 Sandy945 的回复:]
其实 我在7楼就已经答复你了~


不过上面这个DEMO 需要注意一个地方 就是当GridView的列超过10列时
需要换一个算法,来判断要隐藏那几列

因为现在是用字符串的方法来判断
[/Quote]

我想请问下 如果我直接从数据库取出数据来绑定 gridview 这种情况gridview的头名是不是就相当于数据库表的字段名 这没错吧。。
你上面的列子是隐藏的固定的列 而不是去隐藏的自动创建的列
wang520d 2008-05-16
  • 打赏
  • 举报
回复

用上面这个gridview去绑定pubs数据库里面的titles表的title列 你就会发现我说的问题。。

>>

用上面这个gridview去绑定pubs数据库里面的titles表 去隐藏的title列 你就会发现我说的问题。。
阿非 2008-05-16
  • 打赏
  • 举报
回复
其实 我在7楼就已经答复你了~


不过上面这个DEMO 需要注意一个地方 就是当GridView的列超过10列时
需要换一个算法,来判断要隐藏那几列

因为现在是用字符串的方法来判断
wang520d 2008-05-16
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 Sandy945 的回复:]
你运行我写的DEMO了么

我也是动态创建的列。问题的关键是 动态创建的列,你需要给它指定列名

不指定的话,它当然是空了。

指定列名就两个办法 一是指定GridView中的列的列名

二是指定绑定数据源的列名

根据指定的列名在去判断 做操作
[/Quote]

<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>
</Columns>
<SelectedRowStyle BackColor="Cyan" CssClass="dbtable_data3" />
</asp:GridView>


用上面这个gridview去绑定pubs数据库里面的titles表的title列 你就会发现我说的问题。。
阿非 2008-05-16
  • 打赏
  • 举报
回复

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="testGridView.aspx.cs" Inherits="testGridView" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="gv" runat="server" AutoGenerateColumns="true" OnRowDataBound="gv_RowDataBound">
</asp:GridView>

</div>
</form>
</body>
</html>



using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class testGridView : System.Web.UI.Page
{
string index = "";
string columnStr = "";

protected DataTable getDataTable()
{

System.Data.DataTable dt = new System.Data.DataTable();
System.Data.DataRow dr;
dt.Columns.Add(new System.Data.DataColumn("Name", typeof(System.String)));
dt.Columns.Add(new System.Data.DataColumn("Unit", typeof(System.String)));
dt.Columns.Add(new System.Data.DataColumn("Standard", typeof(System.String)));
dt.Columns.Add(new System.Data.DataColumn("Quantity", typeof(System.Int32)));
dr = dt.NewRow();
dr[0] = "黑色圆珠笔";
dr[1] = "枝";
dr[2] = "2*2";
dr[3] = 10;
dt.Rows.Add(dr);
dr = dt.NewRow();
dr[0] = "红色圆珠笔";
dr[1] = "枝";
dr[2] = "2*2";
dr[3] = 15;
dt.Rows.Add(dr);
dr = dt.NewRow();
dr[0] = "黄色圆珠笔";
dr[1] = "枝";
dr[2] = "2*2";
dr[3] = 20;
dt.Rows.Add(dr);
dr = dt.NewRow();
dr[0] = "蓝色圆珠笔";
dr[1] = "枝";
dr[2] = "2*2";
dr[3] = 18;
dt.Rows.Add(dr);
dr = dt.NewRow();
dr[0] = "辰光签字笔";
dr[1] = "枝";
dr[2] = "2*2";
dr[3] = 15;
dt.Rows.Add(dr);
return dt;
}

private void createColumn(GridView gv)
{
BoundField bf = new BoundField();
bf.HeaderText = "名称";
bf.DataField = "Name";
gv.Columns.Add(bf);
bf = new BoundField();
bf.HeaderText = "单位";
bf.DataField = "Unit";
gv.Columns.Add(bf);
bf = new BoundField();
bf.HeaderText = "数量";
bf.DataField = "Quantity";
gv.Columns.Add(bf);
}

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//设置要隐藏列的列名
columnStr = "Name,名称";
createColumn(gv);
gv.DataSource = getDataTable();
gv.DataBind();
}
}
protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
{

if (e.Row.RowType == DataControlRowType.Header)
{
for (int i = 0; i < e.Row.Cells.Count; i++)
{
// 不是字段名,是字段对应的列的列名
if (columnStr.Contains(e.Row.Cells[i].Text))// == "单位")
{
e.Row.Cells[i].Visible = false;
index += "," + i;
}
}
}
else if (e.Row.RowType == DataControlRowType.DataRow)
{
for (int i = 0; i < e.Row.Cells.Count; i++)
{
if (index.Contains(i.ToString()))
{
e.Row.Cells[i].Visible = false;
}
}
}

}
}


wang520d 2008-05-16
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 Sandy945 的回复:]
你运行我写的DEMO了么

我也是动态创建的列。问题的关键是 动态创建的列,你需要给它指定列名

不指定的话,它当然是空了。

指定列名就两个办法 一是指定GridView中的列的列名

二是指定绑定数据源的列名

根据指定的列名在去判断 做操作
[/Quote]

==========关键是这个属性。。。AutoGenerateColumns="False" 你把这个改为true你就知道我说的是什么意思啦 数据源取出来后直接自动创建列。。
阿非 2008-05-16
  • 打赏
  • 举报
回复
你运行我写的DEMO了么

我也是动态创建的列。问题的关键是 动态创建的列,你需要给它指定列名

不指定的话,它当然是空了。

指定列名就两个办法 一是指定GridView中的列的列名

二是指定绑定数据源的列名

根据指定的列名在去判断 做操作
加载更多回复(14)

62,046

社区成员

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

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

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

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