Need *formatted* value from DataGrid...possible?

I

intrepid_dw

Hello, all.

I have a Windows Forms app with a datagrid control, with several custom

column styles applied. One of the columns holds a decimal data type,
with a format string of "C" for currency, and this works precisely as
intended, eg the value 1234.56 is formatted as $1,234.56.

In another portion of the application, I need to get the *formatted*
rendering of that value from the grid. I've tried the simple
grid[row,col] syntax, but it merely returns the underlying value, but I
need the resultant value after the column style is applied. My ultimate
intent is to measure the width of the formatted string to autosize the
width of the column, and what I have now only formats the raw data, and
that won't work.

Is what I need even possible? I've gone through the DataGrid FAQ and
MSDN and not found what I'm needing, so I'm turning here for help.


Many thanks in advance,
-intrepid
 
G

GM3TEN

intrepid,

Here is some code that may help you.

Regards,
GM
http://nonspect.com

//BEGIN CODE SNIP


/// <summary>
/// Formats the custom column styles to evenly distribute and fill the
entire
/// datagrid display area.
/// </summary>
/// <param name="oGrid">datagrid to format</param>
/// <param name="sTableStyleName">name of the custom table style for
the supplied datagrid</param>
/// <param name="p_blnShowRowHeaders">boolean indicating whether or not
row headers should be displayed</param>
public static void AutoSizeCols(DataGrid oGrid, string sTableStyleName,
bool p_blnShowRowHeaders)
{
if(oGrid.TableStyles.Contains(sTableStyleName) && oGrid.DataSource !=
null)
{
//TODO - This method assumes the DataSource to be a
DataTable...should add defensive code.
DataTable oTable = (DataTable) oGrid.DataSource;
float width = 0;
Graphics g = Graphics.FromHwnd(oGrid.Handle);
StringFormat sf = new
StringFormat(StringFormat.GenericTypographic);

SizeF size = new SizeF(0, 0);

if(oTable != null && oTable.Rows.Count > 0)
{
int numAutoSizedColsTotalWidth = 0;

//process each column style
foreach(DataGridColumnStyle oColStyle in
oGrid.TableStyles[sTableStyleName].GridColumnStyles)
{
//run through each row to get the width of the longest
//text value for the column style we are currently processing...
foreach(DataRow oRow in oTable.Rows)
{
if(oRow[oColStyle.MappingName] != null)
{
if(oRow[oColStyle.MappingName].ToString().Length == 0)
size = g.MeasureString(oColStyle.HeaderText.Trim(),
oGrid.HeaderFont, 500, sf);
else
size =
g.MeasureString(oRow[oColStyle.MappingName].ToString().Trim(),
oGrid.Font, 500, sf);
}
if(size.Width > width)
width = size.Width;
}
oColStyle.Width = Convert.ToInt32(width) + 19;

width = 0;
numAutoSizedColsTotalWidth += oColStyle.Width;
}

//show or hide the row headers here...depends on context
oGrid.TableStyles[sTableStyleName].RowHeadersVisible =
p_blnShowRowHeaders;

int numRowDataWidth = 0;
if(oGrid.TableStyles[sTableStyleName].RowHeadersVisible)
numRowDataWidth = oGrid.ClientSize.Width -
oGrid.TableStyles[sTableStyleName].RowHeaderWidth;
else
numRowDataWidth = oGrid.ClientSize.Width;

//get additional column width to fill entire DataGrid (if
applicable)
int numAdditionalWidth = 0;
if(numAutoSizedColsTotalWidth < numRowDataWidth)
{
numAdditionalWidth = ((numRowDataWidth -
numAutoSizedColsTotalWidth)
/ oGrid.TableStyles[sTableStyleName].GridColumnStyles.Count);
}
//apply the additional width (if applicable)
if(numAdditionalWidth > 0)
{
foreach(DataGridColumnStyle oStyle in
oGrid.TableStyles[sTableStyleName].GridColumnStyles)
{
oStyle.Width = oStyle.Width + numAdditionalWidth;
}
}
oGrid.Refresh();
}

g.Dispose();
}
}


//END CODE SNIP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top