By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
460,020 Members | 1,348 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 460,020 IT Pros & Developers. It's quick & easy.

2003 Datagrid and beyond

P: n/a
cj
I was looking over some of my 2003 code today (see below) that loads a
foxpro table via oledb connection.

I used a sub "autosizecolumns" I found on the web but I never quite
understood why they said the style name had to be the same as the table
name. Perhaps the are refering to ts1.mappingname must be the same as
the table name (datagrid1.datamember) because I've read that that is how
the datagrid associates a style with what it's displaying. Is that it?

Also, I can't figure out how I pulled off using
system.drawing.color.fromname("info"). I think that was something I
came up with but it makes no sense now. Can someone enlighten me?

Lastly all this is probably a mute point now as I see we have a new
control in 2005 (datagridview). So I started typing in this code from
scratch in 2005 to see how it works. I got to datagrid1.setdatabinding
only to find it doesn't exist. How do I attach the datatable in the
dataset to the datagrid in 2005?

mySQLCommand.CommandText = fields & from & where & order
mySQLCommand.Connection = myFoxProConnection
myDataAdapter.SelectCommand = mySQLCommand

myDataAdapter.Fill(ds, tableName)

DataGrid1.SetDataBinding(ds, tableName)

Dim ts1 As New DataGridTableStyle
ts1.ReadOnly = True
ts1.RowHeadersVisible = False
ts1.ColumnHeadersVisible = False
ts1.AlternatingBackColor = System.Drawing.Color.FromName("info")
ts1.MappingName = DataGrid1.DataMember
DataGrid1.TableStyles.Add(ts1)

AutoSizeColumns(DataGrid1)

' ************************************************** ********
' Sizes all columns of a datagrid optimally.
'
' Parameters:
' TempDG - The datagrid that owns the column
' Notes:
' Ensure that a style exists for the table you are
' viewing and that the style name is the same as the table name.
' ************************************************** ********
Public Sub AutoSizeColumns(ByRef TempDG As DataGrid)
Dim iCount As Integer
For iCount = 0 To (TempDG.VisibleColumnCount() - 1)
AutoSizeCol(iCount, TempDG)
Next
End Sub

' ************************************************** ********
' Sizes a column of a single datagrid column optimally.
'
' Parameters:
' iCol - The column to resize
' TempDG - The datagrid that owns the column
' Notes:
' Make sure the column is a valid one. Also, ensure
' that a style exists for the table you are viewing and that
' the style name is the same as the table name.
' found on experts-exchange.com
' ************************************************** ********
Public Sub AutoSizeCol(ByVal iCol As Integer, ByRef TempDG As DataGrid)
Dim iMaxWidth, iTemp As Integer
Dim iNumRows As Integer = TempDG.BindingContext(TempDG.DataSource,
TempDG.DataMember).Count
Dim G As Graphics
Dim sf As StringFormat = New
StringFormat(StringFormat.GenericTypographic)
Dim sTemp, sTableName As String
Dim Size As SizeF

' Get a graphics handle
G = Me.CreateGraphics

' Loop thru all rows of this column. Get length of longest string
If iNumRows 0 Then
For iTemp = 0 To (iNumRows - 1)
sTemp = TempDG(iTemp, iCol).ToString
Size = G.MeasureString(sTemp, TempDG.Font, 500, sf)
Size.Width += 10
If Size.Width iMaxWidth Then iMaxWidth = Size.Width
Next iTemp
End If

sTableName = TempDG.DataMember

' Lastly, consider length of column header name
sTemp =
TempDG.TableStyles(sTableName).GridColumnStyles(iC ol).HeaderText()
Size = G.MeasureString(sTemp, TempDG.Font, 500, sf)
Size.Width += 10
If Size.Width iMaxWidth Then iMaxWidth = Size.Width

' Modify the width of the grid column style
TempDG.TableStyles(sTableName).GridColumnStyles(iC ol).Width = iMaxWidth
MsgBox("Width of Column " & iCol & " is " & iMaxWidth,
MsgBoxStyle.Information, "FYI")
G.Dispose()
End Sub
May 11 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Hi Cj,
but I never quite understood why they said the style name had to be the
same as the table name. Perhaps the are refering to ts1.mappingname must
be the same as the table name (datagrid1.datamember)

Yes, you're right. If a DataGrid is bound to a datatable, and the name of
the datatable is the same as the MappingName of one of the
DataGridTableStyle used by the DataGrid, this DataGridStyle is applied.
Also, I can't figure out how I pulled off using
system.drawing.color.fromname("info").

FromName is a static method of the Color structure. This statement is to
return a color named 'Info'.
I got to datagrid1.setdatabinding only to find it doesn't exist. How
do I attach the datatable in the dataset to the datagrid in 2005?

Yes, DataGridView is a new WinForms control introduced in .NET 2.0, which
aims to replace and add functionality to the DataGrid control. DataGridView
doesn't has a method called SetDataBinding. To bind a DataGridView to a
data source, you should set the DataSource and DataMember properties of the
DataGridView. For example:

this.dataGridView1.DataSource= this.dataset1;
this.dataGridView1.DataMember ="DataTable1";

For more information on the differences between the DataGridView and
DataGrid controls, you may visit the following documentation:

'Differences Between the Windows Forms DataGridView and DataGrid Controls'
http://msdn2.microsoft.com/en-us/library/ms171628.aspx

Hope this helps.
If you have any question, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

May 14 '07 #2

P: n/a
cj
Thanks Linda

Linda Liu [MSFT] wrote:
Hi Cj,
>but I never quite understood why they said the style name had to be the
same as the table name. Perhaps the are refering to ts1.mappingname must
be the same as the table name (datagrid1.datamember)

Yes, you're right. If a DataGrid is bound to a datatable, and the name of
the datatable is the same as the MappingName of one of the
DataGridTableStyle used by the DataGrid, this DataGridStyle is applied.
>Also, I can't figure out how I pulled off using
system.drawing.color.fromname("info").

FromName is a static method of the Color structure. This statement is to
return a color named 'Info'.
> I got to datagrid1.setdatabinding only to find it doesn't exist. How
do I attach the datatable in the dataset to the datagrid in 2005?

Yes, DataGridView is a new WinForms control introduced in .NET 2.0, which
aims to replace and add functionality to the DataGrid control. DataGridView
doesn't has a method called SetDataBinding. To bind a DataGridView to a
data source, you should set the DataSource and DataMember properties of the
DataGridView. For example:

this.dataGridView1.DataSource= this.dataset1;
this.dataGridView1.DataMember ="DataTable1";

For more information on the differences between the DataGridView and
DataGrid controls, you may visit the following documentation:

'Differences Between the Windows Forms DataGridView and DataGrid Controls'
http://msdn2.microsoft.com/en-us/library/ms171628.aspx

Hope this helps.
If you have any question, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
May 14 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.