Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / August 2006

Tip: Looking for answers? Try searching our database.

Question about SizeDataGridColumnToContent function...

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
rhaazy - 11 Aug 2006 14:10 GMT
Using C#

string xmlfile = "appsettings.xml";
DataSet ds_login = new DataSet();
ds_login.ReadXml(xmlfile);
dgConnection.DataSource = ds_login;
dgConnection.DataMember = "Connection";
SizeDataGridColumnsToContent(dgConnection, 1);

The function works the way it is suppose to for every other datagrid I
have.
But for the one bound to the dataset with the XML I get an extra column

added.

the column is Connections_Id where Connections is an element name...

The column DOES NOT show up if I don't use the
SizeDataGridColumnsToContent function.

here is the function:
public void SizeDataGridColumnsToContent(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.

                       Graphics Graphics = dataGrid.CreateGraphics();

                       // Define new table style.
                       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(DataSet))
                               {
                                       DataSet dataSet =
(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(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++)
                                               {
                                                       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.
                                                       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(DataTable)) //the
datagrid just has a DataTable
                               {
                                       tableStyle = new
DataGridTableStyle();
                                       DataTable dataTable =
(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++)
                                       {
                                               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.
                                               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();
                       }

                }

Here is the XML file also if that helps...

 <?xml version="1.0" encoding="utf-8" ?>
- <AppSettings>
- <DatebaseConnections>
- <Connection>
 <Server>DSS-SERVER3</Server>
 <Database>ClientScanTest</Database>
 </Connection>
 </DatebaseConnections>
- <LoginSettings>
 <Admin>GaKFQUS2Oo92F6byJQGbEg==</Admin>
 </LoginSettings>
 </AppSettings>

This is the Output also...

Server         Database         DatabaseConnections_Id
DSS-SERVER3     ClientScantest        0

as seen in the datagrid the dataset is bound to.
rhaazy - 14 Aug 2006 17:59 GMT
> Using C#
>
[quoted text clipped - 310 lines]
>
> as seen in the datagrid the dataset is bound to.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.