Problem Statement:
If you have a requirement to keep AutoGenerateColumns="true" and still mamipulate gridview columns.
Solution:
Any <asp:TemplateField> with AutoGenerateColumns="true" should be generic to all rows.
On grid OnRowCreated event iterate over the row type and read item.ContainingField , this is where all the cell values are.
rdbSummaryReport.Checked – logic create a multiple common header and keeps the css applied to <th>
Below source code is a working example :
<asp:GridView ID="grdReconResults" runat="server" AutoGenerateColumns="true"
AllowPaging="false" AllowSorting="false" OnRowCreated="grdReconResults_OnRowCreated" >
<Columns>
<asp:TemplateField> //generates a ‘+’ link button in the first column of the grid
<ItemTemplate>
<asp:LinkButton ID="lnkExpand" runat="server" OnClientClick="return javascript:alert('clicked');" Text="+" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
protected void grdReconResults_OnRowCreated(object sender, GridViewRowEventArgs e)
{
System.Drawing.Color highlighted = System.Drawing.Color.Yellow;
if (rdbDetailReport.Checked)
{ //modification for detailed report
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataRowView drv = (DataRowView)e.Row.DataItem;
if (drv != null)
{
DataRow dataRow = drv.Row;
foreach (DataControlFieldCell item in ((GridViewRow)e.Row).Controls)
{
DataControlField x = item.ContainingField;
if (!x.HeaderText.Equals(string.Empty)) //avoid column from markup
{
if (x.HeaderText.Contains("Diff_")) // || x.HeaderText.Contains("Hierarchy"))
{
x.Visible = false;
}
else
{
string[] parts = x.HeaderText.Split(new char[] { '_' }, 2);
string pComputField = string.Format("Diff_{0}", parts[1]);
if (dataRow.Table.Columns.IndexOf(pComputField) >= 0
&& ((bool)dataRow[pComputField]))
{
item.BackColor = highlighted;
}
}
}
}
}
}
}
if (rdbSummaryReport.Checked)
{ //modification for summary report
if (e.Row.RowType == DataControlRowType.Header)
{
int colSpanThree = 3;// for comparison field combined header span 3 rows
bool isColSpanThreeActive = false;
int colSpanOne = Convert.ToInt32(Decimal.One);
GridView grd = (GridView)sender;
GridViewRow gRow = (GridViewRow)e.Row;
GridViewRow grdHeaderRow = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Insert);
TableHeaderCell cell = null;
List<Control> notRequiredTH = new List<Control>();
foreach (DataControlFieldHeaderCell item in gRow.Controls)
{
DataControlField field = item.ContainingField;
if (field.HeaderText.Contains("Hierarchy"))
{
field.Visible = false;
}
else
{
if (isColSpanThreeActive)
{
--colSpanThree;
field.HeaderText = field.HeaderText.Substring(0, field.HeaderText.IndexOf('_'));
if (colSpanThree == Convert.ToInt32(Decimal.Zero))
{
isColSpanThreeActive = false;
colSpanThree = 3;
}
}
else
{
bool isComparisonCol = selectedMapping.Mappings.Exists(x => x.type == MappingRelation.MappingType.SelectSum
&& (field.HeaderText.Contains(x.Y) | field.HeaderText.Contains(x.X)));
if (isComparisonCol)
{
cell = new TableHeaderCell();
cell.Text = field.HeaderText.Split(new char[] { '_' }, 2)[1];
cell.ColumnSpan = colSpanThree;
cell.HorizontalAlign = HorizontalAlign.Center;
grdHeaderRow.Cells.Add(cell);
field.HeaderText = field.HeaderText.Substring(0, field.HeaderText.IndexOf('_'));
isColSpanThreeActive = true;
--colSpanThree;
}
else
{
cell = new TableHeaderCell();
cell.Text = field.HeaderText;
cell.RowSpan = colSpanOne + colSpanOne;
grdHeaderRow.Cells.Add(cell);
notRequiredTH.Add(item);
}
}
}
}
notRequiredTH.ForEach(x => gRow.Controls.Remove(x));
grdReconResults.Controls[0].Controls.AddAt(gRow.RowIndex, grdHeaderRow);
}
}
}
private string GetHeaderText(string headerText, int headerLevel)
{
if (headerLevel == 2)
{
return headerText;
}
return headerText.Substring(0, headerText.LastIndexOf("."));
}