private void FieldTypeSelectionChanged(object sender, EventArgs e) { // Take the newly selected field type and update the properties var currentcell = searchDataGrid.CurrentCellAddress; var sendingCB = sender as DataGridViewComboBoxEditingControl; string FieldType = sendingCB.EditingControlFormattedValue.ToString(); DataType dt = null; if (FieldType == "Edm.String") { dt = DataType.String; } else if (FieldType == "Edm.Int32") { dt = DataType.Int32; } else if (FieldType == "Edm.Int64") { dt = DataType.Int64; } else if (FieldType == "Edm.Double") { dt = DataType.Double; } else if (FieldType == "Edm.DateTimeOffset") { dt = DataType.DateTimeOffset; } FieldProperties fp = GetDefaultFieldProperties(dt); DataGridViewComboBoxCell cel = (DataGridViewComboBoxCell)searchDataGrid.Rows[currentcell.Y].Cells[3]; cel.Value = fp.Retreiveable; cel = (DataGridViewComboBoxCell)searchDataGrid.Rows[currentcell.Y].Cells[4]; cel.Value = fp.Filterable; cel = (DataGridViewComboBoxCell)searchDataGrid.Rows[currentcell.Y].Cells[5]; cel.Value = fp.Sortable; cel = (DataGridViewComboBoxCell)searchDataGrid.Rows[currentcell.Y].Cells[6]; cel.Value = fp.Facetable; cel = (DataGridViewComboBoxCell)searchDataGrid.Rows[currentcell.Y].Cells[7]; cel.Value = fp.Searchable; }
private void btnChooseFile_Click(object sender, EventArgs e) { try { List <ColumnMetadata> ColumnMetaDataList = new List <ColumnMetadata>(); openFileDialog1.Filter = "CSV files (*.csv)|*.csv|Text files (*.txt)|*.txt"; DialogResult result = openFileDialog1.ShowDialog(); int rowCount = 0; if (result == DialogResult.OK) { btnImportData.Enabled = false; btnCreateIndex.Enabled = false; SetStatus("Parsing CSV file..."); txtFileName.Text = openFileDialog1.FileName; using (var sr = new StreamReader(txtFileName.Text)) { var csv = new CsvReader(sr); int colCount = 0; bool rowLimit = false; searchDataGrid.Rows.Clear(); searchDataGrid.Refresh(); csvDataGrid.Rows.Clear(); csvDataGrid.Refresh(); csvDataGrid.ColumnCount = 0;; // Get the columnn names and how many columns of data are there while ((csv.Read()) && (rowLimit == false)) { if (rowCount == 0) { bool moreColumns = true; while (moreColumns) { try { csvDataGrid.ColumnCount += 1; string stringField = csv.GetField <string>(colCount); csvDataGrid.Columns[csvDataGrid.ColumnCount - 1].Name = stringField; ColumnMetaDataList.Add(new ColumnMetadata { ColumnName = stringField }); colCount++; } catch (Exception) { // This will be called when there is no more columns moreColumns = false; } } } else { List <string> rowData = new List <string>(); for (int col = 0; col < ColumnMetaDataList.Count; col++) { string stringField = csv.GetField <string>(col); rowData.Add(stringField); DataType dt = EvaluateDataType(stringField); // If it is a string I can stop here: if (dt == DataType.String) { ColumnMetaDataList[col].ColumnDataType = dt; if (stringField != null) { if (ColumnMetaDataList[col].ColumnMaxLen < stringField.Length) { ColumnMetaDataList[col].ColumnMaxLen = stringField.Length; } } } else if (dt == DataType.Int32) { if ((ColumnMetaDataList[col].ColumnDataType != DataType.String) && (ColumnMetaDataList[col].ColumnDataType != DataType.Int64) && (ColumnMetaDataList[col].ColumnDataType != DataType.Double) && (ColumnMetaDataList[col].ColumnDataType != DataType.DateTimeOffset)) { ColumnMetaDataList[col].ColumnDataType = dt; } } else if (dt == DataType.Int64) { if ((ColumnMetaDataList[col].ColumnDataType != DataType.String) && (ColumnMetaDataList[col].ColumnDataType != DataType.Double) && (ColumnMetaDataList[col].ColumnDataType != DataType.DateTimeOffset)) { ColumnMetaDataList[col].ColumnDataType = dt; } } else if (dt == DataType.Double) { if ((ColumnMetaDataList[col].ColumnDataType != DataType.String) && ColumnMetaDataList[col].ColumnDataType != DataType.DateTimeOffset) { ColumnMetaDataList[col].ColumnDataType = dt; } } else if (ColumnMetaDataList[col].ColumnDataType != DataType.String) { ColumnMetaDataList[col].ColumnDataType = dt; } } csvDataGrid.Rows.Add(rowData.ToArray()); } rowCount++; if (rowCount == 100) { rowLimit = true; } } } rowCount = 0; foreach (ColumnMetadata cm in ColumnMetaDataList) { FieldProperties fp = GetDefaultFieldProperties(cm.ColumnDataType, cm.ColumnMaxLen); string isKey = "False"; if (rowCount == 0) { isKey = "True"; } string dt = "Edm.String"; if (cm.ColumnDataType == DataType.Int32) { dt = "Edm.Int32"; } else if (cm.ColumnDataType == DataType.Int64) { dt = "Edm.Int64"; } else if (cm.ColumnDataType == DataType.Double) { dt = "Edm.Double"; } else if (cm.ColumnDataType == DataType.Boolean) { dt = "Edm.Boolean"; } else if (cm.ColumnDataType == DataType.DateTimeOffset) { dt = "Edm.DateTimeOffset"; } string[] row = new string[] { ConvertToCamelCase(cm.ColumnName), dt, isKey, fp.Retreiveable, fp.Filterable, fp.Sortable, fp.Facetable, fp.Searchable }; searchDataGrid.Rows.Add(row); rowCount++; } } } catch (Exception ex) { SetStatus("Error: " + ex.Message); return; } SetStatus("Ready."); btnImportData.Enabled = true; btnCreateIndex.Enabled = true; }
public FieldProperties GetDefaultFieldProperties(DataType FieldType, Int32 MaxFieldLength = 0) { // Figure out the best default properties // Max field len helps to tell if it should be filterable, facetable, etc FieldProperties fp = new FieldProperties(); fp.FieldType = FieldType; fp.Retreiveable = "True"; if (FieldType == DataType.String) { fp.Searchable = "True"; if (MaxFieldLength <= 30) { fp.Filterable = "True"; fp.Sortable = "True"; fp.Facetable = "True"; } else { fp.Filterable = "False"; fp.Sortable = "False"; fp.Facetable = "False"; } } else if (FieldType == DataType.Boolean) { fp.Searchable = "False"; fp.Filterable = "True"; fp.Sortable = "True"; fp.Facetable = "True"; } else if (FieldType == DataType.Int32) { fp.Searchable = "False"; fp.Filterable = "True"; fp.Sortable = "True"; fp.Facetable = "True"; } else if (FieldType == DataType.Int64) { fp.Searchable = "False"; fp.Filterable = "True"; fp.Sortable = "True"; fp.Facetable = "True"; } else if (FieldType == DataType.Double) { fp.Searchable = "False"; fp.Filterable = "True"; fp.Sortable = "True"; fp.Facetable = "True"; } else if (FieldType == DataType.DateTimeOffset) { fp.Searchable = "False"; fp.Filterable = "True"; fp.Sortable = "True"; fp.Facetable = "True"; } else if (FieldType == DataType.GeographyPoint) { fp.Searchable = "False"; fp.Filterable = "True"; fp.Sortable = "False"; fp.Facetable = "True"; } return(fp); }