CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Hybrid View

  1. #1
    Join Date
    Feb 2005
    Location
    Canada
    Posts
    204

    No autosize on Datagrid???

    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!
    Will Rate Posts for help!

  2. #2
    Join Date
    Feb 2005
    Location
    Texas, U.S.A
    Posts
    81

    Re: No autosize on Datagrid???

    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.

  3. #3
    Join Date
    Feb 2005
    Posts
    4

    Cool Re: No autosize on Datagrid???

    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

  4. #4
    Join Date
    Nov 2004
    Location
    Poland
    Posts
    1,355

    Re: No autosize on Datagrid???

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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured