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#)
posted by TimeTravelSpeed to Computers & Internet (5 answers total) 2 users marked this as a favorite
 
Response by poster: I should clarify that the loaded spreadsheet is different every single time. The solution needs to dynamically apply to spreadsheets of arbitrary size and with arbitrary column names.
posted by TimeTravelSpeed at 2:02 PM on April 17, 2008


Best answer: You'll have to use a gridview for this, but it should work (adds a dropdown after the header row.)


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);


}

posted by sanko at 4:42 PM on April 17, 2008


 for (int i=0; i<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


Response by poster: Perfect sanko. Worked like a charm. Thank you very much!
posted by TimeTravelSpeed at 3:02 PM on April 21, 2008


« Older Childhood movie memory   |   Podcasts for Rio Carbon? Newer »
This thread is closed to new comments.