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 Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Possible to get Datagrid header as an Object?

Status
Not open for further replies.

DLLHell

Programmer
May 9, 2003
69
US
Would it be possible to create a new Object in order to copy the DataGrid's header & its properties so that I can put it in a non-scrolling area, and then set the auto header's ShowHeader property to false? Any examples of this? I ask because trying to set up my own header based on the column sizes via MeasureString graphics does not work well given the DataGrid shrinks & adjusts the columns further after the header sizes are calculated... also the style width parameter for each column is not available even after DataBind.
 
dll - we had discussed this topic some time ago; and several people chimed in; but there was no ultimate solution. About the best I could do was to create an html table for the header and then put the datagrid within a <div> tag for scrolling.

I actually had pretty good luck with it -- but it seems that, perhaps unlike your situation, I had better luck at controlling the datagrid column widths with the column's style attribute. I actually made a couple of them, and they looked good and worked well, but from what I can remember it took some serious tweaking.
 
Hi Isadore - that is basically what I am doing, except I am putting the Header line in an ASP.NET Placeholder web control within a DIV area above the grid, and putting both DIV areas within one HTML Table (good or bad?). The reason for this is because the length of the datagrid varies and is not fixed-length, plus I use the Grid for Edit/Add/Delete functions.

Since I know the size of the Textbox areas, I am able to line up the header columns fairly well when in Edit/Add/Delete mode but not so well when in display-only mode.

Here is a little sample of the code:

Table t = new Table();
TableRow row = new TableRow();
TableCell cell0 = new TableCell();

t.BorderWidth = DataGrid1.BorderWidth;
t.BorderStyle = DataGrid1.BorderStyle;
t.BorderColor = DataGrid1.BorderColor;
t.CellPadding = DataGrid1.CellPadding;
t.CellSpacing = DataGrid1.CellSpacing;
t.GridLines = DataGrid1.GridLines;
t.Attributes [&quot;rules&quot;] = &quot;all&quot;;
t.Font.CopyFrom(DataGrid1.HeaderStyle.Font);
t.BackColor = DataGrid1.HeaderStyle.BackColor;
t.ForeColor = DataGrid1.HeaderStyle.ForeColor;
FontInfo fontInfo = DataGrid1.Font;
//For some reason our calcs are always 80 pixels off!
float dgLen = 80;SizeF size;

//dcMaxTxt0 would contain $123,456.78 for textboxes and
//could contain $0.00 through $123.456.78 for non-textboxes
size = MeasureString(dcMaxTxt0, fontInfo); //(below)
cell0.Height = Unit.Pixel(Convert.ToInt32(size.Height));
cell0.Width = Unit.Pixel(Convert.ToInt32(size.Width));
dgLen = dgLen + size.Width;
cell0.Text = &quot;<b>Hdr Col0</b>&quot;;
row.Cells.Add(cell0);

DataGrid1.Width = Convert.ToInt32(dgLen);
DataGrid1.DataSource = dataTable;
DataGrid1.DataBind();

string strStyle = &quot;&quot;;
string strTest = Convert.ToString(DataGrid1.Height);
strTest = Regex.Replace(strTest, &quot;px&quot;, &quot;&quot;);
int intScroll = Convert.ToInt32(strTest);
string strHeight = &quot;height:&quot; + Convert.ToString(intScroll) + &quot;px; &quot;;

strTest = Convert.ToString(DataGrid1.Width);
strTest = Regex.Replace(strTest, &quot;px&quot;, &quot;&quot;);
intScroll = Convert.ToInt32(strTest) + 16;
//add 16 for scroll column for DIV width in style
string strWidth = &quot;width:&quot; + Convert.ToString(intScroll) + &quot;px; &quot;;
strStyle = strHeight + strWidth + &quot; overflow-y: auto; overflow-x: hidden; Z-INDEX: 123; POSITION: absolute; LEFT: 0px; TOP: 123; &quot;;

divDataGrid.Attributes.Add(&quot;style&quot;, strStyle);
t.Width = intScroll - 16;
t.Rows.Add(row);
GridHeader.Controls.Add(t);
<end>

//COMPUTE PIXELS
SizeF MeasureString(string text, FontInfo fontInfo)
{
SizeF size;
float emSize;
emSize = Convert.ToSingle(fontInfo.Size.Unit.Value);
emSize = (emSize==0 ?9 :emSize); //9 as defined in CSS

Font stringFont = new Font(fontInfo.Name, emSize);
Bitmap bmp = new Bitmap(1000,100); //?Not sure of 1000/100?
Graphics g = Graphics.FromImage(bmp);

size = g.MeasureString(text, stringFont);
g.Dispose();
return size;
}


Any ideas or suggestions? Thanks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top