Display data table to datagridview

Topics: Data Access, SharpMap v2.0, WinForms Controls
Jul 2, 2013 at 3:26 PM
Edited Jul 2, 2013 at 3:28 PM
Hello,


I have completed the data import from PostGIS using connection string, see codes. Now I want to display the data table that I needed into a datagridview. I am a little bit confusion here as I cannot find a direct example related to this.
            InitializeComponent();

            VectorLayer vlay = new VectorLayer("roadnodes");

            vlay.DataSource = new SharpMap.Data.Providers.PostGIS(Properties.Settings.Default.PostGisConnectionString, "roadnotes", "gid");
            
            FeatureDataTable data = new FeatureDataTable();

            dataGridView1.DataSource = data;

            (some codes here?)


            mapBox1.Map.Layers.Add(vlay);
            mapBox1.Map.ZoomToExtents();
            mapBox1.ActiveTool = SharpMap.Forms.MapBox.Tools.Pan;
            mapBox1.Refresh();
what I want to know is how to connect the Datasource of the layer to the datagridview? any method to do it?

Thanks you!
Coordinator
Jul 3, 2013 at 6:02 AM
/*
 * Other code
 */

var vlay = new SharpMap.Layers.VectorLayer("roadnodes"); 
using (var p = new SharpMap.Data.Providers.PostGis(Properties.Settings.Default.PostGisConnectionString, "roadnodes", "gid"))
{
    var fds = new SharpMap.Data.FeatureDataSet();
    p.ExecuteIntersectionQuery(p.GetExtents(), fds);
    dataGridView1.DataSource = fds.Tables[0];
    vlay.DataSource = new SharpMap.Data.Providers.GeometryFeatureProvider(fds.Tables[0]);
}
/*
 * Other code
 */
Jul 3, 2013 at 9:34 AM
Hello FObermaier,

Thanks for the code! It does work. I post the codes here if anybody have such needs.
        public Form1()
        {
            InitializeComponent();

            VectorLayer vlay = new VectorLayer("roadnodes");
            
            using (
                var p = new SharpMap.Data.Providers.PostGIS(Properties.Settings.Default.PostGisConnectionString,
                                                            "roadnotes", "gid"))
            {
                var fds = new SharpMap.Data.FeatureDataSet();
                p.ExecuteIntersectionQuery(p.GetExtents(),fds);
                dataGridView1.DataSource = fds.Tables[0];
                vlay.DataSource = new SharpMap.Data.Providers.GeometryFeatureProvider(fds.Tables[0]);

                // display geospatia features on the map control
                mapBox1.Map.Layers.Add(vlay);
                mapBox1.Map.ZoomToExtents();
                mapBox1.ActiveTool = SharpMap.Forms.MapBox.Tools.Pan;
                mapBox1.Refresh();
            }
        }