33using System . Text ;
44using System . Windows . Forms ;
55using Microsoft . VisualBasic . FileIO ;
6- using System . Collections . Generic ;
76
87namespace 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 \n Column: '{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 ,
0 commit comments