Skip to content

Commit c16d360

Browse files
asmeyasmey
authored andcommitted
Added comments; Improved some error handling
1 parent 59ee9d5 commit c16d360

6 files changed

Lines changed: 142 additions & 41 deletions

File tree

FindMissingRows/Extensions.cs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
using System.Data;
2+
using System.Text;
3+
using System.Windows.Forms;
4+
5+
public static class Extensions
6+
{
7+
/// <summary>
8+
/// Extension to the DataTable to add a "ToCSV" method.
9+
/// </summary>
10+
/// <param name="table"></param>
11+
/// <returns>the table as a CSV string</returns>
12+
public static string ToCSV(this DataTable table)
13+
{
14+
// reference: adapted from http://stackoverflow.com/questions/888181/convert-datatable-to-csv-stream
15+
var result = new StringBuilder();
16+
for (int i = 0; i < table.Columns.Count; i++)
17+
{
18+
result.AppendFormat("\"{0}\"",table.Columns[i].ColumnName);
19+
result.Append(i == table.Columns.Count - 1 ? "\n" : ",");
20+
}
21+
22+
foreach (DataRow row in table.Rows)
23+
{
24+
for (int i = 0; i < table.Columns.Count; i++)
25+
{
26+
result.AppendFormat("\"{0}\"", row[i].ToString());
27+
result.Append(i == table.Columns.Count - 1 ? "\n" : ",");
28+
}
29+
}
30+
31+
return result.ToString();
32+
}
33+
34+
/// <summary>
35+
/// Extension to the DataGrivView to add a "ToCSV" method.
36+
/// </summary>
37+
/// <param name="table"></param>
38+
/// <returns>the table as a CSV string</returns>
39+
public static string ToCSV(this DataGridView table)
40+
{
41+
// reference: adapted from http://stackoverflow.com/questions/888181/convert-datatable-to-csv-stream
42+
var result = new StringBuilder();
43+
for (int i = 0; i < table.Columns.Count; i++)
44+
{
45+
result.AppendFormat("\"{0}\"", table.Columns[i].Name);
46+
result.Append(i == table.Columns.Count - 1 ? "\n" : ",");
47+
}
48+
49+
foreach (DataGridViewRow row in table.Rows)
50+
{
51+
for (int i = 0; i < row.Cells.Count; i++)
52+
{
53+
result.AppendFormat("\"{0}\"", row.Cells[i].FormattedValue);
54+
result.Append(i == row.Cells.Count - 1 ? "\n" : ",");
55+
}
56+
}
57+
58+
return result.ToString();
59+
}
60+
}

FindMissingRows/FilterDialog.cs

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,4 @@
11
using System;
2-
using System.Collections.Generic;
3-
using System.ComponentModel;
4-
using System.Data;
5-
using System.Drawing;
6-
using System.Linq;
7-
using System.Text;
8-
using System.Threading.Tasks;
92
using System.Windows.Forms;
103

114
namespace FindMissingRows
@@ -21,20 +14,34 @@ public FilterDialog(object [] columnNames)
2114

2215
filterColumn.Items.AddRange(columnNames);
2316

17+
// List of filter types to support
2418
filterTypeCb.Items.AddRange(new string [] {"Equals", "Begins", "Ends" });
19+
20+
// setup default filter type
2521
filterTypeCb.SelectedIndex = filterTypeCb.Items.IndexOf("Equals");
2622
}
2723

24+
/// <summary>
25+
/// Apply the filter
26+
/// </summary>
27+
/// <param name="sender"></param>
28+
/// <param name="e"></param>
2829
private void apply_Click(object sender, EventArgs e)
2930
{
3031
MainForm.Filter(FilterString);
3132
}
3233

34+
// close the filter form
3335
private void cancel_Click(object sender, EventArgs e)
3436
{
3537
Close();
3638
}
3739

40+
/// <summary>
41+
/// Handle changes in the selected filter type
42+
/// </summary>
43+
/// <param name="sender"></param>
44+
/// <param name="e"></param>
3845
private void filterColumn_SelectedIndexChanged(object sender, EventArgs e)
3946
{
4047
if (filterColumn.SelectedItem != null)
@@ -44,6 +51,9 @@ private void filterColumn_SelectedIndexChanged(object sender, EventArgs e)
4451
UpdateFilter();
4552
}
4653

54+
/// <summary>
55+
/// Create the filter and apply it.
56+
/// </summary>
4757
private void UpdateFilter()
4858
{
4959
if (string.IsNullOrWhiteSpace(columnNameBox.Text) && string.IsNullOrWhiteSpace(filterTextBox.Text))
@@ -74,11 +84,21 @@ private void UpdateFilter()
7484
filterExpressionBox.Text = FilterString;
7585
}
7686

87+
/// <summary>
88+
/// handle manual changes to the filter
89+
/// </summary>
90+
/// <param name="sender"></param>
91+
/// <param name="e"></param>
7792
private void filterText_TextChanged(object sender, EventArgs e)
7893
{
7994
UpdateFilter();
8095
}
8196

97+
/// <summary>
98+
/// Handle the Clear button by setting no filter.
99+
/// </summary>
100+
/// <param name="sender"></param>
101+
/// <param name="e"></param>
82102
private void clearButton_Click(object sender, EventArgs e)
83103
{
84104
filterColumn.SelectedItem = null;
@@ -87,6 +107,11 @@ private void clearButton_Click(object sender, EventArgs e)
87107
MainForm.Filter(FilterString);
88108
}
89109

110+
/// <summary>
111+
/// Handle a selection change of the filter type
112+
/// </summary>
113+
/// <param name="sender"></param>
114+
/// <param name="e"></param>
90115
private void filterTypeCb_SelectedIndexChanged(object sender, EventArgs e)
91116
{
92117
UpdateFilter();

FindMissingRows/FindMissingRows.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
<Reference Include="System.Xml" />
5151
</ItemGroup>
5252
<ItemGroup>
53+
<Compile Include="Extensions.cs" />
5354
<Compile Include="FilterDialog.cs">
5455
<SubType>Form</SubType>
5556
</Compile>

FindMissingRows/Form1.Designer.cs

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

FindMissingRows/Form1.cs

Lines changed: 41 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
using System.Text;
44
using System.Windows.Forms;
55
using Microsoft.VisualBasic.FileIO;
6-
using System.Collections.Generic;
76

87
namespace FindMissingRows
98
{
@@ -35,6 +34,11 @@ public Form1()
3534
filterButton.Enabled = false;
3635
}
3736

37+
/// <summary>
38+
/// Select the file to use for the member list
39+
/// </summary>
40+
/// <param name="sender"></param>
41+
/// <param name="e"></param>
3842
private void SelectMemberList_Click(object sender, EventArgs e)
3943
{
4044
openFileDialog1.Filter = "CSV files (*.csv)|*.csv";
@@ -48,6 +52,11 @@ private void SelectMemberList_Click(object sender, EventArgs e)
4852
m_memberListInit = true;
4953
}
5054

55+
/// <summary>
56+
/// Select the file to use for the compare list
57+
/// </summary>
58+
/// <param name="sender"></param>
59+
/// <param name="e"></param>
5160
private void SelectListToCompare_Click(object sender, EventArgs e)
5261
{
5362
openFileDialog1.Filter = "CSV files (*.csv)|*.csv";
@@ -61,7 +70,12 @@ private void SelectListToCompare_Click(object sender, EventArgs e)
6170
m_compareListInit = true;
6271
}
6372

64-
73+
/// <summary>
74+
/// Read in a CSV file into data table and load combo box with column names from the new file
75+
/// </summary>
76+
/// <param name="csv_file_path">name of file to read in</param>
77+
/// <param name="tableName">Title to give table</param>
78+
/// <param name="comboBox">comboBox that will be reloaded with column names from file</param>
6579
private void FillDataTableFromCSVFile(string csv_file_path, string tableName, ref ComboBox comboBox)
6680
{
6781
// always start with a new table so that know the schema is correct
@@ -110,6 +124,11 @@ private void FillDataTableFromCSVFile(string csv_file_path, string tableName, re
110124
return;
111125
}
112126

127+
/// <summary>
128+
/// Compare the two files and show the missing rows
129+
/// </summary>
130+
/// <param name="sender"></param>
131+
/// <param name="e"></param>
113132
private void findMissingItems_Click(object sender, EventArgs e)
114133
{
115134
if (!m_compareListInit)
@@ -159,8 +178,19 @@ private void findMissingItems_Click(object sender, EventArgs e)
159178
// add index to compare list table
160179
DataColumn[] keys = new DataColumn[1];
161180
keys[0] = dtCompare.Columns[compareColName];
162-
dtCompare.PrimaryKey = keys;
181+
try
182+
{
183+
dtCompare.PrimaryKey = keys;
163184

185+
}
186+
catch (Exception ex)
187+
{
188+
StringBuilder builder = new StringBuilder();
189+
builder.AppendFormat("The file: {0}\n\nColumn: '{1}', must have unique values.\n\n", openFileDialog1.FileName, compareColName);
190+
builder.AppendFormat("Error: {0}", ex.Message);
191+
MessageBox.Show(builder.ToString());
192+
return;
193+
}
164194
// find missing items in compare
165195
foreach (DataRow row in dtMembers.Rows)
166196
{
@@ -194,6 +224,11 @@ private void exit_Click(object sender, EventArgs e)
194224
Close();
195225
}
196226

227+
/// <summary>
228+
/// Save missing rows to a CSV file
229+
/// </summary>
230+
/// <param name="sender"></param>
231+
/// <param name="e"></param>
197232
private void save_Click(object sender, EventArgs e)
198233
{
199234
SaveFileDialog saveDlg = new SaveFileDialog();
@@ -203,30 +238,10 @@ private void save_Click(object sender, EventArgs e)
203238
if (res != DialogResult.OK)
204239
return;
205240

206-
StringBuilder output = new StringBuilder();
207-
208-
// output column headers row
209-
int cnt = 1;
210-
foreach (DataGridViewColumn col in dataGridView1.Columns)
211-
{
212-
output.AppendFormat("\"{0}\"", col.Name);
213-
if (cnt++ < m_missingTable.Columns.Count)
214-
output.AppendFormat(",");
215-
}
216-
output.AppendLine();
217-
218-
cnt = 1;
219-
foreach (DataGridViewRow row in dataGridView1.Rows)
241+
using (System.IO.StreamWriter file = new System.IO.StreamWriter(saveDlg.FileName))
220242
{
221-
for (int i = 0; i < row.Cells.Count; i++)
222-
{
223-
output.AppendFormat("\"{0}\"", row.Cells[i].FormattedValue);
224-
if (i < row.Cells.Count - 1)
225-
output.AppendFormat(",");
226-
}
227-
output.AppendLine();
243+
file.Write(dataGridView1.ToCSV());
228244
}
229-
System.IO.File.WriteAllText(saveDlg.FileName, output.ToString());
230245
}
231246

232247
private void filterButton_Click(object sender, EventArgs e)
@@ -263,7 +278,7 @@ public void Filter(string filter)
263278
}
264279
catch (Exception ex)
265280
{
266-
MessageBox.Show("Bad filter: " + ex.ToString());
281+
MessageBox.Show("Bad filter: " + ex.Message);
267282
}
268283
resultSummary.Text = string.Format("{0} members missing out of {1}, {2} rows visible with filter",
269284
m_missingTable.Rows.Count,

FindMissingRows/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,5 @@
3232
// You can specify all the values or you can default the Build and Revision Numbers
3333
// by using the '*' as shown below:
3434
// [assembly: AssemblyVersion("1.0.*")]
35-
[assembly: AssemblyVersion("1.0.0.0")]
36-
[assembly: AssemblyFileVersion("1.0.0.0")]
35+
[assembly: AssemblyVersion("1.0.*")]
36+
//[assembly: AssemblyFileVersion("1.0.0.*")]

0 commit comments

Comments
 (0)