///
/// Given a DataGrid and an int X, this function will size any table columns contained in that DataGrid based on a best-fit size for the first X elements of that column
///
/// the DataGrid whose tables need sizing
/// the number of elements to analyze for sizing starting from the top, -1 for all
static public void DG_SizeColumnsToContent(System.Windows.Forms.DataGrid dataGrid, int nRowsToScan)
{
//first check to make sure the DataGrid has a valid datasource
if (dataGrid.DataSource == null)
{
//it does not
return;
}
// Create graphics object for measuring widths.
System.Drawing.Graphics Graphics = dataGrid.CreateGraphics();
// Define new table style.
System.Windows.Forms.DataGridTableStyle tableStyle;
//necessary b/c of the DataSet looping
int nRowsToScanOriginal = nRowsToScan;
bool scanAllRows;
if(-1 == nRowsToScan)
scanAllRows = true;
else
scanAllRows = false;
try
{
if (dataGrid.DataSource.GetType() == typeof(System.Data.DataSet))
{
System.Data.DataSet dataSet = (System.Data.DataSet)dataGrid.DataSource;
if(dataSet.Tables.Count == 0)
{
//if the DataSet it empty, nothing to do
return;
}
// Clear any existing table styles.
dataGrid.TableStyles.Clear();
foreach(System.Data.DataTable dataTable in dataSet.Tables)
{
if (scanAllRows)
{
nRowsToScan = dataTable.Rows.Count;
}
else
{
// Can only scan rows if they exist.
nRowsToScan = System.Math.Min(nRowsToScanOriginal,
dataTable.Rows.Count);
}
// Use mapping name that is defined in the data source.
tableStyle = new DataGridTableStyle();
tableStyle.MappingName = dataTable.TableName;
// Now create the column styles within the table style.
DataGridTextBoxColumn columnStyle;
int iWidth;
for (int iCurrCol = 0;
iCurrCol < dataTable.Columns.Count; iCurrCol++)
{
System.Data.DataColumn dataColumn = dataTable.Columns[iCurrCol];
columnStyle = new DataGridTextBoxColumn();
columnStyle.TextBox.Enabled = true;
if(dataColumn.Caption != "")
{
columnStyle.HeaderText = dataColumn.Caption;
}
else
{
columnStyle.HeaderText = dataColumn.Caption;
}
columnStyle.MappingName = dataColumn.ColumnName;
// Set width to header text width.
iWidth = (int)(Graphics.MeasureString
(columnStyle.HeaderText,
dataGrid.Font).Width);
// Change width, if data width is
// wider than header text width.
// Check the width of the data in the first X rows.
System.Data.DataRow dataRow;
for (int iRow = 0; iRow < nRowsToScan; iRow++)
{
dataRow = dataTable.Rows[iRow];
if (null != dataRow[dataColumn.ColumnName])
{
int iColWidth = (int)(Graphics.MeasureString
(dataRow.ItemArray[iCurrCol].ToString(),
dataGrid.Font).Width);
iWidth = (int)System.Math.Max(iWidth, iColWidth);
}
}
columnStyle.Width = iWidth + 4;
// Add the new column style to the table style.
tableStyle.GridColumnStyles.Add(columnStyle);
}
// Add the new table style to the data grid.
dataGrid.TableStyles.Add(tableStyle);
}
}
else if(dataGrid.DataSource.GetType() == typeof(System.Data.DataTable)) //the datagrid just has a DataTable
{
tableStyle = new DataGridTableStyle();
System.Data.DataTable dataTable = (System.Data.DataTable)dataGrid.DataSource;
if (-1 == nRowsToScan)
{
nRowsToScan = dataTable.Rows.Count;
}
else
{
// Can only scan rows if they exist.
nRowsToScan = System.Math.Min(nRowsToScan,
dataTable.Rows.Count);
}
// Clear any existing table styles.
dataGrid.TableStyles.Clear();
// Use mapping name that is defined in the data source.
tableStyle.MappingName = dataTable.TableName;
// Now create the column styles within the table style.
DataGridTextBoxColumn columnStyle;
int iWidth;
for (int iCurrCol = 0;
iCurrCol < dataTable.Columns.Count; iCurrCol++)
{
System.Data.DataColumn dataColumn = dataTable.Columns[iCurrCol];
columnStyle = new DataGridTextBoxColumn();
columnStyle.TextBox.Enabled = true;
if(dataColumn.Caption != "")
{
columnStyle.HeaderText = dataColumn.Caption;
}
else
{
columnStyle.HeaderText = dataColumn.ColumnName;
}
columnStyle.MappingName = dataColumn.ColumnName;
// Set width to header text width.
iWidth = (int)(Graphics.MeasureString
(columnStyle.HeaderText,
dataGrid.Font).Width);
// Change width, if data width is
// wider than header text width.
// Check the width of the data in the first X rows.
System.Data.DataRow dataRow;
for (int iRow = 0; iRow < nRowsToScan; iRow++)
{
dataRow = dataTable.Rows[iRow];
if (null != dataRow[dataColumn.ColumnName])
{
int iColWidth = (int)(Graphics.MeasureString
(dataRow.ItemArray[iCurrCol].ToString(),
dataGrid.Font).Width);
iWidth = (int)System.Math.Max(iWidth, iColWidth);
}
}
columnStyle.Width = iWidth + 4;
// Add the new column style to the table style.
tableStyle.GridColumnStyles.Add(columnStyle);
}
// Add the new table style to the data grid.
dataGrid.TableStyles.Add(tableStyle);
}
}
catch(Exception e)
{
MessageBox.Show(e.Message);
}
finally
{
Graphics.Dispose();
}
}