合并datagrid中内容相同的单元格
http://kb.csdn.net/Articles/200506/4548fc35-a911-48fe-ad4c-df5100716e0e.html
http://www.chinabs.net/aspnet/default.asp?infoid=391
合并datagrid中内容相同的单元格
作者: >>原作者: >>来源:| 来源: 中文c#技术站| 原文地址 | 2004-8-10
有时,我们要把一列中内容相同的单元格合并起来。如下图:
合并后的效果图:
下面就说说怎么实现的:
Sub SpanGrid()
Dim i As Integer
Dim j As Integer
Dim intSpan As Integer,NowSpan As Integer = 0
Dim strTemp As String
For i = 0 To DGrid.Items.Count - 1
intSpan = 1
'得到第一列(颜色)、第一行单元格中的内容。这里得到是“红色Red”。(datagrid里用了模版列)
strTemp = CType(DGrid.Items(i).Cells(0).Controls(1), System.Web.UI.WebControls.Label).Text
’循环判断。判断第一列中,和第一行相同的内容。相同做记号,intspan加一
For j = i + 1 To DGrid.Items.Count - 1
If String.Compare(strTemp, CType(DGrid.Items(j).Cells(1).Controls(1), System.Web.UI.WebControls.Label).Text) = 0 Then
intSpan += 1
'利用datagrid的rowspan属性。(设置控件中单元格跨越的行数为intspan)
DGrid.Items(i).Cells(0).RowSpan = intSpan
’把内容相同单元格隐藏
DGrid.Items(j).Cells(0).Visible = False
Else
Exit For
End If
Next
'i = j - 1 我原来的
NowSpan += intSpan '修改后的可以节省循环次数.
i = NowSpan - 1
Next
End Sub
=======
引用:
Sub bindgrid()
'把数据绑定到datagrid
........
SpanGrid()
End Sub
=================新增内容。合并其他列的内容相同的单元格========
在第一列已经合并的情况下再合并其他列内容相同的单元格
只要在i=j-1前做个类示循环即可。下面是效果图:
DataGrid自动求和、合并单元格、排序
--------------------------------------------------------------------------------
源作者:雪地青松 人气:13404
以前在asp很难实现代码重用,asp.net很好的解决了这个问题,以下是我写的DataGrid,继承DataGrid,加进了升降序/全并单元格/自动求和功能,原理很简单,但很好的实现的代码重用.
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
namespace SunService
{
///
/// Summary description for DataGrid.
///
[DefaultProperty("Text"),
ToolboxData("<{0}:datagrid runat="server">")]
public class DataGrid : System.Web.UI.WebControls.DataGrid
{
private string text;
private SqlDataAdapter adp;
private DataSet ds;
private DataView view;
private string[] arritem;
[Bindable(true),
Category("Appearance"),
DefaultValue("")]
public string Text
{
get
{
return text;
}
set
{
text = value;
}
}
///
/// protect SortDirection 排序方向
///
public string SortDirection
{
get
{
if(ViewState["SortDirection"]==null)
{
return null;
}
else
{
if(ViewState["SortDirection"].ToString()=="")
{
return null;
}
else
{
return ViewState["SortDirection"].ToString();
}
}
}
set
{
ViewState["SortDirection"]=value;
}
}
///
/// protect SortField 排序字段
///
public string SortField
{
get
{
if(ViewState["SortField"]==null)
{
return null;
}
else
{
if(ViewState["SortField"].ToString()=="")
{
return null;
}
else
{
return ViewState["SortField"].ToString();
}
}
}
set
{
ViewState["SortField"]=value;
}
}
///
/// sql 查询字串
///
public string selectCommandText
{
get
{
if(ViewState["selectCommandText"]==null)
{
return null;
}
else
{
if(ViewState["selectCommandText"].ToString()=="")
{
return null;
}
else
{
return ViewState["selectCommandText"].ToString();
}
}
}
set
{
ViewState["selectCommandText"]=value;
}
}
///
/// 连接字串
///
public string selectConnectionString
{
get
{
if(ViewState["selectConnectionString"]==null)
{
return null;
}
else
{
return ViewState["selectConnectionString"].ToString();
}
}
set
{
ViewState["selectConnectionString"]=value;
}
}
public DataTable Bindtable;
public DataGrid()
{
this.Init+=new System.EventHandler(this.DataGrid_Init);
}
private void DataGrid_Init(object sender,EventArgs e)
{
this.Load+= new System.EventHandler(this.DataGrid_Load);
this.SortCommand+=new System.Web.UI.WebControls.DataGridSortCommandEventHandler(this.DataGrid_SortCommand);
this.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid_ItemDataBound);
}
private void DataGrid_Load(object sender,EventArgs e)
{
this.HorizontalAlign=HorizontalAlign.Center;
this.AllowSorting=true;
arritem=new string[256];
ds=new DataSet();
}
///
/// GRID绑定
///
/// 查询字串
/// 连接字串
public void BindGrid(string selectCommandText,string selectConnectionString)
{
this.selectCommandText=selectCommandText;
this.selectConnectionString=selectConnectionString;
BindGrid();
}
///
/// grid绑定
///
/// 查询字串
/// 连接对象
public void BindGrid(string selectCommandText,SqlConnection cn)
{
this.selectCommandText=selectCommandText;
this.selectConnectionString=cn.ConnectionString;
BindGrid();
}
///
/// grid绑定,必须先设置 selectCommmandText 及SelectConnectionString 属性
///
public void BindGrid()
{
if(this.selectCommandText!=null&&this.selectConnectionString!=null)
{
adp=new SqlDataAdapter(this.selectCommandText,this.selectConnectionString);
adp.Fill(ds,"temp");
view=ds.Tables["temp"].DefaultView;
if(this.SortField!=null)
{
view.Sort=this.SortField+" "+this.SortDirection;
int sortfieldindex=0;
for( int i=0;i {
if(ds.Tables["temp"].Columns[i].ColumnName==this.SortField)
{
sortfieldindex=i;
break;
}
}
string SortDirectionImg="▲";
if(this.SortDirection==" DESC")
{
SortDirectionImg="▼";
}
if(this.SortField!=this.DataKeyField)
{
ds.Tables["temp"].Columns[sortfieldindex].ColumnName+=SortDirectionImg;
}
}
Bindtable=ds.Tables["temp"];
DataRow row=Bindtable.NewRow();
row[0]="总计:";
for(int i=1;i {
Type t=Bindtable.Columns[i].DataType;
if(t==typeof(Decimal)||t==typeof(Double)||t==typeof(Int16)||t==typeof(Int32)||t==typeof(Int64)||t==typeof(UInt16)||t==typeof(UInt32)||t==typeof(Int64))
{
row[i]=0;
foreach( DataRow r in Bindtable.Rows)
{
try
{
row[i]=double.Parse(row[i].ToString())+double.Parse(r[i].ToString());
}
catch(Exception et)
{
}
}
}
}
Bindtable.Rows.Add(row);
this.DataSource=view;
this.DataBind();
}
else
{
}
}
private void DataGrid_SortCommand(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
{
if( this.SortDirection==" DESC")
{
this.SortDirection=" ASC";
}
else
{
this.SortDirection=" DESC";
}
this.SortField=e.SortExpression;
this.SortField=this.SortField.Replace("▲","");
this.SortField=this.SortField.Replace("▼","");
BindGrid();
}
private void DataGrid_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
try
{
string txt="";
for(int i=0;i {
// e.Item.Cells[i].Wrap=false;
txt=e.Item.Cells[i].Text.Trim();
if(myClass.IsDouble(txt))
{
e.Item.Cells[i].HorizontalAlign=HorizontalAlign.Right;
}
else
{
if(txt==arritem[i]&&txt!=""&&txt!=null)
{
e.Item.Cells[i].Text="";
}
else
{
arritem[i]=txt;
}
}
}
}
catch(Exception et)
{
}
}
}
}
调用简单:
把组件拖到页面中 ,假设ID为 DataGrid1:
调用:DataGrid1.BindGrid(string selectCommandText,string selectConnectionString)
这样省了建 conntion DataAdapter DataSet再绑定的时间.
大家还可把显示时间显示格式/数字显示格式等加进ItemDataBound事件中,还有自定义分页功能等.
浙公网安备 33010602011771号