Notes for binding data sources

Source: Internet
Author: User

For ComboBox data source binding, imagine that we can retrieve data from the database and store the data in the able. There may be many columns in the table, but our ComboBox usually contains two items, one selecteditem is the text displayed on the interface, and the other selectedvalue is the value of the text displayed on the interface. This value is generally increased from 0 to int type. Then how can we bind the field we want to bind to combox.

For example, we can find the ID, name, age, sex, and birthday fields from the database table student. We set two attributes of the ComboBox,

Combstudent. valuemember = "ID ";

Combstudent. displaymember = "name ";

Combstudent. datasource = DT;

In this way, we can bind ComboBox.

However, we will find a problem at this time. What we want is that after binding, our ComboBox will display the index as 0. But what we see is empty.

At this time, we thought of adding a property selectedindex = 0 to him. When we run it, an error will be reported. It seems that the data source cannot be bound...

At this time, we can add one item to the able. The datatrow in the datatable has this method, insertat (), which can add the row to the position we want to add. Create a row first.

Datarow DR = new datatrow ()

Dr ["ID"] =-1 // we say that our value is auto-incrementing from 0. We can add this value to us.ProgramThe value of the item with no impact is set to-1, so we don't have to worry about selecting this program will report an error.

Dr ["name"] = "-- Please Select name --"

DT. Rows. insertat (DR, 0 );

In this way, we will probably implement the ComboBox filling.

The filling of the gridview is relatively simple. Note that some fields in our datatable do not want to be displayed on the interface.

However, our program must use this field, but we do not want to display it to users.

For example, ID, but when we bind it directly, our ID will be displayed on the interface. How can this problem be solved:

You can click the dview.

First, we will think like this. We can add four names, age, sex, and birthday to the value without ID. Then we can set the values of datapropertyname to name, age, sex, and birthday respectively.

When running, we will find that the ID is still displayed, but it is displayed at the end. Let's look at the window for editing the column. We will find that each item has a visable attribute, at this time, we can solve the problem.

We edit the number of columns in the table, and set the visable Attribute Set datapropertyname to ID to false.

In addition, when we want to click a row of dataview, We will display a lot of text boxes showing the name, age, sex, and birthday of the selected row. It is convenient for us to modify the text. At this time, we need to bind the text.

Basically, each control has this attribute, databingings.

Txtname. databingings. add ("text", datagridview1.datasource, "sname"). The first parameter of the add method is the attribute of the text to be bound, and the second parameter is the data source to be bound, the third is the attribute of the field of the data source to be bound.

This seems hard to understand.

In this way, two text boxes, txtbox1 and txtbox2.

Set txtbox1 attributes: txtbox1.databingings. Add ("text", txtbox2, "text ");

Then we enter in text 2 to see what will happen.

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.