Simple data binding of a commonly used control for Winform development-code binding of DataSet, able, IList, SqlDataReader, and datagridviewilist

Source: Internet
Author: User

Simple data binding of a commonly used control for Winform development-code binding of DataSet, able, IList, SqlDataReader, and datagridviewilist

The previous section describes the automatic data binding feature provided by Winform for the DataGridView. The following section describes how to bind data using code.

1. Use DataSet and DataTable to provide data sources for the DataGridView

Code first

Private void Form1_Load (object sender, EventArgs e) {String strConn = "Data Source = 210. 26. *. *; Initial Catalog = Test; User ID = sa; Password = ***** "; SqlConnection conn = new SqlConnection (strConn ); string sqlId = "select * from [Student]"; conn. open (); SqlCommand cmd = new SqlCommand (sqlId, conn); SqlDataAdapter da = new SqlDataAdapter (cmd); DataSet ds = new DataSet (); da. fill (ds, "student"); // dataGridView1.DataSource = ds. tables ["Student"]; albe is used directly to bind the data. The effect of the following two lines of code is the same as that of the dataGridView1.DataSource = ds; // If Dataset is used, DataMember must be specified, because DataSet is a set of DataTable, and datagridview can only bind one datatable dataGridView1.DataMember = "Student"; conn. close ();}

It should be noted that, if you do not set the dataGridview, the above Code will automatically bind the data to the dataGridview, as shown below:

The display is not very good, so I only need to display the name and address. What should I do? You need to set the items attribute of the datagridview itor the arrow on the right of the datagridview -- edit the column.

In addition to setting HeaderText to display column names, you must set the DataPropertyName attribute to correspond to the columns in the table.

Is that all right? NO. In this case, if you continue to run the above program, the situation will occur.

Why? You only specify to bind two columns.

DataGridView1.AutoGenerateColumns = false; // you do not need to automatically bind data columns to this row. The data columns are specified in the items set of the dataGridView itattribute and must be placed before the data is bound. It is useless to put them in the future.

Yes, you need to add this line of code to the Code, and this line of code must be placed before the dataGridView. DataSource. It is useless if it is placed later.

This section describes the meaning of a column attribute Frozen (freeze), that is, if the datagridview has a scroll bar, the Frozen columns will not be moved when the scroll bar is pulled, and other columns will follow the scroll bar, the figure above shows that when the scroll bar is pulled, the address bar is hidden, and the name column is frozen column true.

2. Use the IList class set to bind

In the process of software development, some data is found to be IList, And it is troublesome to convert the data into a able. Can we bind IList Ni directly? In fact, it is also possible to paste a little bit of code

IList <Student> lst = StudentService. GetAllOrderbyNumb (); // the result of the method provided by Student is Ilist dataGridView1.DataSource = lst;

It's actually so simple.

3. bind with SqlDataReader

Use SqlDataReader to bind the data source. Now, place a data source control BindingSource on the form, and run the code on it.

Private void Form1_Load (object sender, EventArgs e) {String strConn = "Data Source = 210. 26. *. *; Initial Catalog = Test; User ID = sa; Password = *** "; SqlConnection conn = new SqlConnection (strConn ); string sqlId = "select * from [Student]"; conn. open (); SqlCommand cmd = new SqlCommand (sqlId, conn); SqlDataReader studentlist = cmd. executeReader (); bindingSource1.DataSource = studentlist; // the data source control bindingSourse is a required dataGridView1.DataSource = bindingSource1; conn. close ();}

 

Related Article

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.