views:

15

answers:

2

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.

+1  A: 

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.

Leniel Macaferi
ok so i set a breakpoint in the if(regex.Ismatch(folderName)) statement. it DOES create the new row...however i did notice that on this line: dataGridView1.Rows.Add(grid); there was an index of -1? is that maybe why it's not creating anything? Weirdly, when i put breakpoints on the cells, the values were displaying correctly. It seems the info is coming in correctly however it's just not being written to the cells.
-1 where? Rows?
Leniel Macaferi
-1 in the line dataGridView1.Rows.Add(grid);
would it be more helpful if i included the code that calls this method?
How many rows does your datagridview has? I don't think the code that calls will influence...
Leniel Macaferi
well, i'm creating the rows programmatically so it would really be that # of results = # of rows in the dgv
What I want to know is what is giving you this value of -1? What property?
Leniel Macaferi
ok from the debugger: 'grid.inheritedStyle' threw an exception of type 'System.IO.InvalidOperationException'. i also see {DataGridViewRow{Index =-1}}
So you've got an Exception. The problem appears to be the way you're creating DataGridViewRow grid = new DataGridViewRow() and assigning its values.
Leniel Macaferi
but i've done that before in other classes and it seems to work just fine. how do i fix this exception then?
If you look at this page - http://msdn.microsoft.com/en-us/library/ec7xbehz.aspx you'll see that the exception InvalidOperationException is thrown when: A row that already belongs to the DataGridView was added. -or- A column that has no cell template was added.
Leniel Macaferi
Does your grid have two columns?
Leniel Macaferi
yes -- two columns but rows are programmatically added. saw the link that you provided...but the thing is that this form has a dgv...this is the ONLY instance where i call it in that class. i'm 100% sure the dgv rows are not named the same either!
A: 

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!