protected void classmarkgrid_RowCreated(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.Header) { string examname = examdropdown.SelectedItem.Text; if (examname == "AdmissionNumber" || examname == "Rollnumber" || examname == "Name") { CustomizeGridHeader1((GridView)sender, e.Row, 2); } else { CustomizeGridHeader((GridView)sender, e.Row, 2); } } }
private void CustomizeGridHeader(GridView timeSheetGrid, GridViewRow gridRow, int headerLevels) { for (int item = 1; item <= headerLevels; item++) { //creating new header row GridViewRow gridviewRow = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Insert); IEnumerable<IGrouping<string, string>> gridHeaders = null; //reading existing header gridHeaders = gridRow.Cells.Cast<TableCell>() .Select(cell => GetHeaderText(cell.Text, item)) .GroupBy(headerText => headerText); foreach (var header in gridHeaders) { TableHeaderCell cell = new TableHeaderCell(); if (item == 2) { if (header.Key == "AdmissionNumber" || header.Key == "Rollnumber" || header.Key == "Name") { cell.Text = ""; } else { cell.Text = header.Key.Substring(header.Key.LastIndexOf(_seperator) + 1); } } else { cell.Text = header.Key.ToString(); if (!((cell.Text.Contains("AdmissionNumber")) || (cell.Text.Contains("Rollnumber")) || (cell.Text.Contains("Name")))) { cell.ColumnSpan = 1; } } gridviewRow.Cells.Add(cell); } // Adding new header to the grid timeSheetGrid.Controls[0].Controls.AddAt(gridRow.RowIndex, gridviewRow); } //hiding existing header gridRow.Visible = false; }