Skip to content

Commit

Permalink
Merge pull request #10 from solarwinds/datetime-null-formatting
Browse files Browse the repository at this point in the history
Formatting DateTime and NULL value changes
  • Loading branch information
tdanner committed Oct 19, 2015
2 parents 8418e15 + cd390fb commit 9a8f231
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 12 deletions.
15 changes: 9 additions & 6 deletions Src/SwqlStudio/DataGridExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,16 @@ public void ExportAsCsv(DataGridView grid, string outputFileName)
isFirst = false;

object item = cell.Value;
if (item is DateTime)
if (item != null && !DBNull.Value.Equals(item))
{
csvStream.Write(EscapeCsvString(Convert.ToDateTime(item).ToString("O").Trim()));
}
else
{
csvStream.Write(EscapeCsvString(cell.FormattedValue.ToString().Trim()));
if (item is DateTime)
{
csvStream.Write(EscapeCsvString(Convert.ToDateTime(item).ToString("O").Trim()));
}
else
{
csvStream.Write(EscapeCsvString(cell.FormattedValue.ToString().Trim()));
}
}
}
csvStream.WriteLine();
Expand Down
33 changes: 27 additions & 6 deletions Src/SwqlStudio/QueryTab.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
Expand All @@ -28,9 +29,12 @@ enum Tabs
ErrorMessages = 32
}

private Font nullFont;

public QueryTab()
{
InitializeComponent();
nullFont = new Font(dataGridView1.DefaultCellStyle.Font, dataGridView1.DefaultCellStyle.Font.Style | FontStyle.Italic);
ShowTabs(Tabs.Results);
Disposed += QueryTabDisposed;
}
Expand All @@ -42,6 +46,11 @@ void QueryTabDisposed(object sender, EventArgs e)
subscription.Dispose();
subscription = null;
}
if (nullFont != null)
{
nullFont.Dispose();
nullFont = null;
}
}

private IApplicationService applicationService;
Expand Down Expand Up @@ -109,7 +118,13 @@ public ConnectionInfo ConnectionInfo

private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (e.Value != null)
if (e.Value == null || DBNull.Value.Equals(e.Value))
{
e.Value = "NULL";
e.CellStyle.ForeColor = SystemColors.GrayText;
e.CellStyle.Font = nullFont;
}
else
{
StringBuilder str = FormatGridValue(e.Value);
e.Value = str.ToString();
Expand All @@ -122,7 +137,9 @@ private static StringBuilder FormatGridValue(object value)
if (value == null)
return str;

if (value.GetType().IsArray)
var valueType = value.GetType();

if (valueType.IsArray)
{
var enumerable = (IEnumerable)value;
var enumerator = enumerable.GetEnumerator();
Expand All @@ -135,15 +152,19 @@ private static StringBuilder FormatGridValue(object value)
if (!first)
str.Append(", ");
if (enumerator.Current != null)
str.Append(enumerator.Current.ToString());
str.Append(enumerator.Current);

first = false;
}
str.Append("]");
}
else if (valueType == typeof(DateTime))
{
str.AppendFormat("{0:yyyy'-'MM'-'dd HH':'mm':'ss'.'FFFFFF}", value);
}
else
{
str.Append(value.ToString());
str.Append(value);
}

return str;
Expand Down Expand Up @@ -337,8 +358,8 @@ private void queryWorker_RunWorkerCompleted(object sender, System.ComponentModel
grid.Columns.Clear();
foreach (DataColumn column in table.Columns)
{
grid.Columns.Add(column.ColumnName, column.ColumnName);
grid.Columns[column.ColumnName].DataPropertyName = column.ColumnName;
var columnIndex = grid.Columns.Add(column.ColumnName, column.ColumnName);
grid.Columns[columnIndex].DataPropertyName = column.ColumnName;
}

grid.DataSource = arg.Results;
Expand Down

0 comments on commit 9a8f231

Please sign in to comment.