Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

GridView Extra Header Row Problem

Status
Not open for further replies.

sheila11

Programmer
Dec 27, 2000
251
US
Hi all,

I am adding an extra row into the header using following code:

GridViewRow row = new GridViewRow(-1, -1, DataControlRowType.Header, DataControlRowState.Normal );

TableCell cel = new TableHeaderCell();
cel.ColumnSpan = 2;
row.Cells.Add(cel);

System.Web.UI.WebControls.Table t = grid.Controls[0] as System.Web.UI.WebControls.Table;
t.Rows.AddAt(0, row);

This works fine. However, when I submit the GridView using Submit button, on postback I get data for one row less.

When I parse through the gridview rows, total number of rows received are correct. But I get the 2nd header row as the first row, and the last datarow is missing.

for (int i = 0; i < gdv.Rows.Count; i++)//parsing each row
{
txtBook = gdv.Rows.Cells[1].Text;
}

What am I doing wrong?
Any tips?

TIA,
Sheila

 
Why are you dynamically building a grid? Why not just query your database and return a datatable, and then bind that to your grid?
 
I am not building the grid dynamically, it gets bound as you describe. However, some columns belong to one group and some other belong to another. So I need to have an extra header-row at the top to indicate the groups. I am adding only that extra header-row dynamically after the Grid is bound, in the 'RowCreated' event function.

 
I would create an object that reflects how the data is presented to the user. then use a repeater and some use controls (if needed) to build out the UI. this will be easier to maintain that forcing tabular data into an non-tabular format.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
I agree with Jason, a repeater would make more sense and would be easier to create the HTML that you actually want to render.
 
I agree with you guys, but I don't have that option. Also, there are a few articles on web showing how to add extra header-row, so I thought the GridView supports such manipulations. .. may be I was wrong ?
 
Sure anything is usually possible, but the HTML that the grid renders is not easily manipulated. That is why we suggested the repeater which allows for customization of the HTML and is meant for just the issue you are having.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top