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?

7
  • yourDataGridView.Columns.Clear() Commented Apr 20, 2020 at 6: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. Commented Apr 20, 2020 at 7:11
  • @reza-aghaei this isn't working.... Commented Apr 20, 2020 at 7:19
  • 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
    Commented Apr 20, 2020 at 9:33
  • 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
    Commented Apr 20, 2020 at 10:21

2 Answers 2

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
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(); 
3
  • 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. Commented Apr 20, 2020 at 9: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. Commented Apr 20, 2020 at 9:40
  • Sorry it's my typo mistake Commented Apr 20, 2020 at 10:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.