0

I want to get user's input on how many rows he/she wants and then add that number of rows to the datagridview, while the datagridview is being populated with it's data. This way an ecxeption occurs,System.InvalidOperationException. Is there a work around for this?

        int numberOfRows = Int32.Parse(txt_numberofrows.Text.Trim());
        for (var i = 1; i < numberOfRows; i++)
        {
                expenseList.Rows.Add(numberOfRows);
        }
  • There error message describes the issue. If you really needed to do this then I suppose you could add a second unbound DataGridView and pass the data from this to the original data source. – Andy G Dec 07 '18 at 13:23
  • Working on the principle of least surprise, it would be better to just let the user create as many rows as they need (or check after rows are added if you needed to limit them for some reason). – Andy G Dec 07 '18 at 13:26
  • I suggest you review the command: `DataGridView.RowCount` such that the line `dadosDataGridView.RowCount = numberOfRows;` might work as you describe, however, bear in mind that if the grid already has `numberOfRows` rows… it will not add “new” rows, therefore, you may have to ADD the current number of rows in the grid to the new `numberOfRows` wanted… just a thought. Unfortunately, the error could be coming from a couple of different places and it is difficult to tell where the error is thrown, usually it is best to supply that info in your question. Where is the error thrown? – JohnG Dec 08 '18 at 23:40

0 Answers0