0

I've datagrid view in form which is dynamically populating data using DataView technique through this code:

 DV = New DataView()
 DV.Table = ds.Tables("rsSearch")
 FlxSearch.DataSource = DV

enter image description here

but i want to remove previous column structure. similarly, when i call this grid with same dataset this is happening: enter image description here

I've tried many solutions of clearing and disposing DGV and DataView but couldn't achieve desired result. any suggestions for how to remove previously filled column structure when loading new Data?

Osama Rizwan
  • 615
  • 1
  • 7
  • 19
  • `yourDataGridView.Columns.Clear()` – Reza Aghaei Apr 20 '20 at 06:55
  • Your `DataView` is probably pointless. Just bind the `DataTable`. Every `DataTable` already has a `DataView` associated with it, accessible via its `DefaultView` property. When you bind a `DataTable`, that's where the data comes from anyway. That's how you are able to click a grid column header to sort. You only need to create a `DataView` explicitly if you want multiple views of the same table. – jmcilhinney Apr 20 '20 at 07:11
  • @reza-aghaei this isn't working.... – Osama Rizwan Apr 20 '20 at 07:19
  • @jmcilhinney the solution you have given isn't working for my case. my `DataTable` column structure is not disposing properly that's the actual bug – Osama Rizwan Apr 20 '20 at 08:09
  • 1
    Set `FlxSearch.DataSource = ds.Tables("rsSearch")`. When you change the DataTable, the DGV's Column structure follows suite. Unless you have added those Columns in the Designer. Since you want to change DataTable, thus Column Names, dynamically, remove the Columns from the Designer. – Jimi Apr 20 '20 at 09:33
  • @Jimi actually i want to filter out data using `RowFilter` on data set. that's why can't assign directly to grid... – Osama Rizwan Apr 20 '20 at 10:18
  • 1
    Yes, you can. Well, not simply *can*, that's exactly how you do it. Just with `ds.Tables("rsSearch").DefaultView.RowFilter = "The Filter"`. Or `DirectCast(FlxSearch.DataSource, DataTable).DefaultView.RowFilter = "The Filter"`. Same object, same filter, same result. – Jimi Apr 20 '20 at 10:21

2 Answers2

0

Set the DataSource to Nothing first, which will remove the rows, and then call Clear on the Columns collection. Setting the DataSource anew will then generate new columns.

myDataGridView.DataSource = Nothing
myDataGridView.Columns.Clear()
myDataGridView.DataSource = myNewDatatable
jmcilhinney
  • 50,448
  • 5
  • 26
  • 46
0

Need to clear Data Table columns not DGV. Because it’s not strongly bound to DataGridView.

In VB.NET:

ds.Tables(tableName).Columns.Clear()

In C#:

ds.Table[TableName].Columns.Clear(); 
Osama Rizwan
  • 615
  • 1
  • 7
  • 19
  • You do need to clear the grid columns regardless. You need to clear the table columns if you're using the same `DataTable` each time. If you're creating a new `DataTable` each time then there's no need to clear the columns of the old one. If you do use the same table, you still need to rebind the grid each time, otherwise it won't generate new columns itself. A `DataGridView` only auto-generates columns at the time of binding. Changing the schema of a `DataTable` while it's bound won't affect the grid. – jmcilhinney Apr 20 '20 at 09:05
  • Then why did you say this: *"when i call this grid with another dataset this is happening"*? If you omit details or, worse still, provide inaccurate details, it makes it hard for us to provide a relevant solution. ALWAYS provide a FULL and CLEAR explanation. – jmcilhinney Apr 20 '20 at 09:40
  • Sorry it's my typo mistake – Osama Rizwan Apr 20 '20 at 10:14