3

Why would you construct a new DataView instead of using the DefaultView of the DataTable in C#?

What are the scenarios creating a new DataView is preferable?

What are the advantages and disadvantages of both?

var dataView = new DataView(dataTable);

vs

var dataView = dataTable.DefaultView;

TaW
  • 53,122
  • 8
  • 69
  • 111
Brk
  • 478
  • 2
  • 6
  • 17

2 Answers2

7

The DefaultView has the advantage of being there already by default, as the name implies.

Additional DataViews have the advantage of allowing you to keep several of them ready and in use in parallel.

So you can filter and sort 3 of them in different ways and bind 3 different controls, e.g. three DataGridViews or a DGV and the Items of a ComboboxCell to them independently.

Quoting from this post:

A dataview is a view on a datatable, a bit like a sql view. It allows you to filter and sort the rows - often for binding to a windows form control. Additionally, a DataView can be customized to present a subset of data from the DataTable. This capability allows you to have two controls bound to the same DataTable, but showing different versions of the data.

Community
  • 1
  • 1
TaW
  • 53,122
  • 8
  • 69
  • 111
0

And another scenarios creating a new DataView is preferable, it is asp global (app variable) datatable shared between sessions. Defaultview with rowfilter not preferable, because applied filter affect all sessions defaultview. So you must create dataview for every session. vb.net

Application("dt") = New DataTable() - persits across sessions
Application("dt").DefaultView.RowFilter="Field = Value" - not preferable because it apply all sessions
Session("dv") = New DataView(Application("dt"))
Session("dv").RowFilter="Field = Value" - preferable
Maxrem
  • 45
  • 10
  • Maxrem, this does not provide an answer to the question. You can [search for similar questions](//stackoverflow.com/search), or refer to the related and linked questions on the right-hand side of the page to find an answer. If you have a related but different question, [ask a new question](//stackoverflow.com/questions/ask), and include a link to this one to help provide context. See: [Ask questions, get answers, no distractions](//stackoverflow.com/tour) – Yunnosch Jun 14 '18 at 19:57
  • Disagree . It is scenarios creating a new DataView is preferable, which asked. – Maxrem Jun 14 '18 at 19:59
  • In that case please take my apology and rephrase your answer, so that it reads less like "I have the same problem, it does not work, what I need is ...". For an answer the most misleading phrases are "I use", "not good", "I need" and the final inconsequentially trailing off "...". – Yunnosch Jun 14 '18 at 20:02
  • 1
    Edit / rephrase my answer. – Maxrem Jun 14 '18 at 20:08
  • 1
    I see. Yes, now is reads much more like an answer. Thank you for taking my ciriticism in such a matter-of-fact way. – Yunnosch Jun 14 '18 at 20:08