ASP.NET GridView second header row to span main header row - c#

I have an ASP.NET GridView which has columns that look like this:
| Foo | Bar | Total1 | Total2 | Total3 |
Is it possible to create a header on two rows that looks like this?
| | Totals |
| Foo | Bar | 1 | 2 | 3 |
The data in each row will remain unchanged as this is just to pretty up the header and decrease the horizontal space that the grid takes up.
The entire GridView is sortable in case that matters. I don't intend for the added "Totals" spanning column to have any sort functionality.
Edit:
Based on one of the articles given below, I created a class which inherits from GridView and adds the second header row in.
namespace CustomControls
{
public class TwoHeadedGridView : GridView
{
protected Table InnerTable
{
get
{
if (this.HasControls())
{
return (Table)this.Controls[0];
}
return null;
}
}
protected override void OnDataBound(EventArgs e)
{
base.OnDataBound(e);
this.CreateSecondHeader();
}
private void CreateSecondHeader()
{
GridViewRow row = new GridViewRow(0, -1, DataControlRowType.Header, DataControlRowState.Normal);
TableCell left = new TableHeaderCell();
left.ColumnSpan = 3;
row.Cells.Add(left);
TableCell totals = new TableHeaderCell();
totals.ColumnSpan = this.Columns.Count - 3;
totals.Text = "Totals";
row.Cells.Add(totals);
this.InnerTable.Rows.AddAt(0, row);
}
}
}
In case you are new to ASP.NET like I am, I should also point out that you need to:
1) Register your class by adding a line like this to your web form:
<%# Register TagPrefix="foo" NameSpace="CustomControls" Assembly="__code"%>
2) Change asp:GridView in your previous markup to foo:TwoHeadedGridView. Don't forget the closing tag.
Another edit:
You can also do this without creating a custom class.
Simply add an event handler for the DataBound event of your grid like this:
protected void gvOrganisms_DataBound(object sender, EventArgs e)
{
GridView grid = sender as GridView;
if (grid != null)
{
GridViewRow row = new GridViewRow(0, -1,
DataControlRowType.Header, DataControlRowState.Normal);
TableCell left = new TableHeaderCell();
left.ColumnSpan = 3;
row.Cells.Add(left);
TableCell totals = new TableHeaderCell();
totals.ColumnSpan = grid.Columns.Count - 3;
totals.Text = "Totals";
row.Cells.Add(totals);
Table t = grid.Controls[0] as Table;
if (t != null)
{
t.Rows.AddAt(0, row);
}
}
}
The advantage of the custom control is that you can see the extra header row on the design view of your web form. The event handler method is a bit simpler, though.

I took the accepted answer approach, but added the header to the existing GridView instead of a custom inherited GridView.
After I bind my GridView, I do the following:
/*Create header row above generated header row*/
//create row
GridViewRow row = new GridViewRow(0, -1, DataControlRowType.Header, DataControlRowState.Normal);
//spanned cell that will span the columns I don't want to give the additional header
TableCell left = new TableHeaderCell();
left.ColumnSpan = 6;
row.Cells.Add(left);
//spanned cell that will span the columns i want to give the additional header
TableCell totals = new TableHeaderCell();
totals.ColumnSpan = myGridView.Columns.Count - 3;
totals.Text = "Additional Header";
row.Cells.Add(totals);
//Add the new row to the gridview as the master header row
//A table is the only Control (index[0]) in a GridView
((Table)myGridView.Controls[0]).Rows.AddAt(0, row);
/*fin*/

This article should point you in the right direction. You can programmatically create the row and add it to the collection at position 0.

Note for those who choose to use RowDataBound Method in VB.NET
If you end up with too many extra header rows popping up, add an If Statement that only proceeds if the gridview's header row is nothing (meaning it is the one currently being bound)
If grid.HeaderRow Is Nothing Then

You will have to create a class which extends gridview then override the CreateRow method.
try this as a starting point

Add t.EnableViewState = false; after you add the row:
Dim t As Table = TryCast(grid.Controls(0), Table)
If t IsNot Nothing Then
t.Rows.AddAt(0, row)
End If
t.EnableViewState = false;

Please refer to https://stackoverflow.com/a/9333714/1060656
i created this solution example
To run in your local system will will need to create 2 files ( one for the control and one aspx) you can either do it one project or 2 projects.
GridViewPlus ==> Control class
GridViewPlusCustomHeaderRows ==> a collection to hold custom header class
CustomHeaderEventArgs ==> Event Args when custom header row is created
aspx file ==> Test program
public class GridViewPlus : GridView
{
public event EventHandler<CustomHeaderEventArgs> CustomHeaderTableCellCreated;
private GridViewPlusCustomHeaderRows _rows;
public GridViewPlus() : base ()
{
_rows = new GridViewPlusCustomHeaderRows();
}
/// <summary>
/// Allow Custom Headers
/// </summary>
public bool ShowCustomHeader { get; set; }
[PersistenceMode(PersistenceMode.InnerDefaultProperty)]
[MergableProperty(false)]
public GridViewPlusCustomHeaderRows CustomHeaderRows
{
get {return _rows; }
}
protected virtual void OnCustomHeaderTableCellCreated(CustomHeaderEventArgs e)
{
EventHandler<CustomHeaderEventArgs> handler = CustomHeaderTableCellCreated;
// Event will be null if there are no subscribers
if (handler != null)
{
// Use the () operator to raise the event.
handler(this, e);
}
}
protected override void OnRowCreated(GridViewRowEventArgs e)
{
if (ShowCustomHeader && e.Row.RowType == DataControlRowType.Header) return;
base.OnRowCreated(e);
}
protected override void PrepareControlHierarchy()
{
//Do not show the Gridview header if show custom header is ON
if (ShowCustomHeader) this.ShowHeader = false;
base.PrepareControlHierarchy();
//Safety Check
if (this.Controls.Count == 0)
return;
bool controlStyleCreated = this.ControlStyleCreated;
Table table = (Table)this.Controls[0];
int j = 0;
if (CustomHeaderRows ==null )return ;
foreach (TableRow tr in CustomHeaderRows)
{
OnCustomHeaderTableCellCreated(new CustomHeaderEventArgs(tr,j));
table.Rows.AddAt(j, tr);
tr.ApplyStyle(this.HeaderStyle);
j++;
}
}
}
public class GridViewPlusCustomHeaderRows : System.Collections.CollectionBase
{
public GridViewPlusCustomHeaderRows()
{
}
public void Add(TableRow aGridViewCustomRow)
{
List.Add(aGridViewCustomRow);
}
public void Remove(int index)
{
// Check to see if there is a widget at the supplied index.
if (index > Count - 1 || index < 0)
// If no widget exists, a messagebox is shown and the operation
// is cancelled.
{
throw (new Exception("Index not valid"));
}
else
{
List.RemoveAt(index);
}
}
public TableRow Item(int Index)
{
// The appropriate item is retrieved from the List object and
// explicitly cast to the Widget type, then returned to the
// caller.
return (TableRow)List[Index];
}
}
public class CustomHeaderEventArgs : EventArgs
{
public CustomHeaderEventArgs(TableRow tr ,int RowNumber )
{
tRow = tr;
_rownumber = RowNumber;
}
private TableRow tRow;
private int _rownumber = 0;
public int RowNumber { get { return _rownumber; } }
public TableRow HeaderRow
{
get { return tRow; }
set { tRow = value; }
}
}
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Example1();
GridViewExtension1.CustomHeaderTableCellCreated += new EventHandler<CustomHeaderEventArgs>(GridViewExtension1_CustomHeaderTableCellCreated);
}
void GridViewExtension1_CustomHeaderTableCellCreated(object sender, CustomHeaderEventArgs e)
{
TableRow tc = (TableRow)e.HeaderRow;
tc.BackColor = System.Drawing.Color.AliceBlue;
}
private void Example1()
{
System.Data.DataTable dtSample = new DataTable();
DataColumn dc1 = new DataColumn("Column1",typeof(string));
DataColumn dc2 = new DataColumn("Column2",typeof(string));
DataColumn dc3 = new DataColumn("Column3",typeof(string));
DataColumn dc4 = new DataColumn("Column4",typeof(string));
// DataColumn dc5 = new DataColumn("Column5",typeof(string));
dtSample.Columns.Add(dc1);
dtSample.Columns.Add(dc2);
dtSample.Columns.Add(dc3);
dtSample.Columns.Add(dc4);
// dtSample.Columns.Add(dc5);
dtSample.AcceptChanges();
for (int i = 0; i < 25; i++)
{
DataRow dr = dtSample.NewRow();
for (int j = 0; j < dtSample.Columns.Count; j++)
{
dr[j] = j;
}
dtSample.Rows.Add(dr);
}
dtSample.AcceptChanges();
//GridViewExtension1.ShowHeader = false;
GridViewExtension1.ShowCustomHeader = true;
/*
*=======================================================================
* |Row 1 Cell 1 | Row 1 Col 2 (Span=2) | Row 1 Col 3 |
* | | | |
*=======================================================================
* |Row 2 Cell 1 | | | |
* | | Row 2 Col 2 | Row 2 Col 3 |Row 2 Col 4 |
*=======================================================================
*
*
*
*
* */
// SO we have to make 2 header row as shown above
TableRow TR1 = new TableRow();
TableCell tcR1C1 = new TableCell();
tcR1C1.Text = "Row 1 Cell 1";
tcR1C1.ColumnSpan = 1;
TR1.Cells.Add(tcR1C1);
TableCell tcR1C2 = new TableCell();
tcR1C2.Text = "Row 1 Cell 2";
tcR1C2.ColumnSpan = 2;
TR1.Cells.Add(tcR1C2);
TableCell tcR1C3 = new TableCell();
tcR1C3.Text = "Row 1 Cell 3";
tcR1C3.ColumnSpan = 1;
TR1.Cells.Add(tcR1C3);
GridViewExtension1.CustomHeaderRows.Add(TR1);
TableRow TR2 = new TableRow();
TableCell tcR2C1 = new TableCell();
tcR2C1.Text = "Row 2 Cell 1";
tcR2C1.ColumnSpan = 1;
TR2.Cells.Add(tcR2C1);
TableCell tcR2C2 = new TableCell();
tcR2C2.Text = "Row 2 Cell 2";
tcR2C2.ColumnSpan = 1;
TR2.Cells.Add(tcR2C2);
TableCell tcR2C3 = new TableCell();
tcR2C3.Text = "Row 2 Cell 3";
tcR2C3.ColumnSpan = 1;
TR2.Cells.Add(tcR2C3);
TableCell tcR2C4 = new TableCell();
tcR2C4.Text = "Row 2 Cell 4";
tcR2C4.ColumnSpan = 1;
TR2.Cells.Add(tcR2C4);
GridViewExtension1.CustomHeaderRows.Add(TR2);
GridViewExtension1.DataSource = dtSample;
GridViewExtension1.DataBind();
}
}

I wanted to do a similar task but required clickable buttons inside the header - none of the above worked in that case as the event handlers were not wired up (due to the sequencing of the events). In the end i used the headertemplate tag in the appropriate templatefield of the grid view. The html looks a bit more bloated but the events remain intact with no additional code behind effort. For example
<asp:TemplateField >
<HeaderTemplate>
<div>
<div style="text-align: center;padding-bottom: 5px;">
text
</div>
<div>
<asp:Button ID="Button1" runat="server" Text="Apply to all" ToolTip="Apply to all - Special Bolt On" CssClass="sub_button input_btn_5" OnClick="ApplyButton1_Click" />
</div>
</div>
</HeaderTemplate>
<ItemTemplate>....

Related

How to achieve multiple colspan and multiple header row using ASP.Net gridview?

I'm in a situation were need to achieve multiple colspan and two header row using ASP.Net gridview.
Something like below
+----------+--------------------------------+--------------------------------+
|Name |English |Math |
| |----------+----------+----------+----------+----------+----------+
| |1st Term |2nd Term |3rd Term |1st Term |2nd Term |3rd Term |
+----------+----------+----------+----------+----------+----------+----------+
|Adam |50 |60 |70 |55 |65 |75 |
+----------+----------+----------+----------+----------+----------+----------+
|Smith |52 |62 |72 |57 |68 |70 |
+----------+----------+----------+----------+----------+----------+----------+
Is it possible to do it?
Edit : Number of rows and / or columns or even sub-headers of a header is not fixed.
Access to the RowSpan and ColumnSpan for a grid view is done through the Cells property.
To access the header cells:
//Replace ColumnSpan with RowSpan if needed (and if you can get multiple header rows)
Gridview1.HeaderRow.Cells[CELL_INDEX].ColumnSpan = 2; //Need to know the cell index
To access the cells of a normal row:
//Replace ColumnSpan with RowSpan if needed
Gridview1.Rows[ROW_INDEX].Cells[CELL_INDEX].ColumnSpan = 2; //Need to know the row index and the cell index in that row
The multiple header row might be a bit more tricky. I've never done it, but the guy here seemed to have a good answer: How to add Header and Subheader in Gridview
It can be done, but it takes a bit of trial and error to get the design you want. Use the GridView OnRowDataBound event. It would be easier to do this after the GridView is build, especially for RowSpan.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
int spanColumn_1_start = 1;
int spanColumn_1_length = 3;
//apply colspan
e.Row.Cells[spanColumn_1_start].ColumnSpan = 3;
//remove the spanned cells
for (int i = 1; i < spanColumn_1_length; i++)
{
e.Row.Cells.RemoveAt(spanColumn_1_start + 1);
}
//note that the startindex of the 2nd colspan is set after removing cells for 1st colspan
int spanColumn_2_start = 2;
int spanColumn_2_length = 3;
//apply colspan
e.Row.Cells[spanColumn_2_start].ColumnSpan = 3;
//remove the spanned cells
for (int i = 1; i < spanColumn_2_length; i++)
{
e.Row.Cells.RemoveAt(spanColumn_2_start + 1);
}
}
else if (e.Row.RowType == DataControlRowType.DataRow)
{
int rowIndex = e.Row.DataItemIndex;
//to make a rowspan you have to work backwards since the next row does not exist yet
if (rowIndex == 1)
{
GridView1.Rows[rowIndex - 1].Cells[0].RowSpan = 2;
e.Row.Cells.RemoveAt(0);
}
//span 4 rows in column 3 starting at row 6
if (rowIndex == 9)
{
int rowSpan = 4;
int columnIndex = 3;
//apply rowspan
GridView1.Rows[rowIndex - rowSpan].Cells[columnIndex].RowSpan = rowSpan;
//remove the spanned rows
for (int i = 1; i < rowSpan; i++)
{
GridView1.Rows[rowIndex - (rowSpan - i)].Cells.RemoveAt(columnIndex);
}
}
}
}
The above snippet will give the following result.
UPDATE
To add an extra header row you need to use the OnRowCreated event of the GridView.
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
//cast the sender back to a gridview
GridView gv = sender as GridView;
//check if the row is the header row
if (e.Row.RowType == DataControlRowType.Header)
{
//create a new row
GridViewRow extraHeader = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Insert);
extraHeader.BackColor = Color.Green;
//loop all the columns and create a new cell for each
for (int i = 0; i < gv.Columns.Count; i++)
{
TableCell cell = new TableCell();
cell.Text = "ExtraHeader " + i;
//add the cell to the new header row
extraHeader.Cells.Add(cell);
}
//add the new row to the gridview
gv.Controls[0].Controls.AddAt(0, extraHeader);
}
}

Access Table from Code behind

I have this code:
for(int u = 0; u < 3; u++)
{
TableRow row = new TableRow();
for(int i = 0; i < 3; i++)
{
TableCell cell = new TableCell();
cell.ID = "test"+i;
cell.Text = "Cell" + i + "-" +u;
row.Cells.Add(cell);
}
tabAntraege.Rows.Add(row);
}
So now I have an ASP.NET table like so:
| Cell0-0 | Cell1-0 | Cell2-0 |
| Cell0-1 | Cell1-1 | Cell2-1 |
| Cell0-2 | Cell1-2 | Cell2-2 |
Now I want to change the Text in cell "Cell0-0".
So I wrote this code:
TableRow t1 = tabAntraege.Rows[0];
TableCell t2 = t1.Cells[0];
t2.Text = "new Text"
When I try to use my code I get the following error:
system.argumentoutofrangeexception specified argument was out of the range of valid values
Somebody got an idea?
Here the code from the Button event:
protected void test(object sender, EventArgs e)
{
if(tabAntraege.Rows.Count != 0)
{
TableRow t1= tabAntraege.Rows[0];
TableCell t2 = t1.Cells[0];
t2.Text = "new Text";
}
}
I am assuming from what you've posted that you are declaring the table in the markup, but filling it with TableRow and TableCell instances during the Load event.
You'll have to re-create the rows that you've put in the table every time you post back, as well as any controls you've put in the table cells.
Doing that in the Page_Init handler will allow their content and attributes (such as background color, etc.) to be persisted across postbacks.
List controls (such as the DataGrid, DataList, and GridView) will do this automatically if they're databound, but an ordinary Table will not.
Your code should work, but can you try the following:
tabAntraege.Rows[0].Cells[0].Text = "new Text";
Can you check in your code if the tabAntraege has rows first.

Dynamically add Columns to DataGrid in wpf

I am currently working on a custom canvas and in that i have to add a table,So i thought dataGrid would be fine. SO i Want to create a "Table" from "Datagrid" by which user can add a table to the canvas at runtime.
Till now, I have tried to Populate DataGrid With a list and succeded.
How Can I add Columns to a Datagrid at runtime,such that the number of columns and header value Would be taken from the user at runtime using a textbox and based on the value of the textbox the datagrid should add columns and header value.
Actually I want to develop a Table in which user passes the no of columns and the column header and the table should be generated.
Or
"Can you suggest me with a way where i should look in order to to "Draw" a Table using DrawingVisual class"
It is a part of GraphicsTable Class
//Custom Classes "DrawingCanvas & GraphicsTable"
public void CreateDataGrid(GraphicsTable graphicsTable, DrawingCanvas drawingCanvas)
{
dt = new DataGrid();
dt.Name = "Data";
dt.ItemsSource = person();
dt.AllowDrop = true;
dt.AutoGenerateColumns = true;
dt.Height = graphicsTable.Rectangle.Height;
dt.Width = graphicsTable.Rectangle.Width;
drawingCanvas.Children.Add(dt);
Canvas.SetTop(dt, graphicsTable.Rectangle.Top);
Canvas.SetLeft(dt, graphicsTable.Rectangle.Left);
dt.Width = dt.Width;
dt.Height = dt.Height;
dt.Focus();
}
//I have just tried to add dome dummy data to the datagrid.
public List<Person> person()
{
List<Person> peep = new List<Person>();
peep.Add(new Person() {});
return peep;
}
public class Person
{
private string name;
private double salary;
public string Names
{
get { return name; }
set { name = value; }
}
public double Salary
{
get { return salary; }
set { salary = value; }
}
}
You can dynamically build the columns of a DataGrid as follows.
public void buildTable(string[] headers)
{
myGrid.Columns.Clear();
foreach (string header in headers)
{
DataGridTextColumn c = new DataGridTextColumn();
c.Header = header;
myGrid.Columns.Add(c);
}
}
If you are setting ItemsSource, however, the number of rows and columns will automatically adjust to match the value of ItemsSource. For example, the following code produces a DataGrid with 3 rows and 3 columns.
dt = new DataTable();
for (int i = 0; i < 3; i++)
dt.Columns.Add("col" + i.ToString());
for (int i = 0; i < 3; i++)
{
DataRow r = items.NewRow();
r[0] = "a" + i.ToString();
r[1] = "b" + i.ToString();
r[2] = "c" + i.ToString();
dt.Rows.Add(r);
}
myGrid.ItemsSource = dt;
+------+------+------+
| col0 | col1 | col2 |
+------+------+------+
| a0 | b0 | c0 |
+------+------+------+
| a1 | b1 | c1 |
+------+------+------+
| a2 | b2 | c2 |
+------+------+------+
Without knowing your exact requirements, I would not bother with manually drawing a table in code unless you have some special need custom graphics and even in that case I would look into using XAML to restyle the DataGrid or it's elements before attempting to render it myself. That's just my opinion though. Best of luck!
EDIT:
If you want to generate the table columns based on user input, you would just need to put the column generation code in a event handler. In your example you could add an event handler for the Textbox TextChanged event as follows. This event handler will run every time the text changes in the Textbox. You may want to add validation to prevent users from keying in large numbers.
private void numColsTextbox_TextChanged(object sender, TextChangedEventArgs e)
{
int numCols;
if (Int32.TryParse(tb.Text, out numCols))
{
myGrid.Columns.Clear();
for (int i = 1; i <= numCols; i++)
{
DataGridTextColumn c = new DataGridTextColumn();
c.Header = "Column " + i.ToString();
myGrid.Columns.Add(c);
}
}
}

get dynamic checkbox value on button click

i have a page where i create 2 checkboxes dynamically.
TableRow tr = new TableRow();
for (int i = 0; i < 2; i++)
{
TableCell Tc = new TableCell();
Tc.Attributes["style"] = "line-height: 30px; text-align: left";
Tc.Attributes["width"] = "50%";
Tc.Style.Add("padding-left", "5px");
//Checkboxes on left along with labels
CheckBox checkBoxCtrl = new CheckBox();
checkBoxCtrl.ID = "checkBoxCtrl" + i;
Tc.Controls.Add(checkBoxCtrl);
tr.Cells.Add(Tc);
}
once they are created in the page load event i have a Ok_button click event which requires to check if the checkbox is checked or not.
protected void Update2_Click(object sender, EventArgs e)
{
if(checkBoxCtrl.checked)
//here i wont be able to get the value
// i get the error the name checkBoxCtrl does not exist..
{
response.write("true");
}
}
but how do i do the check in this case.
thanks
Answer:
this is what needs to be done to get the checkbox values
protected void Update1_Click(object sender, EventArgs e)
{
for(int i = 0; i < ControlPropList.Count; i++)
{
CheckBox chkTest = (CheckBox)xxx.FindControl("checkBoxCtrl" + i);
{
if (chkTest.Checked)
{
Global.logger.Info("Checkbox True = " + chkTest.ID);
}
else
{
Global.logger.Info("Checkbox False = " + chkTest.ID);
}
}
}
}
This should work fine as long as you add the checkboxes to your page in the Page_PreInit method. If you add them after that (Page_Load for example), their values will not be maintained.
Read about the asp.net page lifecycle here:
http://msdn.microsoft.com/en-us/library/ms178472.aspx
Consider storing the dynamic checkbox in a local member:
private CheckBox _myCustomCheckbox = new CheckBox();
protected override void OnInit(EventArgs e)
{
TableRow tr = new TableRow();
for (int i = 0; i < 2; i++)
{
TableCell Tc = new TableCell();
if (i == 0)
{
Tc.Attributes["style"] = "line-height: 30px; text-align: left";
Tc.Attributes["width"] = "50%";
Tc.Style.Add("padding-left", "5px");
//Checkboxes on left along with labels
_myCustomCheckbox.ID = "checkBoxCtrl" + j;
Tc.Controls.Add(_myCustomCheckbox);
tr.Cells.Add(Tc);
}
}
// the row needs added to a page control so that the child control states can be loaded
SomeTableOnThePage.Controls.Add(tr);
base.OnInit(e);
}
protected void Update2_Click(object sender, EventArgs e)
{
if(_myCustomCheckbox.Checked)
{
response.write("true");
}
}
May not be quite what you want, but I had a similar issue, I have a dynamically generated table in ASP.NET page, with dynamically generated CheckBoxes in one column. I have created the data for the table from a collection, and then as the dynamic CB's are created I give them an ID and store them in a second collection, such as an array of CB's.
So when I need to find the Checked value I simply iterate through the collection, and I can find the ones that are Checked.
Also as they were created simultaneously with the data in the dynamic table I was able to easily tie the table data row to the Checkbox value.
This obviously assumes that the dynamic table and CB's were created using some kind of looping.
This may not be the best solution but works for my current needs.

Creating a header row with buttons in a Custom GridView

After posting this: Custom Header in GridView
...I have a related problem. I have added the table row during OnDataBound, and it shows up, the links are clickable. There are two problems with adding it here: first, if a postback occurs that doesn't DataBind, the row disappears; second, no events are happening when the LinkButtons are clicked. Here is the OnDataBound code:
protected override void OnDataBound(EventArgs e)
{
base.OnDataBound(e);
// Hook up the handler to create the Selection header/footer
// TODO: Wrap this in a function sometime
Table table = (Table)Controls[0];
GridViewRow row = new GridViewRow(-1, -1, DataControlRowType.EmptyDataRow, DataControlRowState.Normal);
// TODO: should add css classes
TableHeaderCell cell = new TableHeaderCell();
cell.ColumnSpan = Columns.Count + 1; // plus 1 for the checkbox column
cell.HorizontalAlign = HorizontalAlign.Left; // do this or css?
HtmlGenericControl label = new HtmlGenericControl("label");
label.InnerText = "Select:";
selectNoneLK = new LinkButton();
selectNoneLK.ID = "SelectNoneLK";
selectNoneLK.Text = "None";
selectNoneLK.Click += SelectNoneLK_Click;
//selectNoneLK.CommandName = "SelectNone";
//selectNoneLK.Command += SelectNoneLK_Click;
selectAllLK = new LinkButton();
selectAllLK.ID = "SelectAllLK";
selectAllLK.Text = "All on this page";
//selectAllLK.CommandName = "SelectAll";
//selectAllLK.Command += SelectAllLK_Click;
selectAllLK.Click += SelectAllLK_Click;
cell.Controls.Add(label);
cell.Controls.Add(selectNoneLK);
cell.Controls.Add(selectAllLK);
row.Controls.Add(cell);
// Find out where to put this row
int rowIndex = 0;
if(SelectionMode == SelectionMode.AboveHeader)
{
rowIndex = 0;
}
else if(SelectionMode == SelectionMode.BelowHeader)
{
rowIndex = 1;
}
else if(SelectionMode == SelectionMode.AboveFooter)
{
rowIndex = table.Rows.Count;
}
else if(SelectionMode == SelectionMode.BelowFooter)
{
rowIndex = table.Rows.Count + 1;
}
table.Rows.AddAt(rowIndex, row);
}
You can try putting it in the RowCreated Event, while the header is being created. This might also fix your problem with the LinkButtons not working.
void GridView1_RowCreated(Object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.Header)
{
...your code here
}

Categories

Resources