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.

Re: Question about SizeDataGridColumnToContent function... by rhaazy

rhaazy
Mon Aug 14 11:59:07 CDT 2006


rhaazy wrote:
> 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.