1

I have a DataGridView that im trying to empty and then put data back into but no matter what method I try to fill the dgv I only get empty rows.

I have this code but no matter what I try, dt.Rows.Add(sortestStus[row]) always results in an empty row being added.

sortedStus = Split_List(stuRows);

DataTable dt = (DataTable)dgvClassStuInfo.DataSource;
dgvClassStuInfo.DataSource = dt;
dt.Clear();

for (int row = 0; row < sortedStus.Count; row++)
{
    dt.Rows.Add(sortedStus[row]);
}

sortedStus is a List<DataRow> filled by a merge sort - this works as intended and sortedStus is filled with rows that have their intended data. dt.Clear(); is working as intended and removing all rows but its the for loop that is causing issues.

I've tried dt.NewRow(); and adding each item individually but it still didn't work and I've also tried refreshing both the dgv and the datatable and that also didn't work. Anyone have any ideas?

ETA: sorry if this is a duplicate question but I can't seem to find any with this problem, only trying to actually add the empty rows.

ETA2: trying dt.Rows.Clear(); as Anonymous suggested has the same behaviour

budywudy9
  • 29
  • 7
  • Don't you want `dt.rows.Clear();` instead of `dt.Clear();` ? Because you are clearing the table structure in addition to the data. – Kate Oct 30 '21 at 19:21
  • Just tried that and it has the same behaviour but thanks for the suggestion! – budywudy9 Oct 30 '21 at 19:28
  • I would suggest adding a BindingSource, so that changes to the datatable are reflected immediately in the DGV. [Here](https://learn.microsoft.com/en-us/dotnet/desktop/winforms/controls/how-to-bind-data-to-the-windows-forms-datagridview-control?view=netframeworkdesktop-4.8) is an example. You should add some prints in your loop to check the data you are trying to add. – Kate Oct 30 '21 at 19:39
  • You need to clarify “what” `sortedStus` IS… you state that _”sortedStus is a DataRow list”_ … and this is odd. Does this mean that `sortedStus` is a `List`? If this is the case, then can you show what `Split_List(stuRows);` is doing and what it is returning? I am just saying that it is odd to have a "List" of `DataRows` since the `DataRows` MUST have originated from some `DataTable`. Can you clarify any of this? – JohnG Oct 31 '21 at 00:44
  • ```sortedStus``` is a ```List``` yes. ```Split_List(stuRows)``` is there to recursively break the list down into ```List```s of length 1 and which will have a field in them compared and sorted by another method, ```Sort_Lists``` that returns a ```List```. This works as intended and correctly orders the ```DataRows```. The ```DataRows``` originate from a ```DataGridView``` and are added to another ```List```, ```stuRows```, via a foreach loop that converts a ```DataRowView``` to a ```DataRow``` and then adds that to ```stuRows``` - this also works as intended – budywudy9 Oct 31 '21 at 08:53

1 Answers1

0

I FIGURED OUT A SOLUTION

my question was actually a duplicate of Simple way to convert datarow array to datatable

I needed to initialise dt as a new instance of DataTable and fill it using sortedStus.CopyToDataTable() and then setting the datasource of my DataGridView to dt.

Closing question now.

budywudy9
  • 29
  • 7