MapWinGIS:SampleCode-C Sharp Net:PopulateDataGridView

This is a back-up of the WIKI.
Not all links might work
We're working on a new wiki.

Main Page | Recent changes | View source | Page history | Log in / create account |

Printable version | Disclaimers | Privacy policy

 using System;
 using System.Collections.Generic;
 using System.ComponentModel;
 using System.Data;
 using System.Drawing;
 using System.Text;
 using System.Windows.Forms;
 
 namespace ChartTest
 {
     // Example written by Shade1974 5/1/2007
     // This example shows how to populate the fields using the cell values from a shapefile.
     // The project assumes that you have added a DataGridView to Form1 called dataGridView1
     // this project requires a reference to MapWinGIS.ocx but not necessarilly the AxMap.
     public partial class Form1 : Form
     {
         MapWinGIS.Shapefile mwShapefile;
         DataTable m_DataTable;
 
         public Form1()
         {
             InitializeComponent();
         }
 
         private void openToolStripMenuItem_Click(object sender, EventArgs e)
         {
             OpenFileDialog OFD = new OpenFileDialog();
             OFD.Filter = "Shapefiles: (*.shp)|*.shp";
             if(OFD.ShowDialog() != DialogResult.OK)return;
             mwShapefile = new MapWinGIS.Shapefile();
             mwShapefile.Open(OFD.FileName, null);
             PopulateTable();
         }
 
 
         /// <summary>
         /// This subroutine reads the information from the dbf table of a shapefile and adds the information to the DataGridView
         /// No special formatting is specified in the code, only the field names, types and values.
         /// </summary>
         private void PopulateTable()
         {
             if(mwShapefile == null)return;
 
             // Variables for the OCX
             MapWinGIS.Field myField;
 
             // Variables related to the C# data table 
             m_DataTable = new DataTable();
             DataColumn myDataColumn;
             DataRow myDataRow;
 
             // Add the columns to the table based on the fields
             for (int fld = 0; fld < mwShapefile.NumFields; fld++)
             {
                 myDataColumn = new DataColumn();
                 myField = mwShapefile.get_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)
                 {
                     myDataColumn.DataType = typeof(double);
                 }
                 else if (myField.Type == MapWinGIS.FieldType.INTEGER_FIELD)
                 {
                     myDataColumn.DataType = typeof(int);
                 }
                 else
                 {
                     myDataColumn.DataType = typeof(string);
                 }
 
                 // Add each column
                 m_DataTable.Columns.Add(myDataColumn);
             }
 
             // Add the rows to the table based on the cell values
             for (int shp = 0; shp < mwShapefile.NumShapes; shp++)
             {
                 myDataRow = m_DataTable.NewRow();
 
                 // Each row has information for all the columns.
                 for (int fld = 0; fld < mwShapefile.NumFields; fld++)
                 {
                      myField = mwShapefile.get_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)
                      {
                          myDataRow[fld] = DBNull.Value == mwShapefile.get_CellValue(fld, shp) ? 0 : (double)mwShapefile.get_CellValue(fld, shp);
                      }
                      else if (myField.Type == MapWinGIS.FieldType.INTEGER_FIELD)
                      {
                          myDataRow[fld] = DBNull.Value == mwShapefile.get_CellValue(fld, shp) ? 0 : (int)mwShapefile.get_CellValue(fld, shp);
                      }
                      else
                      {
                          myDataRow[fld] = (string)mwShapefile.get_CellValue(fld, shp);
                      }
                      
                 }
                 // Once we add the values to a row, add the rows to the table.
                 m_DataTable.Rows.Add(myDataRow);
             }
 
             // This demonstrates using the table we just created as the data source
             dataGridView1.DataSource = m_DataTable;
 
         }
     }
 }

Posted 5/1/2007 by Shade1974
Updated 4/2/2008 by earljon

Retrieved from "http://mapwindow.org/wiki/index.php/MapWinGIS:SampleCode-C_Sharp_Net:PopulateDataGridView"

This page has been accessed 3,955 times. This page was last modified on 2 April 2008, at 08:47.