Jump to content

windowsforms.Datagrid control question

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
hoser2001

hoser2001

    Programmer

  • Members
  • PipPipPipPip
  • 173 posts
I have a MDI and I'm trying to call the ResizeDatagridToContents Function found here:

void SizeDataGridColumnsToContent(DataGrid dataGrid, int nRowsToScan) 

		{

			// Create graphics object for measuring widths.

			Graphics Graphics = dataGrid.CreateGraphics();

      

			// Define new table style.

			DataGridTableStyle tableStyle = new DataGridTableStyle();

      

			try

			{

				//DataTable dataTable = (DataTable)dataGrid.DataSource;

				DataTable dataTable = ((DataSet)dataGrid.DataSource).Tables[0]; //for the 1st table

        

				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++)

				{

					DataColumn dataColumn = dataTable.Columns[iCurrCol];

          

					columnStyle = new DataGridTextBoxColumn();


					columnStyle.TextBox.Enabled = true;

					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.

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

			}

		}

THe datagrid that is being populated is not on the same form that is populating it. The only way I know of to call this function for this datagrid is like this:


SizeDataGridColumnsToContent(((frmMDIMain)this.ParentForm).dataGrid1, -1);


this does nothing... So i figured I would try to just put a button on the form with the datagrid and see if I could get the function to work in the same matter I have been using it like this:

SizeDataGridColumnsToContent(dataGrid1, -1);


However this does not do anything either... There is a difference between how this datagrid is filled and the rest of them, I populate it from an xml document like this:

DataSet ds = new DataSet(); 

				ds.ReadXml("appsettings.xml"); 

				((frmMDIMain)this.ParentForm).dataGrid1.DataSource = ds;

				((frmMDIMain)this.ParentForm).dataGrid1.DataMember = "DatabaseSource";



Anyone have any idea why this doesnt work, or any other ideas how I can get my datagrid columns to fit the text within.

BTW im using VS2003 w/ C#

#2
Guest_NeedHelp_*

Guest_NeedHelp_*
  • Guests
This function to resize was written by you?

If I were you, I would put a break point at the beginning and see if the code is even being executed.

#3
hoser2001

hoser2001

    Programmer

  • Members
  • PipPipPipPip
  • 173 posts
no i didnt write the resize function, did some research to find that, however the code works fine as I am currently using it in the same project throughout the application, however it seems to not like the idea that im calling the MDI parent form as I am using the function from the child.