Extension Method (2) GridView cell merge

Source: Internet
Author: User

We all use more than the GridView. There is a need to merge the cells you have encountered.

The principle of cell merging is simply to determine whether the value in the cell you want to merge is the same as the one in the previous row, and if the same is the case, then you will be judged by the different words.

We can add unit merges to the GridView by extending the method

public static Class Gridviewextensions
{
<summary>
GridView Row Merge
</summary>
<param name= "GridView" ></param>
<param name= "field" > Merge parameters (anonymous type)
ColumnIndex: Index of rows to be merged (must be specified at 0)
ID (optional): If the behavior template line must specify the
PropertyName: Text based on ID property default value
Colums: (string type) means that additional row merges are the same as columnindex (multiple separated by commas, such as colums= "5,6,7,8")
Cases:
Merges the first row (the first Behavior template row), and a label name for the binding is called Lblname the 6th row is merged according to the value of the Text property and the first row is the same
New {columnindex=0,id= "Lblname", propertyname= "Text", columns= "5"}
</param>
public static GridView RowSpan (the 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 + +)
{
Bound row Merge Processing
if (string. IsNullOrEmpty (ColumnName))
{
Compare 2 rows for the same value
if (gridview.rows[i). Cells [ColumnIndex]. Text = = Gridview.rows[j]. Cells[columnindex]. Text)
{
Number of merged rows +1
rowspancount++;
Hide the same line
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
{
Merge processing of template lines
if (GetPropertyValue (gridview.rows [i]). Cells[columnindex]. FindControl (ColumnName), propertyname) = = GetPropertyValue (gridview.rows[j). Cells[columnindex]. FindControl (ColumnName), PropertyName))
{
rowspancount++;
Hide the same line
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)
{
Row merge
Gridview.rows[i]. Cells[columnindex]. RowSpan = Rowspancount;
Determine if there are additional lines that need to be merged
if (!string. IsNullOrEmpty (columns))
{
Additional row merges
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)
{
Resultdictionary.add (Tempproperty.name, Tempproperty.getvalue (fields, NULL). ToString ());
}
Specify default values
if (!resultdictionary.keys.contains ("ColumnIndex"))
{
throw new Exception ("Do not specify index ColumnIndex property to merge Rows!");
}
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>
Gets an attribute of an object ...
</summary>
<param name= "obj" ></param>
<param name= "PropertyName" > Property name </param>
The value of the <returns> property and returns null</returns> if it is not available
Private static object GetPropertyValue (Object obj, String propertyname)
{
PropertyInfo property = obj. GetType (). GetProperty (PropertyName);

Return property. GetValue (Obj,null);
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.