Click to See Complete Forum and Search --> : No autosize on Datagrid???


Ctwizzy
February 24th, 2005, 01:07 PM
Is there anyway of telling the datagrid to autosize horizontally? Or both? This seems like it would be a needed feature for sure.

Also. 2 of the fields on my datagrid are Date/Time I need to have these formatted from 12/12/05 to 12:36:05 PM? Is this possible also?

Thanks!

checksal
February 24th, 2005, 11:16 PM
1.) Is this for ASP.Net or windows datagrid?
2.) You can use Convert.ToDateTime(value).ToShortTimeString(); and also use a regularexpression to add the PM or AM.

smartravitej
February 25th, 2005, 01:54 AM
Instead You can also use a this conversion in the SQl statement only
there si CONVERT function in SQL statements to display in which ever format you want.
eg.
CONVERT(varchar, '10:00:00', 108))
and regarding autisizing just put wrap option to false

WRAP = false in the datagrid options

Krzemo
February 25th, 2005, 02:13 AM
I think it could help:


///
/// 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();
}
}