How to bind a database field to a ComboBox control in WinForm development

Source: Internet
Author: User

Recently started to write a financial analysis software, because they are just learning. NET soon, so I wrote the time encountered a lot of problems, hope that through the blog to some impressive problems recorded.

How to bind a database field to a ComboBox control in WinForm development

1. Problem Introduction

The combobox control is used in development, which is the control that implements the drop-down selection function. However, the data for the option is not fixed, but is dynamically adjusted based on a column in the database.

2. Solution Ideas

Here I think about two things, one is that the selection of the drop-down list is a property of the entity class in the project, or the other database primary key that the project uses for that property.

This is mostly done by reading and encapsulating the entity object through the SqlDataReader method into the collection, and then binding the data.

And my case here is just to show the corresponding property, using the SqlDataAdapter method to read the data, and then bind the data when the control's form is initialized.

3. Reference Code

First case:

Code in the SqlHelper class:

1  Public StaticSqlDataReader Getreader (stringsql)2     {3SqlConnection conn =NewSqlConnection (connstring);4SqlCommand cmd =NewSqlCommand (SQL, conn);5         Try6         {7 Conn. Open ();8             returncmd. ExecuteReader (commandbehavior.closeconnection);9         }Ten         Catch(Exception ex) One         { A Conn. Close (); -             //write error message to log ... -  the             Throwex; -         } -}

Encapsulation into the collection:

1  PublicList<studentclass>getallclasses ()2     {3         stringsql ="Select Classname,classid from StudentClass";4SqlDataReader objreader =sqlhelper.getreader (SQL);5list<studentclass> list =NewList<studentclass>();6          while(Objreader.read ())7         {8List. ADD (NewStudentClass ()9             {TenClassId = Convert.ToInt32 (objreader["ClassId"]), OneClassName = objreader["ClassName"]. ToString () A             }); -         } - objreader.close (); the         returnlist; -}

The data is then called and bound in the form initialization method:

1  PrivateStudentclassservice Objclassservice =NewStudentclassservice ();2 3      Publicfrmaddstudent ()4     {5 InitializeComponent ();6         //Initialize class drop-down box7          This. Cboclassname.datasource =objclassservice.getallclasses ();8          This. Cboclassname.displaymember ="ClassName";9          This. Cboclassname.valuemember ="ClassId";Ten  One}

Second case:

Code in the SqlHelper class:

1   Public StaticDataSet GetDataSet (stringsql)2     {3SqlConnection conn =NewSqlConnection (connstring);4         //SqlCommand cmd = new SqlCommand (SQL, conn);5SqlDataAdapter da =NewSqlDataAdapter (SQL, conn);6DataSet ds =NewDataSet ();7 da. Fill (DS);8         returnds;9}

The data is then called and bound in the form initialization method:

1  Public Partial classFrmvoucherquery:form2 {3      Publicfrmvoucherquery ()4     {5 InitializeComponent ();6         stringsql =string. Format ("Select FName from T_vouchergroup Order by fGroupId");7         //The name of the control. datasource= data set. Data Sheet8Cbvouchername.datasource = Sqlhelper.getdataset (sql). tables[0];9Cbvouchername.displaymember ="FName";TenCbvouchername.valuemember ="FName"; One     } A}

How to bind a database field to a ComboBox control in WinForm development

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.