A question about Data Binding

Source: Internet
Author: User

I have been doing a little thing over the past few days, and I have been very depressed about data binding. It takes a lot of time for me to understand what is going on in the end,

I bound the data of ListBox and ComboBox, and there was always a problem when getting the data. I never met it before when I learned it,

The binding method of is

String SQL = "select * From userinfo ";
DS = dB. getdataset (SQL );
Lboxuser. datasource = Ds. Tables [0];
Lboxuser. displaymember = "userid ";
When I get the data, I always get the value system. Data. datarowview, which makes me depressed. Finally, I asked in blog, someone told me this should happen.

String SQL = "select * From userinfo ";
DS = dB. getdataset (SQL );
Lboxuser. datasource = Ds. Tables [0];
Lboxuser. displaymember = "userid ";
Lboxuser. valuemember = "userid ";

However, this method can obtain the correct value in lboxuser. selectvalue. When lbloxuser. items [0]. tostring () is used, the system. Data. datarowview is still obtained.

I was very depressed, and finally I used one,

I found that the itemsindex sequence of the control is the same as that of the data source, so I used two very reliable methods.

Method 1:

Datatable dt = (datatable) (cmbtype2.datasource );

String iems = DT. Rows [itemindex] ["userid"]. tostring (); // itemindex is the itemindex in ListBox.

This works.

Method 2:

This method is relatively simple. Check that my value is a system. Data. datarowview. Check that this string is a data type.

I will try to see if it can be converted to the corresponding data type. Let's take a look at the results and we can see the data,

Datarowview DR = lboxuser. items [itemindex] As datarowview; // itemindex is the itemindex

String iems = dt ["userid"]. tostring (); // you can obtain the expected data.

One advantage of this method is that you can retrieve all the data in the required columns when obtaining data. You do not need to perform operations with the database during ListBox operations.

Saving connections to databases

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.