asp.net GridView and DataGrid same column merge implementation code

  • 2020-05-17 05:15:24
  • OfStack

(1) ordinary columns
 
/// <summary> 
/// Gridview Merge of columns (normal columns, not including template columns)  
///  Note: 1.GridView Group and sort the same rows at bind time 1 since  
/// 2. The method should be applied at the right time Gridview the DataBound Use in event  
/// </summary> 
/// <param name="gv"> merged-in GridView object </param> 
/// <param name="columnIndex"> The index of the column to be merged </param> 
public static void UnitCell(GridView gv, int columnIndex) 
{ 
int i = 0; // The current line number  
string lastType = string.Empty; // Currently determines whether to merge the values of the corresponding columns for the rows  
int lastCell = 0; // Determine the final 1 Index of rows of the 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> 
/// DataGrid Merge of columns (normal columns, not including template columns)  
///  Note: 1.DataGrid Group and sort the same rows at bind time 1 since  
/// 2. The method should be applied at the right time DataGrid the DataBound Use in event  
/// </summary> 
/// <param name="dg"> merged-in DataGrid object </param> 
/// <param name="columnIndex"> The index of the column to be merged </param> 
public static void UnitCell_T(DataGrid dg, int columnIndex) 
{ 
int i = 0; // The current line number  
string lastType = string.Empty; // Currently determines whether to merge the values of the corresponding columns for the rows  
int lastCell = 0; // Determine the final 1 Index of rows of the 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; 
} 
} 
} 

(2) template columns
 
/// <summary> 
/// Gridview Merge of columns (template columns)  
/// </summary> 
/// <param name="gv"> merged-in GridView object </param> 
/// <param name="columnIndex"> The index of the column to be merged </param> 
/// <param name="lblName"> Of the template column object ID</param> 
public static void UnitCell(GridView gv, int columnIndex, string lblName) 
{ 
int i = 0; // The current line number  
string lastType = string.Empty; // Currently determines whether to merge the values of the corresponding columns for the rows  
int lastCell = 0; // Determine the final 1 Index of rows of the 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; 
} 
} 
} 

(3) can be called in DataBound event.

Displays a fixed-width column in 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.
 
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.Cells[i].Width = Unit.Pixel(50); 
e.Item.Cells[i].Wrap = false; 
} 
} 
} 

Related articles: