62,046
社区成员
发帖
与我相关
我的任务
分享
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow ||
e.Row.RowType == DataControlRowType.Header)
{
e.Row.Cells[这个值怎么根据字段名得到].Visible = false; //
}
}
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;
}
}
}
}
<%@ 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;
}
}
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;
//}
}
}
}
}
<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>
<%@ 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;
}
}
}
}
}