/// <summary> /// Constructs a new data grid row with a new key-value collection /// </summary> /// <param name="grid">The data grid reference</param> /// <param name="values">The values to add to the row</param> protected internal DataGridRow ( IDataGrid grid, params KeyValuePair <string, object>[] values ) { if (grid == null) { throw new ArgumentNullException("grid"); } if (values == null) { throw new ArgumentNullException("values"); } if (values.Length == 0) { throw new ArgumentException ( "At least one value is required to create a data grid row." ); } this.Grid = grid; _columnValues = new Dictionary <string, object>(); // Add the column values to the row but also pad the row with blanks where values are missing foreach (var column in grid.GetColumnNames()) { if (values.Any(m => m.Key == column)) { var matchingItem = values.First ( m => m.Key.ToLower() == column.ToLower() ); _columnValues.Add ( column, matchingItem.Value ); } else { _columnValues.Add ( column, null ); } } }
/// <summary> /// Converts the data grid specified to a CSV new data string /// </summary> /// <param name="grid">The data grid binder to convert</param> /// <returns>A string that represents the data grid in CSV format</returns> public string Serialize ( IDataGrid grid ) { if (grid == null || grid.Count() == 0) { return(String.Empty); } var configuration = new CsvConfiguration() { Delimiter = ",", HasHeaderRecord = true, Quote = '"' }; using (var stream = new MemoryStream()) using (var writer = new StreamWriter(stream)) using (var csv = new CsvWriter(writer, configuration)) { // Create the CSV headings grid.GetColumnNames().ToList().ForEach ( m => csv.WriteField(m) ); csv.NextRecord(); // Create a CSV record for every row in the data grid foreach (var row in grid) { foreach (var cell in row) { // Get a string representation of the cells value var value = ( cell.Value == null ? String.Empty : cell.Value.ToString() ); csv.WriteField(value); } csv.NextRecord(); } // Reset the memory stream and writers writer.Flush(); stream.Position = 0; return(new StreamReader(stream).ReadToEnd()); } }
/// <summary> /// Serializes a data grid to a JSON document /// </summary> /// <param name="grid">The data grid</param> /// <returns>A JSON string containing the grids data</returns> public string Serialize ( IDataGrid grid ) { if (grid == null || grid.Count() == 0) { return(String.Empty); } var jsonBuilder = new StringBuilder(); var rowCount = grid.Count(); var columnCount = grid.GetColumnNames().Length; var rowNumber = 1; jsonBuilder.Append("{\n"); jsonBuilder.Append ( "\t\"{0}\": [\n".With ( grid.Name ) ); foreach (var row in grid) { var columnNumber = 1; jsonBuilder.Append("\t{\n"); foreach (var cell in row) { // Get a string representation of the cells value var value = ( cell.Value == null ? String.Empty : cell.Value.ToString() ); // Create a JSON property template var template = ( (columnNumber < columnCount) ? "\t\t\"{0}\": \"{1}\",\n" : "\t\t\"{0}\": \"{1}\"\n" ); // Add the column name and value to the JSON items properties jsonBuilder.Append ( template.With(cell.Key, value) ); columnNumber++; } jsonBuilder.Append ( (rowNumber < rowCount) ? "\t},\n" : "\t}\n" ); rowNumber++; } jsonBuilder.Append("]}"); return(jsonBuilder.ToString()); }