How to implement datagridview binding
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!