Datagridview.datasource= list (of T)

Source: Internet
Author: User

Note: The code for this example follows the previous article: DataTable fills the entity class back to the generic collection.


After the D-level query is complete, we convert the DataTable into a generic collection, then go through the middle layers, return to the U-layer, and here, the problem is, we're going to bind this set as a data source to DataGridView and display the results, This article focuses on the issue of how to display collections on DataGridView. (Maybe you're returning a DataTable or an entity, and they're loading the data in one drop ~)


One, show all properties of the entity

when the column name of the DataGridView is the attribute of class T, it is only necessary to bind the data source and then change the column name manually.


<span style= "FONT-SIZE:14PX;" > ' Convert dt to generic collection myList = Entityhelper.converttolist (of Entityuser) (dt) TextBox1.Text = Mylist.count ' in text The box shows the number of teachers to query if (Mylist.count > 0) Then ' if query to record ' will display the queried data to DataGridView MyData            Grid.datasource = MyList ' Sets the data source for the control: But this setting is not enough because the name of the column is displayed in English, so you should also set the name that is not listed. ' Modify the column name Mydatagrid.columns (0). HeaderText = "card number" mydatagrid.columns (1). HeaderText = "Password" mydatagrid.columns (2). HeaderText = "Teacher Name" Mydatagrid.columns (3). HeaderText = "Teacher Level" mydatagrid.columns (4). HeaderText = "Registration Date" mydatagrid.columns (5). HeaderText = "Registration Time" mydatagrid.columns (6). HeaderText = "Logoff Date" mydatagrid.columns (7). HeaderText = "Logoff Time" mydatagrid.columns (8). HeaderText = "On machine status" Else MsgBox ("No records are queried!") ") End if</span>



The results appear as follows:





Two, display some properties of class T

sometimes we don't want to show all the properties of T, we just need to show part of it, for example, we may not need to display the teacher's password when we display the teacher's information.


method One:Manually remove columns after all data has been loaded

For example, if I want to remove the password column, just after all the data has been loaded, add a line of code:

            ' Manually remove the password after loading all data            myDataGrid.Columns.Remove ("PWD")

when shown again, the results are as follows:




Although I can achieve the desired effect, but always feel like doing a bit of thunder, as if eating something to spit it out, anyway, I do not. Let's look at other ways.


Method Two: Set the DataPropertyName property

For example, I now just want to display the user name, first, right-click the DataGridView control on the form and choose Edit column:



Add a column called a user name, and then set the column's DataPropertyName property, which allows you to set a property value that is bound to an entity, a column in the DataTable, and so on. The datapropertyname value here corresponds to the attribute name of the entity .



after that, we write in the code

  <span style= "FONT-SIZE:18PX;" >       mydatagrid.autogeneratecolumns = False  ' Do not allow auto-add column        Mydatagrid.datasource = myList ' Set data source </span >

this will be OK ~


Run, you can see this effect:



Summary:

really feel DataGridView This space is very useful, directly bound data source can display data.

And if, on the D-level, the query is completed, the DataTable is converted into an entity set, and when the results are uploaded to the U-layer DataGridView Display, the datasource is directly specified as the entity collection, then the whole process is not the same as in the previous VB MSHFlexGrid controls:




        This process is assigned one line at a time, avoiding the problem that the Filds attribute fails at the specified time. And the code is more concise, and our workload has been greatly reduced.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.