Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement datagridview binding

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces how to achieve datagridview binding, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

Before datagridview binding begins, take a look at the two forms of data binding for WinForm controls, simple data binding and complex data binding.

Simple data datagridview binding

Example 1

Using (SqlConnectionconn=newSqlConnection (ConfigurationManager.ConnectionStrings ["connStr"] .ToString ())

{

SqlDataAdaptersda=newSqlDataAdapter ("Select*FromT_ClassWhereF_Type='Product'orderbyF_RootID,F_Orders", conn)

DataSetDs=newDataSet ()

Sda.Fill (Ds, "T_Class")

/ / when using DataSet binding, you must also specify DateMember

This.dataGridView1.DataSource=Ds

This.dataGridView1.DataMember= "T_Class"

/ / you can also bind directly with DataTable

This.dataGridView1.DataSource=Ds.Tables ["T_Class"]

}

Simple data binding is to bind a property of a user control to a property on an instance of a type.

Bind in the following form: reference control .DataBindings.add (Control Properties, instance object, property name, true)

Example 2

Read the data from the database and put it in a dataset, such as List and DataTable,DataSet. I usually use List.

Then bind the data source:

IListsList=StudentDB.GetAllList ()

DataGridView.DataSource=sList

If you do not set the columns for DataGridView, it will automatically generate all columns.

Complex data datagridview binding

Complex data binding is to bind a list-based user control (such as ComboBox, ListBox, ErrorProvider, DataGridView, etc.) to a list of data objects.

Basically, WindowsForms's complex data binding allows binding to data lists that support IList interfaces. In addition, if you want to bind through a BindingSource component, you can bind to a data list that supports the IEnumerable interface.

For complex data binding, common data source types are (the code uses DataGridView as a sample control).

Thank you for reading this article carefully. I hope the article "how to achieve datagridview binding" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report