Hi Aussie,
If you're using VS2005, I suggest you use DataGridView control in your
project to get what you want. DataGridView is newly introduced in .NET 2.0
and is easy to extend.
Cell Calculation.. Like excel does (=sum(cell1 + cell2)) and other basic
calculations
You could handle the DataGridView's CellFormatting and CellValueChanged
events to do this. The following is a sample, in which the DataGridView has
3 rows and 3 columns and the value in the 3rd column has is the sum of the
2nd and first columns.
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Me.DataGridView1.RowCount = 3
Me.DataGridView1.ColumnCount = 3
End Sub
Private Sub DataGridView1_CellFormatting(ByVal sender As Object, ByVal
e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles
DataGridView1.CellFormatting
If (e.RowIndex >= 0 And e.ColumnIndex = 2) Then
Dim total = 0, value = 0
Dim result = False
For i As Integer = 0 To Me.DataGridView1.ColumnCount - 2
If Not (Me.DataGridView1.Rows(e.RowIndex).Cells(i).Value Is
Nothing) Then
result =
Integer.TryParse(Me.DataGridView1.Rows(e.RowIndex).Cells(i).Value.ToString()
, value)
If (result) Then
total += value
End If
End If
Next
Me.DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value =
total
End If
End Sub
Private Sub DataGridView1_CellValueChanged(ByVal sender As Object,
ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles
DataGridView1.CellValueChanged
If (e.ColumnIndex > -1 And e.ColumnIndex <> 2 And e.RowIndex > -1)
Then
Me.DataGridView1.InvalidateCell(2, e.RowIndex)
End If
End Sub
End Class
Total Rows - Self calculating total rows (col and row locations). This
would work for when the grid is bound or unbound
You could get the total rows count via the Rows.Count or RowCount property
of the DataGridView.
I find a sample which exports the data in a DataGrid to Excel. I think this
method also applies to DataGridView. Below is the link of the sample:
http://www.codeproject.com/office/datagridexcelexport.asp
Save/load from a access or SQL database.
This is a basic function of a DataGridView. Usually we use a DataAdapter or
TableAdapter to retrieve the data from the database and fill the returned
data in a DataTable. Then we could set the DataSource property of the
DataGirdView to the DataTable, so that the data will be shown in the
DataGridView.
For more information on DataGridView control in .NET 2.0, you may refer to
the following MSDN documents:
'DataGridView Control Overview (Windows Forms)'
http://msdn2.microsoft.com/en-us/library/k39d6s23.aspx
'DataGridView Control (Windows Forms)'
http://msdn2.microsoft.com/en-us/library/e0ywh3cz.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/subscriptions/managednewsgroups/default.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/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.