How to create a DataTable and populate it with data from a database using a DataView?

I am a new ASP.NET developer, and I want to create a table programmatically using HtmlTable or DataTable , and then populating this table with data from the database using DataView .

I could create a table using HtmlTable , but when I did a search regarding "how to populate an HtmlTable with data using a DataView ", I found that the DataView would not work with an HtmlTable . So I repeated the creation of the table using the DataTable , but now I need to populate it with data using the DataView , so how can I do this?

My code is:

  DataTable table = new DataTable(); DataColumn col1 = new DataColumn("Name"); DataColumn col2 = new DataColumn("Username"); col1.DataType = System.Type.GetType("System.String"); col2.DataType = System.Type.GetType("System.String"); table.Columns.Add(col1); table.Columns.Add(col2); DataRow row = table.NewRow(); row[col1] = "John"; row[col2] = "John123"; table.Rows.Add(row); GridView gv = new GridView(); gv.DataSource = table; gv.DataBind(); 

Regarding the table in the database, the schema of the user table:

 Username, FirstName, LastName, Age, Job 
+4
source share
2 answers

You can easily set the DataSource property to GridView as a DataView .

 GridView gv = new GridView(); gv.DataSource = yourDataView; gv.DataBind(); 

By the way, I don't see the need to create GridView software; you can probably define it directly on your markup. But it is for you to decide what suits you.

+2
source

I think you could be something like:

 var MyTable = new DataAccess.MyTable(); MyTable.LoadAll(); row[col1] = MyTable.Username; row[col2] = MyTable.FirstName + MyTable.LastName; row[col3] = MyTable.Age; row[col4] = MyTable.Job; 

Hope this helps.

+1
source

Source: https://habr.com/ru/post/1387780/


All Articles