我给个函数,也许能帮你! ------------------------------------------------------------------------------ '功能:对选定的Grid格子按给定的方式进行合并 '参数:intStartRow开始行,intStartCol开始列,intEndRow结束行,intEndCol结束列, ' flag合并方式 0-按行、1-按列、2-全部 , ' HAlign水平排列方式 0-NotSet、1-Left,2-Center,3-Right,4-Justify, ' VAlign垂直排列方式 0-NotSet、1-Top,2-Middle,3-Bottom ---------------------------------------------------------------------------------- Protected Overridable Sub PageLoad_MergeCell(ByVal intStartRow As Int32, ByVal intStartCol As Int32, ByVal intEndRow As Int32, ByVal intEndCol As Int32, ByVal flag As Int16, ByVal HAlign As Int16, ByVal VAlign As Int16) Dim i, j As Int16 If (Me.DataGridMe.Items.Count > intStartRow And Me.DataGridMe.Columns.Count >= intStartCol) Then If flag = 0 Then For i = intStartRow To intEndRow Me.DataGridMe.Items(i).Cells(intStartCol).ColumnSpan = intEndCol - intStartCol + 1 For j = 1 To (intEndCol - intStartCol) Me.DataGridMe.Items(i).Cells(intStartCol + j).Visible = False Next Me.DataGridMe.Items(i).Cells(intStartCol).HorizontalAlign = HAlign Me.DataGridMe.Items(i).Cells(intStartCol).VerticalAlign = VAlign Next End If If flag = 1 Then For i = intStartCol To intEndCol Me.DataGridMe.Items(intStartRow).Cells(i).RowSpan = intEndRow - intStartRow + 1 For j = 1 To intEndRow - intStartRow Me.DataGridMe.Items(intStartRow + j).Cells(i).Visible = False Next Me.DataGridMe.Items(intStartRow).Cells(i).HorizontalAlign = HAlign Me.DataGridMe.Items(intStartRow).Cells(i).VerticalAlign = VAlign Next End If If flag = 2 Then For i = intStartRow To intEndRow Me.DataGridMe.Items(i).Cells(intStartCol).ColumnSpan = intEndCol - intStartCol + 1 For j = 1 To intEndCol - intStartCol Me.DataGridMe.Items(i).Cells(intStartCol + j).Visible = False Next Next For i = intStartCol To intStartCol Me.DataGridMe.Items(intStartRow).Cells(i).RowSpan = intEndRow - intStartRow + 1 For j = 1 To intEndRow - intStartRow Me.DataGridMe.Items(intStartRow + j).Cells(i).Visible = False Next Next Me.DataGridMe.Items(intStartRow).Cells(i).HorizontalAlign = HAlign Me.DataGridMe.Items(intStartRow).Cells(i).VerticalAlign = VAlign End If End If End Sub
to:76wzc(麦田人) 谢谢,我试试
我刚才试了一下,没什么问题啊,不知道是不是你要的效果,代码如下
private void Page_Load(object sender, System.EventArgs e) { DataGrid1.DataSource = CreateDataSource(); DataGrid1.DataBind(); DataGrid1.Items[0].Cells[0].RowSpan = 2; DataGrid1.Items[1].Cells[0].Visible = false; DataGrid1.Items[0].Cells[2].RowSpan = 2; DataGrid1.Items[1].Cells[2].Visible = false; } private DataSet CreateDataSource() { // Create sample data for the DataGrid control. DataSet ds = new DataSet(); DataTable dt = new DataTable(); DataRow dr;
我這裡也有兩個函數 ,傳上來湊個熱鬧 Public Class SpanGrid '============================================================================= '無條件合併同一列的內容相同行 'intBaseColumn 列號 'DG DataGrid ID '***************** '***************** '* * C * '* * * '* * * '* * * '* A ********* BaseColumnSpan(1) '* A * * '* * C * ================ '* * * '* * * '* * C * '* *************** '* ******* * '* B * C * '* B * * '* * * '* * * '***************** '***************** Function BaseColumnSpan(ByVal intBaseColumn As Integer, ByVal DG As DataGrid) Dim i As Integer Dim j As Integer Dim intColumn As Integer Dim intSpan As Integer Dim strTemp As String Dim strTemp2 As String For i = 0 To DG.Items.Count - 1 intSpan = 1 strTemp = DG.Items(i).Cells(intBaseColumn).Text For j = i + 1 To DG.Items.Count - 1 If String.Compare(strTemp, DG.Items(j).Cells(intBaseColumn).Text) = 0 Then intSpan += 1 DG.Items(i).Cells(intBaseColumn).RowSpan = intSpan DG.Items(j).Cells(intBaseColumn).Visible = False Else Exit For End If Next i = j - 1 Next End Function '============================================================================= '============================================================================= '根據父列的分類情況對子列分類合併 '子列的C 与 C 不合併 '***************** '***************** '* * C * '* * * '* * * '* * * '* A ********* SpanGrid(0,1) '* A * C * '* * C * ================ '* * * '* * * '* * * '* *************** '* *************** '* B * C * '* B * C * '* * * '* * * '***************** '***************** 'DG DataGrid ID 'intBaseColumn 父列列號 'intSpanColumn 子列列號 Function SpanGrid(ByVal intBaseColumn As Integer, ByVal intSpanColumn As Integer, ByVal DG As DataGrid) Dim i As Integer Dim j As Integer Dim intColumn As Integer Dim intSpan As Integer Dim strTemp As String Dim strTemp2 As String Dim k, m As Integer intColumn = intSpanColumn For i = 0 To DG.Items.Count - 1 For j = i + 1 To DG.Items.Count - 1 If DG.Items(j).Cells(intBaseColumn).Visible = True Then For m = i To j - 1 intSpan = 1 strTemp2 = DG.Items(k).Cells(intColumn).Text For k = m + 1 To j - 1 If String.Compare(strTemp2, DG.Items(k).Cells(intColumn).Text) = 0 Then intSpan += 1 DG.Items(m).Cells(intColumn).RowSpan = intSpan DG.Items(k).Cells(intColumn).Visible = False Else Exit For End If Next m = k - 1 Next Exit For End If If j = DG.Items.Count - 1 Then For m = i To j intSpan = 1 strTemp2 = DG.Items(k).Cells(intColumn).Text For k = m + 1 To j If String.Compare(strTemp2, DG.Items(k).Cells(intColumn).Text) = 0 Then intSpan += 1 DG.Items(m).Cells(intColumn).RowSpan = intSpan DG.Items(k).Cells(intColumn).Visible = False Else Exit For End If Next m = k - 1 Next Exit For End If Next i = j - 1 Next End Function '============================================================================= End Class
我没有移走了太多的cell.
to:lbzq(longbow)、yzxdc(yzxdc)
visiable和removeat的效果是一样的,都是造成某一单元格消失,完了之后,附近的右侧的单元格会左移!!
removeat(3)
removeat(2)
removeat(1)
你说的这个方法我也试过了,当要合并行的列是连续的时候可以,但是,如果中间有不需要的合并行的列的时候,问题又出来了!!也就是第一、三列需要,第二列不要!那第二列的项又会出错!
移走一列后,是否占用这一列位置的colspan="1"单位格未设置属性?应该是colspan="2"。此问题我并未深入理解。
谢谢你的提醒,我试试看!
这个问题就是说当你删除或者合并了一个单元格之后,这个位置就会空出来或者变大,那么 就会使同行右边的单元格左移或者右移!
------------------------------------------------------------------------------
'功能:对选定的Grid格子按给定的方式进行合并
'参数:intStartRow开始行,intStartCol开始列,intEndRow结束行,intEndCol结束列,
' flag合并方式 0-按行、1-按列、2-全部 ,
' HAlign水平排列方式 0-NotSet、1-Left,2-Center,3-Right,4-Justify,
' VAlign垂直排列方式 0-NotSet、1-Top,2-Middle,3-Bottom
----------------------------------------------------------------------------------
Protected Overridable Sub PageLoad_MergeCell(ByVal intStartRow As Int32, ByVal intStartCol As Int32, ByVal intEndRow As Int32, ByVal intEndCol As Int32, ByVal flag As Int16, ByVal HAlign As Int16, ByVal VAlign As Int16) Dim i, j As Int16 If (Me.DataGridMe.Items.Count > intStartRow And Me.DataGridMe.Columns.Count >= intStartCol) Then
If flag = 0 Then
For i = intStartRow To intEndRow
Me.DataGridMe.Items(i).Cells(intStartCol).ColumnSpan = intEndCol - intStartCol + 1
For j = 1 To (intEndCol - intStartCol)
Me.DataGridMe.Items(i).Cells(intStartCol + j).Visible = False
Next
Me.DataGridMe.Items(i).Cells(intStartCol).HorizontalAlign = HAlign
Me.DataGridMe.Items(i).Cells(intStartCol).VerticalAlign = VAlign
Next
End If
If flag = 1 Then
For i = intStartCol To intEndCol
Me.DataGridMe.Items(intStartRow).Cells(i).RowSpan = intEndRow - intStartRow + 1
For j = 1 To intEndRow - intStartRow
Me.DataGridMe.Items(intStartRow + j).Cells(i).Visible = False
Next
Me.DataGridMe.Items(intStartRow).Cells(i).HorizontalAlign = HAlign
Me.DataGridMe.Items(intStartRow).Cells(i).VerticalAlign = VAlign
Next
End If
If flag = 2 Then
For i = intStartRow To intEndRow
Me.DataGridMe.Items(i).Cells(intStartCol).ColumnSpan = intEndCol - intStartCol + 1
For j = 1 To intEndCol - intStartCol
Me.DataGridMe.Items(i).Cells(intStartCol + j).Visible = False
Next
Next
For i = intStartCol To intStartCol
Me.DataGridMe.Items(intStartRow).Cells(i).RowSpan = intEndRow - intStartRow + 1
For j = 1 To intEndRow - intStartRow
Me.DataGridMe.Items(intStartRow + j).Cells(i).Visible = False
Next
Next
Me.DataGridMe.Items(intStartRow).Cells(i).HorizontalAlign = HAlign
Me.DataGridMe.Items(intStartRow).Cells(i).VerticalAlign = VAlign
End If
End If
End Sub
谢谢,我试试
private void Page_Load(object sender, System.EventArgs e)
{
DataGrid1.DataSource = CreateDataSource();
DataGrid1.DataBind(); DataGrid1.Items[0].Cells[0].RowSpan = 2;
DataGrid1.Items[1].Cells[0].Visible = false; DataGrid1.Items[0].Cells[2].RowSpan = 2;
DataGrid1.Items[1].Cells[2].Visible = false;
} private DataSet CreateDataSource()
{
// Create sample data for the DataGrid control.
DataSet ds = new DataSet();
DataTable dt = new DataTable();
DataRow dr;
dt.Columns.Add(new DataColumn("ID", typeof(String))); //ID
dt.Columns.Add(new DataColumn("NAME", typeof(String))); //名称
dt.Columns.Add(new DataColumn("CODE", typeof(String))); //编号 for(int i=0;i<5;i++)
{
dr = dt.NewRow();
dr[0] = i;
dr[1] = "Name" + i;
dr[2] = "Code" + i;
dt.Rows.Add(dr);
} ds.Tables.Add(dt); return ds;
}
给分吧
Public Class SpanGrid
'=============================================================================
'無條件合併同一列的內容相同行
'intBaseColumn 列號
'DG DataGrid ID
'***************** '*****************
'* * C * '* * *
'* * * '* * *
'* A ********* BaseColumnSpan(1) '* A * *
'* * C * ================ '* * *
'* * * '* * C *
'* *************** '* ******* *
'* B * C * '* B * *
'* * * '* * *
'***************** '*****************
Function BaseColumnSpan(ByVal intBaseColumn As Integer, ByVal DG As DataGrid)
Dim i As Integer
Dim j As Integer
Dim intColumn As Integer
Dim intSpan As Integer
Dim strTemp As String
Dim strTemp2 As String
For i = 0 To DG.Items.Count - 1
intSpan = 1
strTemp = DG.Items(i).Cells(intBaseColumn).Text
For j = i + 1 To DG.Items.Count - 1
If String.Compare(strTemp, DG.Items(j).Cells(intBaseColumn).Text) = 0 Then
intSpan += 1
DG.Items(i).Cells(intBaseColumn).RowSpan = intSpan
DG.Items(j).Cells(intBaseColumn).Visible = False
Else
Exit For
End If
Next
i = j - 1
Next End Function
'============================================================================= '=============================================================================
'根據父列的分類情況對子列分類合併
'子列的C 与 C 不合併
'***************** '*****************
'* * C * '* * *
'* * * '* * *
'* A ********* SpanGrid(0,1) '* A * C *
'* * C * ================ '* * *
'* * * '* * *
'* *************** '* ***************
'* B * C * '* B * C *
'* * * '* * *
'***************** '*****************
'DG DataGrid ID
'intBaseColumn 父列列號
'intSpanColumn 子列列號
Function SpanGrid(ByVal intBaseColumn As Integer, ByVal intSpanColumn As Integer, ByVal DG As DataGrid)
Dim i As Integer
Dim j As Integer
Dim intColumn As Integer
Dim intSpan As Integer
Dim strTemp As String
Dim strTemp2 As String
Dim k, m As Integer
intColumn = intSpanColumn
For i = 0 To DG.Items.Count - 1
For j = i + 1 To DG.Items.Count - 1 If DG.Items(j).Cells(intBaseColumn).Visible = True Then
For m = i To j - 1
intSpan = 1
strTemp2 = DG.Items(k).Cells(intColumn).Text
For k = m + 1 To j - 1
If String.Compare(strTemp2, DG.Items(k).Cells(intColumn).Text) = 0 Then
intSpan += 1
DG.Items(m).Cells(intColumn).RowSpan = intSpan
DG.Items(k).Cells(intColumn).Visible = False
Else
Exit For
End If
Next
m = k - 1
Next
Exit For
End If If j = DG.Items.Count - 1 Then
For m = i To j
intSpan = 1
strTemp2 = DG.Items(k).Cells(intColumn).Text
For k = m + 1 To j
If String.Compare(strTemp2, DG.Items(k).Cells(intColumn).Text) = 0 Then
intSpan += 1
DG.Items(m).Cells(intColumn).RowSpan = intSpan
DG.Items(k).Cells(intColumn).Visible = False
Else
Exit For
End If
Next
m = k - 1
Next
Exit For
End If
Next
i = j - 1
Next
End Function
'=============================================================================
End Class