Tutorial :Output not being written to the DGV in my code


I'm not exactly sure what's going on here--I tried to debug but couldn't really come up with any explanation as to why nothing is being written to my datagridview.

Anybody have any idea?

public partial class CleanPathResults : Form  {      public CleanPathResults()      {          InitializeComponent();      }        public void RenameFolder(string folderName)      {          string regPattern = (@"[~#&$!%+{}]+");          string replacement = "";          List<string> normal = new List<string>();          Regex regExPattern = new Regex(regPattern);          dataGridView1.Rows.Clear();          List<string> cleanDirNames = new List<string>();            try          {              if (regExPattern.IsMatch(folderName))              {                  string cleanup = regExPattern.Replace(folderName, replacement);                  System.IO.Directory.Move(folderName, cleanup);                  DataGridViewRow grid = new DataGridViewRow();                  grid.CreateCells(dataGridView1);                  grid.Cells[0].Value = folderName;                  grid.Cells[1].Value = cleanup;                  dataGridView1.Rows.Add(grid);                  folderName = cleanup;                  cleanDirNames.Add(cleanup);                }              else              {                  normal.Add(folderName);              }              }          catch(Exception e)          {                throw;            }            DirectoryInfo di = new DirectoryInfo(folderName);          DirectoryInfo[] diArr = di.GetDirectories();              List<string> subdirectories = new List<string>();          try          {              foreach (DirectoryInfo subdir in diArr)              {                  subdirectories.Add(subdir.ToString());              }          }          catch(Exception e)          {              throw;          }            try          {              foreach (string folder in subdirectories)              {                  string sF = folder;                    RenameFolder(folderName + "\\" + sF);              }          }          catch(Exception e)          {              throw;          }      }        private void button1_Click_1(object sender, EventArgs e)      {          Application.Exit();      }      }  

I'm not hitting any errors--the app does what it's supposed to do (in this case, make sure folder names do not contain the invalid chars defined in the regex)...however it's just an issue of output not displaying on the dgv.

Any help would be appreciated.


Probably there's no match for your regex... this way no row is being created and added to dataGridView1.

Have you debugged the code? Try to insert a breakpoint within the if statement right after regExPattern.IsMatch. See if the debugger stops there. This way you can assert that a new row is being created.

I'll try to help you more if that holds true.


actually nevermind. figured out that b/c my method keeps calling itself, the datagridview1.Rows.Clear() would clear out everything, everytime the method called itself. Hence no output. Thanks for all your help though Leniel!

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »