Example #1
0
        private void RebuildDbf(string path)
        {
            string dbfPath = Path.ChangeExtension(path, ".dbf");

            if (File.Exists(dbfPath))
            {
                File.SetAttributes(dbfPath, FileAttributes.Normal);

                using (GeoDbf geoDbf = new GeoDbf(dbfPath, GeoFileReadWriteMode.ReadWrite))
                {
                    geoDbf.Open();
                    int columnNumber = -1;
                    for (int i = 1; i <= geoDbf.ColumnCount; i++)
                    {
                        string columnName = geoDbf.GetColumnName(i);
                        if (columnName.Equals("RECID", StringComparison.OrdinalIgnoreCase))
                        {
                            columnNumber = i;
                            break;
                        }
                    }

                    if (columnNumber > -1)
                    {
                        for (int i = 1; i <= geoDbf.RecordCount; i++)
                        {
                            geoDbf.WriteField(i, columnNumber, i);
                        }
                    }
                }
            }
        }
        public override DataTable ReadDataToDataGrid(string filePath, string customParameter)
        {
            DataTable PreviewDataTable = new DataTable();

            if (File.Exists(filePath))
            {
                DataTable dataTable = new DataTable();

                using (GeoDbf geoDbf = new GeoDbf(filePath, GeoFileReadWriteMode.Read))
                {
                    geoDbf.Open();

                    dataTable = new DataTable();

                    var columns = geoDbf.GetAllColumns();
                    foreach (var item in columns)
                    {
                        dataTable.Columns.Add(item.ColumnName);
                    }

                    for (int i = 1; i <= geoDbf.RecordCount; i++)
                    {
                        DataRow dr         = dataTable.NewRow();
                        var     dictionary = geoDbf.ReadRecordAsString(i);
                        for (int j = 1; j <= geoDbf.ColumnCount; j++)
                        {
                            string value;
                            dictionary.TryGetValue(geoDbf.GetColumnName(j), out value);
                            dr[j - 1] = value;
                        }
                        dataTable.Rows.Add(dr);
                    }

                    geoDbf.Close();
                }

                PreviewDataTable = dataTable;
            }

            return(PreviewDataTable);
        }