Editing Geometry Feature Set in DataGridView

Topics: Data Access, SharpMap v0.9 / v1.x, WinForms Controls
Oct 24, 2012 at 8:43 PM

I have an application that allows various geometry components to be drawn using Sharpmap v0.9.  They are stored in various layers with a GeometryProvider as the data source for each layer.  I would like to be able at times to edit precisely the coordinates of the geometries (i.e. type in the coordinates of each corner of a polygon).  Is there a way to bind a GeometryProvider to a DataGridView or similar control to allow its contents to be edited? Would I be better off using a PostGIS provider so that I can provide direct access to the tables values to the user?

 

Thanks,

 

David

Developer
Nov 6, 2012 at 7:06 AM

Hi Hododav,

- You can bind a GeometryProvider ( or any datasource ) to a datagridview .

EX:

 

FeatureDataSet ds;
_layer.DataSource.ExecuteIntersectionQuery(_layer.Envelope,ds);
dataGridView1.DataSource=ds.Table[0];
Datatable _table=ds.Table[0];

 

 


 
//Add update function to postgis datasouce:
public void UpdateFeature(FeatureDataRow row)
        {
            string strUpdate = "";
            string rowID = row[this.ObjectIdColumn].ToString();
            for (int i = 0; i < row.Table.Columns.Count; i++)
            {
                if (row.Table.Columns[i].ColumnName != this.ObjectIdColumn)
                {
                    strUpdate += "\"" + row.Table.Columns[i].ColumnName + "\"=";
                    if (row.Table.Columns[i].ColumnName != this.GeometryColumn)
                    {
                        if (row.Table.Columns[i].DataType.Name.Equals("String"))
                        {
                            strUpdate += "'" + row[i] + "',";
                        }
                        else
                        {
                            if (string.IsNullOrEmpty(row[i].ToString()))
                                row[i] = 0;
                            strUpdate += row[i] + ",";
                        }
                    }                   
                }
            }
            if (row.Geometry != null)
            {
                strUpdate += "\"" + this.GeometryColumn + "\"=";
                strUpdate += _prefixFunction + "GeometryFromText('" +
                row.Geometry.AsText() + "'," + this.SRID + ") ";
            }
            else
            {
                strUpdate = strUpdate.Remove(strUpdate.Length - 1);
            }            
            string sql = "update \"" + this.Table + "\" set " + strUpdate+
               " where " + this.ObjectIdColumn + "=" + rowID;
           // ExcuteNonQuery(sql);    
        }
- Update from datagridview to datasoure

DataTable tbChanged = _table.GetChanges(DataRowState.Modified);
if (tbChanged!=null && tbChanged.Rows.Count > 0)
{
	_layer.DataSource.Open();	                
	for (int i = 0; i < tbChanged.Rows.Count; i++ )
	{		
		FeatureDataRow fR = tbChanged.Rows[i] as FeatureDataRow;                    							
		_layer.DataSource.UpdateFeature(fR); // use (_layer.Datasource as PostGis).UpdateFeature(fR) if this function has not added to IProvider
	}
	_layer.DataSource.Close();                  
   
}