Custom header row in .NET datagrid?
April 17, 2008 1:39 PM Subscribe
How do I insert a row of identical drop-down lists into the top of my .NET datagrid?
I have a DataGrid whose source is an excel spreadsheet. My GUI needs to allow a user to select an option from a drop-down above each column. I'd like this row to be a part of the grid so that everything lines up all pretty-like.
The best option I've found is to manually create all the rows and columns and bind that to the grid. Another option was to use a repeater of datalists instead. Is there a simpler or better way? (I'm using C#)
I have a DataGrid whose source is an excel spreadsheet. My GUI needs to allow a user to select an option from a drop-down above each column. I'd like this row to be a part of the grid so that everything lines up all pretty-like.
The best option I've found is to manually create all the rows and columns and bind that to the grid. Another option was to use a repeater of datalists instead. Is there a simpler or better way? (I'm using C#)
Best answer: You'll have to use a gridview for this, but it should work (adds a dropdown after the header row.)
posted by sanko at 4:42 PM on April 17, 2008
protected void foo_DataBound1(object sender, EventArgs e)
{
GridViewRow row = new GridViewRow(0, 0, DataControlRowType.DataRow, DataControlRowState.Normal);
for (int i=0; i<2>
{
DropDownList ddl = new DropDownList();
ddl.Items.Add("foo");
ddl.Items.Add("bar");
TableCell tc = new TableCell();
tc.Controls.Add(ddl);
row.Cells.Add(tc);
}
(sender as GridView).Controls[0].Controls.AddAt(1, row);
}
2>
posted by sanko at 4:42 PM on April 17, 2008
for (int i=0; i<2> { 2>should read
for (int i=0; i<2; i++) { // need to determine col. ct... Columns.Count returns 0
posted by sanko at 4:47 PM on April 17, 2008
Response by poster: Thanks sanko! I'll try that when I'm back at work.
posted by TimeTravelSpeed at 7:40 PM on April 17, 2008
posted by TimeTravelSpeed at 7:40 PM on April 17, 2008
Response by poster: Perfect sanko. Worked like a charm. Thank you very much!
posted by TimeTravelSpeed at 3:02 PM on April 21, 2008
posted by TimeTravelSpeed at 3:02 PM on April 21, 2008
This thread is closed to new comments.
posted by TimeTravelSpeed at 2:02 PM on April 17, 2008