473,385 Members | 1,356 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

2003 Datagrid and beyond

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
2 1755
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: PeterZ | last post by:
Hi, In a running C# app with a datagrid control I select all rows in the dataGrid using CTRL-A, I then paste into some other app like notepad or Word but the column headings get left off. Is...
1
by: Michael Gorbach | last post by:
Iv got a StatisticsContainer object that contains an arraylist of objects of different types, all inherited from class Statistic. The statistics class has 2 string public properties, name and...
15
by: Rik Brooks | last post by:
I find it amazing that I am quickly able to find answers to obscure questions but simple ones elude me. I have a datagrid that I've bound, no problem at all. Now I want to - programatically -...
8
by: Inigo Jimenez | last post by:
I have an ASP .net web application installed in a Windows 2003 server. This web application has a webform that has a Datagrid. This Datagrid is filled with the data of a SQL table. I have a...
0
by: optimizeit | last post by:
What I am attempting to do is import an Excel Workbook and display the worksheets in a datagrid dynamically. I am very close to getting this to work. I have to this point successfully imported a...
2
by: charliewest | last post by:
I am dynamically creating my datagrid, building each column in real-time via code-behind (using c#). The only way i have read to add ImageButtons to my grid dynamically is by creating a separate...
3
by: Richard | last post by:
In ASP.NET 1.1, I have SmartNavigation turned on so that the user's focus will remain on the line in the datagrid they choose to edit. When the user chooses to add a new line in the datagrid,...
3
by: OhWhite | last post by:
In an off-line Vb.net 2003 application, is there any reason why the following would happen. 1) Minimize button doesn't function. 2) Button and DataGrid widen beyond Design time settings. ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.