gridview能这样显示吗 几行数据前面几个字段都相同,最后一个字段内容不同,怎样让前面几个字段只显示一行;比如姓名 身份证号 最后登陆日期 角色 WZGL_ADMIN aaa 1234567 1980-01-01 QNLT_ADMIN QNSS_ADMIN谢谢 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 两种方法,一种是GridView嵌套DataList,这需要以两个表的形式将数据取出,第二种是在RowDataBound事件处理中调整表格格式 你可以在RowDataBound事件后判断上一个item各个cell的值是不是相等如果相等则不显示否则显示 第一种,GridView嵌套DataList,参考这个例子:<%@ Page language="c#" Codebehind="WebForm6466181.aspx.cs" AutoEventWireup="false" Inherits="bsTest2005_8_16.CSDN.WebForm6466181" %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" ><HTML> <HEAD> <title>WebForm6466181</title> <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR"> <meta content="C#" name="CODE_LANGUAGE"> <meta content="JavaScript" name="vs_defaultClientScript"> <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema"> </HEAD> <body MS_POSITIONING="GridLayout"> <form id="Form1" method="post" runat="server"> <FONT face="宋体"> <asp:datalist id="DataList1" style="Z-INDEX: 101; LEFT: 8px; POSITION: absolute; TOP: 8px" runat="server" Width="528px" BorderColor="#999999" BorderStyle="None" BackColor="White" CellPadding="3" GridLines="Vertical" BorderWidth="1px"> <SelectedItemStyle Font-Bold="True" ForeColor="White" BackColor="#008A8C"></SelectedItemStyle> <AlternatingItemStyle BackColor="Gainsboro"></AlternatingItemStyle> <FooterStyle ForeColor="Black" BackColor="#CCCCCC"></FooterStyle> <ItemStyle ForeColor="Black" BackColor="#EEEEEE"></ItemStyle> <ItemTemplate> [<%# DataBinder.Eval(Container.DataItem, "name") %>]<br> <asp:DataList id=DataList2 runat="server" DataSource='<%#GetSubData(DataBinder.Eval(Container.DataItem, "id").ToString())%>' RepeatDirection="Horizontal"> <ItemTemplate> <a href='webform1.aspx?id=<%#DataBinder.Eval(Container.DataItem, "ID1")%>'> <%# DataBinder.Eval(Container.DataItem, "name1") %> </a> </ItemTemplate> </asp:DataList> </ItemTemplate> <HeaderStyle Font-Bold="True" ForeColor="White" BackColor="#000084"></HeaderStyle> </asp:datalist></FONT></form> </body></HTML>using System;using System.Collections;using System.ComponentModel;using System.Data;using System.Drawing;using System.Web;using System.Web.SessionState;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.HtmlControls;namespace bsTest2005_8_16.CSDN{ /// <summary> /// WebForm6466181 的摘要说明。 /// </summary> public class WebForm6466181 : System.Web.UI.Page { protected System.Web.UI.WebControls.DataList DataList1; private DataSet ds; private void Page_Load(object sender, System.EventArgs e) { // 在此处放置用户代码以初始化页面 if(!IsPostBack) { this.createTestData(); this.DataList1.DataSource = ds.Tables[0]; this.DataList1.DataBind(); } } private void createTestData() { //模拟一些测试数据 ds = new DataSet(); DataTable dt1 = new DataTable("Table1"); dt1.Columns.Add("ID"); dt1.Columns.Add("Name"); dt1.Rows.Add(new object[]{"001","车"}); dt1.Rows.Add(new object[]{"002","水"}); ds.Tables.Add(dt1); DataTable dt2 = new DataTable("Table2"); dt2.Columns.Add("ID1"); dt2.Columns.Add("Name1"); dt2.Columns.Add("ID"); dt2.Rows.Add(new object[]{"1001","自行车","001"}); dt2.Rows.Add(new object[]{"1002","电动车","001"}); dt2.Rows.Add(new object[]{"1003","矿泉水","002"}); dt2.Rows.Add(new object[]{"1004","冰冻水","002"}); ds.Tables.Add(dt2); ds.AcceptChanges(); } public DataView GetSubData(string id) { DataView dv = ds.Tables[1].Copy().DefaultView; dv.RowFilter = "[ID] = '"+id+"'"; return dv; } #region Web 窗体设计器生成的代码 override protected void OnInit(EventArgs e) { // // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。 // InitializeComponent(); base.OnInit(e); } /// <summary> /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// </summary> private void InitializeComponent() { this.Load += new System.EventHandler(this.Page_Load); } #endregion }} 第二种,参考 protected System.Web.UI.WebControls.GridViewRow curRow = null; protected int rowSpan = 1; protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowIndex > -1) { if (curRow == null) { curRow = e.Row; } else { if (e.Row.Cells[0].Text == curRow.Cells[0].Text) { rowSpan += 1; curRow.Cells[0].RowSpan = rowSpan; e.Row.Cells[0].Visible = false; } else { rowSpan = 1; curRow = e.Row; } } } } 两种方法,一种是GridView嵌套DataList,这需要以两个表的形式将数据取出,第二种是在RowDataBound事件处理中调整表格格式Eddie005(♂) №.零零伍 (♂) 同意 关于 <form id="form1" runat="server">的认识与迷茫 我怎么获取不到IP了~~呜呜 请教关于SQL查询语句的问题 一个即时聊天的问题 关于Html.DropDownList() 求C#的文件上专代码 请问网易的一个会员通天下的功能如何实现的 做论坛的文章列表,这个列表要根据回复的时间排序,那我第一页就根据回复的时间选择比较容易,那第二页呢? 如何实现datagrid的滚动显示? 怎样才能不做page_load事件? componentart grid,在CALLBACK模式下如何获取当前选中行的某个字段值? 请问"返回上一页"的代码是怎么写的啊?
判断上一个item各个cell的值是不是相等
如果相等则不显示
否则显示
<%@ Page language="c#" Codebehind="WebForm6466181.aspx.cs" AutoEventWireup="false" Inherits="bsTest2005_8_16.CSDN.WebForm6466181" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm6466181</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<FONT face="宋体">
<asp:datalist id="DataList1" style="Z-INDEX: 101; LEFT: 8px; POSITION: absolute; TOP: 8px" runat="server"
Width="528px" BorderColor="#999999" BorderStyle="None" BackColor="White" CellPadding="3" GridLines="Vertical"
BorderWidth="1px">
<SelectedItemStyle Font-Bold="True" ForeColor="White" BackColor="#008A8C"></SelectedItemStyle>
<AlternatingItemStyle BackColor="Gainsboro"></AlternatingItemStyle>
<FooterStyle ForeColor="Black" BackColor="#CCCCCC"></FooterStyle>
<ItemStyle ForeColor="Black" BackColor="#EEEEEE"></ItemStyle>
<ItemTemplate>
[<%# DataBinder.Eval(Container.DataItem, "name") %>]<br>
<asp:DataList id=DataList2 runat="server" DataSource='<%#GetSubData(DataBinder.Eval(Container.DataItem, "id").ToString())%>' RepeatDirection="Horizontal">
<ItemTemplate>
<a href='webform1.aspx?id=<%#DataBinder.Eval(Container.DataItem, "ID1")%>'>
<%# DataBinder.Eval(Container.DataItem, "name1") %>
</a>
</ItemTemplate>
</asp:DataList>
</ItemTemplate>
<HeaderStyle Font-Bold="True" ForeColor="White" BackColor="#000084"></HeaderStyle>
</asp:datalist></FONT></form>
</body>
</HTML>
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;namespace bsTest2005_8_16.CSDN
{
/// <summary>
/// WebForm6466181 的摘要说明。
/// </summary>
public class WebForm6466181 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataList DataList1;
private DataSet ds;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(!IsPostBack)
{
this.createTestData();
this.DataList1.DataSource = ds.Tables[0];
this.DataList1.DataBind();
}
} private void createTestData()
{
//模拟一些测试数据
ds = new DataSet(); DataTable dt1 = new DataTable("Table1");
dt1.Columns.Add("ID");
dt1.Columns.Add("Name");
dt1.Rows.Add(new object[]{"001","车"});
dt1.Rows.Add(new object[]{"002","水"});
ds.Tables.Add(dt1);
DataTable dt2 = new DataTable("Table2");
dt2.Columns.Add("ID1");
dt2.Columns.Add("Name1");
dt2.Columns.Add("ID");
dt2.Rows.Add(new object[]{"1001","自行车","001"});
dt2.Rows.Add(new object[]{"1002","电动车","001"});
dt2.Rows.Add(new object[]{"1003","矿泉水","002"});
dt2.Rows.Add(new object[]{"1004","冰冻水","002"});
ds.Tables.Add(dt2);
ds.AcceptChanges();
} public DataView GetSubData(string id)
{
DataView dv = ds.Tables[1].Copy().DefaultView;
dv.RowFilter = "[ID] = '"+id+"'";
return dv;
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load); }
#endregion
}
}
protected System.Web.UI.WebControls.GridViewRow curRow = null;
protected int rowSpan = 1;
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowIndex > -1)
{
if (curRow == null)
{
curRow = e.Row;
}
else
{
if (e.Row.Cells[0].Text == curRow.Cells[0].Text)
{
rowSpan += 1;
curRow.Cells[0].RowSpan = rowSpan;
e.Row.Cells[0].Visible = false;
}
else
{
rowSpan = 1;
curRow = e.Row;
}
}
}
}