(i) Ordinary columns
Copy Code code as follows:
<summary>
Merge of the GridView column (normal column, no template column included)
Note: 1. The GridView groups and sorts when binding to allow the same rows to be grouped together
2. The timing of the application of the method should be used in the DataBound event of the GridView
</summary>
<param name= "GV" > The GridView object to be merged </param>
<param name= "ColumnIndex" > The index of the columns to be merged </param>
public static void Unitcell (GridView gv, int columnindex)
{
int i = 0; Current number of rows
String lasttype = String. Empty; Current determines whether the values of row-corresponding columns are merged
int Lastcell = 0; The index of the row that determines the last same value
if (GV). Rows.Count > 0)
{
Lasttype = GV. Rows[0]. Cells[columnindex]. Text.tostring ();
Gv. Rows[0]. Cells[columnindex]. RowSpan = 1;
Lastcell = 0;
}
for (i = 1; i < GV. Rows.Count; i++)
{
if (GV). Rows[i]. Cells[columnindex]. Text = = Lasttype)
{
Gv. Rows[i]. Cells[columnindex]. Visible = false;
Gv. Rows[lastcell]. Cells[columnindex]. rowspan++;
}
Else
{
Lasttype = GV. Rows[i]. Cells[columnindex]. Text.tostring ();
Lastcell = i;
Gv. Rows[i]. Cells[columnindex]. RowSpan = 1;
}
}
}
<summary>
Merge of DataGrid columns (normal columns, no template columns)
Note: 1. The DataGrid is grouped and sorted when binding to allow the same rows to be placed together
2. The timing of the application of the method should be used in the DataBound event of the DataGrid
</summary>
<param name= "DG" > The DataGrid object to be merged </param>
<param name= "ColumnIndex" > The index of the columns to be merged </param>
public static void unitcell_t (DataGrid dg, int columnindex)
{
int i = 0; Current number of rows
String lasttype = String. Empty; Current determines whether the values of row-corresponding columns are merged
int Lastcell = 0; The index of the row that determines the last same value
if (DG. Items.count> 0)
{
Lasttype = DG. Items[0]. Cells[columnindex]. Text.tostring ();
Dg. Items[0]. Cells[columnindex]. RowSpan = 1;
Lastcell = 0;
}
for (i = 1; i < DG. Items.Count; i++)
{
if (DG. Items[i]. Cells[columnindex]. Text = = Lasttype)
{
Dg. Items[i]. Cells[columnindex]. Visible = false;
Dg. Items[lastcell]. Cells[columnindex]. rowspan++;
}
Else
{
Lasttype = DG. Items[i]. Cells[columnindex]. Text.tostring ();
Lastcell = i;
Dg. Items[i]. Cells[columnindex]. RowSpan = 1;
}
}
}
(ii) Template columns
Copy Code code as follows:
<summary>
Merging of GridView columns (template columns)
</summary>
<param name= "GV" > The GridView object to be merged </param>
<param name= "ColumnIndex" > The index of the columns to be merged </param>
Id</param> of <param name= "Lblname" > Template Column objects
public static void Unitcell (GridView gv, int columnindex, string lblname)
{
int i = 0; Current number of rows
String lasttype = String. Empty; Current determines whether the values of row-corresponding columns are merged
int Lastcell = 0; The index of the row that determines the last same value
if (GV). Rows.Count > 0)
{
Lasttype = (GV. Rows[0]. Cells[columnindex]. FindControl (Lblname) as Label). Text;
Gv. Rows[0]. Cells[columnindex]. RowSpan = 1;
Lastcell = 0;
}
for (i = 1; i < GV. Rows.Count; i++)
{
if (GV. Rows[i]. Cells[columnindex]. FindControl (Lblname) as Label). Text = = Lasttype)
{
Gv. Rows[i]. Cells[columnindex]. Visible = false;
Gv. Rows[lastcell]. Cells[columnindex]. rowspan++;
}
Else
{
Lasttype = (GV. Rows[i]. Cells[columnindex]. FindControl (Lblname) as Label). Text.tostring ();
Lastcell = i;
Gv. Rows[i]. Cells[columnindex]. RowSpan = 1;
}
}
}
(iii) called in the DataBound event.
Display fixed-width columns in the GridView or DataGrid
By default, the GridView and DataGrid controls automatically resize columns based on their content. To specify a fixed width for the column, set the Width property for each TableCell object and set the Wrap property to False. The following example shows how to do this by using the DataGrid control.
Copy Code code as follows:
rotected void datagrid1_itemcreated (object sender, DataGridItemEventArgs E
{
ListItemType lit = e.item.itemtype;
if (lit = listitemtype.header)
{
for (int i = 0; i < E.item.cells.count; i++)
{
E.item.cel Ls[i]. Width = Unit.pixel (50);
E.item.cells[i]. Wrap = false;
}
}
}