大家GridVIEw都用的比較多吧..有沒遇到單元格需要合並的需求..
單元格合並原理其實很簡單,就是逐行判斷要合並的單元格裡的值是否和上一行的相同,要是相同的 話就合並,不同的話就接著判斷
我們可以通過擴展方法為GridVIEw添加單元合並
public static class GridVIEwExtensions
{
/// <summary>
/// GridVIEw行合並
/// </summary>
/// <param name="gridVIEw"></param>
/// <param name="fIEld">合並參數(匿名類型)
/// ColumnIndex:要合並行的索引 (以0開始,必須指定)
/// ID(可選):如果該行為模板行則必須指定
/// PropertyName:根據ID屬性 默認值為Text
/// Colums:(string類型)表示額外的行合並方式和ColumnIndex一樣(多個使用逗 號隔開,如Colums="5,6,7,8")
/// 例:
/// 合並第一行(第一行為模板行),綁定的一個Label名稱為lblName 根據Text 屬性值合並 第6行方式和第一行相同
/// new {ColumnIndex=0,ID="lblName",PropertyName="Text",Columns="5"}
/// </param>
public static GridView RowSpan(this GridView gridView, object fIEld)
{
Dictionary<string, string> rowDictionary = ObjectLoadDictionary(fIEld);
int columnIndex = int.Parse(rowDictionary["ColumnIndex"]);
string columnName = rowDictionary["ColumnName"];
string propertyName = rowDictionary["PropertyName"];
string columns = rowDictionary["Columns"];
for (var i = 0; i < gridVIEw.Rows.Count; i++)
{
int rowSpanCount = 1;
for (int j = i + 1; j < gridVIEw.Rows.Count; j++)
{
//綁定行合並處理
if (string.IsNullOrEmpty(columnName))
{
//比較2行的值是否相同
if (gridView.Rows[i].Cells [columnIndex].Text == gridVIEw.Rows[j].Cells[columnIndex].Text)
{
//合並行的數量+1
rowSpanCount++;
//隱藏相同的行
gridVIEw.Rows[j].Cells [columnIndex].Visible = false;
if (!string.IsNullOrEmpty (columns))
{
columns.Split (',').ToList<string>().ForEach(c => gridVIEw.Rows[j].Cells[int.Parse (c)].Visible=false);
}
}
else
{
break;
}
}
else
{
//模板行的合並處理
if (GetPropertyValue(gridView.Rows [i].Cells[columnIndex].FindControl(columnName), propertyName) == GetPropertyValue (gridVIEw.Rows[j].Cells[columnIndex].FindControl(columnName), propertyName))
{
rowSpanCount++;
//隱藏相同的行
gridVIEw.Rows[j].Cells [columnIndex].Visible = false;
if (!string.IsNullOrEmpty (columns))
{
columns.Split (',').ToList<string>().ForEach(c => gridVIEw.Rows[j].Cells[int.Parse(c)].Visible = false);
}
}
else
{
break;
}
}
}
if (rowSpanCount > 1)
{
//行合並
gridVIEw.Rows[i].Cells[columnIndex].RowSpan = rowSpanCount;
//判斷是否有額外的行需要合並
if (!string.IsNullOrEmpty(columns))
{
//額外的行合並
columns.Split(',').ToList<string> ().ForEach(c => gridVIEw.Rows[i].Cells[int.Parse(c)].RowSpan = rowSpanCount);
}
i = i + rowSpanCount - 1;
}
}
return gridVIEw;
}
private static Dictionary<string, string> ObjectLoadDictionary (object fIElds)
{
Dictionary<string, string> resultDictionary = new Dictionary<string, string>();
PropertyInfo[] property = fields.GetType().GetPropertIEs (BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.GetProperty);
foreach (PropertyInfo tempProperty in property)
{
resultDictionary.Add(tempProperty.Name, tempProperty.GetValue(fIElds, null).ToString());
}
//指定默認值
if (!resultDictionary.Keys.Contains("ColumnIndex"))
{
throw new Exception("未指定要合並行的索引 ColumnIndex 屬性!");
}
if (!resultDictionary.Keys.Contains("ColumnName"))
{
resultDictionary.Add("ColumnName", null);
}
if (!resultDictionary.Keys.Contains("PropertyName"))
{
resultDictionary.Add("PropertyName", "Text");
}
if (!resultDictionary.Keys.Contains("Columns"))
{
resultDictionary.Add("Columns", null);
}
return resultDictionary;
}
/// <summary>
/// 獲取一個對象的一個屬性..
/// </summary>
/// <param name="obj"></param>
/// <param name="PropertyName">屬性名稱</param>
/// <returns>屬性的值, 如果無法獲取則返回 null</returns>
private static object GetPropertyValue(object obj, string PropertyName)
{
PropertyInfo property = obj.GetType().GetProperty (PropertyName);
return property.GetValue(obj,null);
}
}