'
' Creado por SharpDevelop.
' Usuario: german
' Fecha: 08/03/2008
' Hora: 12:20 p.m.
' 
' Para cambiar esta plantilla use Herramientas | Opciones | Codificación | Editar Encabezados Estándar
'
Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Text
Imports System.Windows.Forms

Public Partial Class frmTabla
	Inherits Form
		Private mwShapefile As MapWinGIS.Shapefile
		Private m_DataTable As DataTable
	Public Sub New()
		' The Me.InitializeComponent call is required for Windows Forms designer support.
		Me.InitializeComponent()
		
		'
		' TODO : Add constructor code after InitializeComponents
		'
	End Sub
	
	Sub OpenToolStripMenuItemClick(ByVal sender As Object, ByVal e As EventArgs)
		Dim OFD As New OpenFileDialog()
			OFD.Filter = "Shapefiles: (*.shp)|*.shp"
			If OFD.ShowDialog() <> DialogResult.OK Then
				Return
			End If
			mwShapefile = New MapWinGIS.Shapefile()
			mwShapefile.Open(OFD.FileName, Nothing)
			PopulateTable()
	End Sub
	
	Private Sub PopulateTable()
			If mwShapefile Is Nothing Then
				Return
			End If

			' Variables for the OCX
			Dim myField As MapWinGIS.Field

			' Variables related to the C# data table 
			m_DataTable = New DataTable()
			Dim myDataColumn As DataColumn
			Dim myDataRow As DataRow
			For fld As Integer = 0 To mwShapefile.NumFields - 1

				' Add the columns to the table based on the fields
				myDataColumn = New DataColumn()
				myField = mwShapefile.Field(fld)

				' Use the existing field name in order to give the column a name
				myDataColumn.ColumnName = myField.Name

				' Set up columns with different data types depending on the type specified in the shapefile
				If myField.Type = MapWinGIS.FieldType.DOUBLE_FIELD Then
					myDataColumn.DataType = GetType(Double)
ElseIf myField.Type = MapWinGIS.FieldType.INTEGER_FIELD Then
					myDataColumn.DataType = GetType(Integer)
				Else
					myDataColumn.DataType = GetType(String)
				End If

				' Add each column
				m_DataTable.Columns.Add(myDataColumn)
			Next
			For shp As Integer = 0 To mwShapefile.NumShapes - 1

				' Add the rows to the table based on the cell values
				myDataRow = m_DataTable.NewRow()
				For fld As Integer = 0 To mwShapefile.NumFields - 1

					' Each row has information for all the columns.
					myField = mwShapefile.Field(fld)

					' This just explicitly casts from the object values into the correct
					' field type for the Data Grid
					If myField.Type = MapWinGIS.FieldType.DOUBLE_FIELD Then
						myDataRow(fld) = CDbl(mwShapefile.CellValue(fld, shp))
ElseIf myField.Type = MapWinGIS.FieldType.INTEGER_FIELD Then
						myDataRow(fld) = CInt(mwShapefile.CellValue(fld, shp))
					Else
						myDataRow(fld) = DirectCast(mwShapefile.CellValue(fld, shp), String)

					End If
				Next
				' Once we add the values to a row, add the rows to the table.
				m_DataTable.Rows.Add(myDataRow)
			Next

			' This demonstrates using the table we just created as the data source
			dataGridView1.DataSource = m_DataTable
			msgbox(datagridview1.Rows.count)
		End Sub
End Class
