DataGridView Embedded ComboBox and Datagridviewcomboboxcolumn data binding

Source: Internet
Author: User

We often use the combo drop-down box in the design of the client program, as in this case

I want to use DataGridView to achieve this function in the project, read a number of blogs, MSDN also turned half a day, finally have a little understanding.
Here are a few key points:
1. The DataGridView control has multiple types of columns, including
(1) Datagridviewtextboxcolumn (text column, which is the case by default)
(2) Datagridviewcomboboxcolumn (combo box column, the name of the blind, is the function to use the column)
(3) Datagridviewlinkcolumn (link column)
(4) Datagridviewbuttoncolumn (Button column)
(5) Datagridviewcheckboxcolumn (Hook column) That's almost what it means)
I want to add a combo box column like this, just choose new datagridviewcomboboxcolumn and add it to DataGridView, that's the basic idea.
2. Datagridviewcomboboxcolumn Data Binding
Datagridviewcomboboxcolumn has several key properties
(1) DataSource: As the name implies, bound data, the need to bind the table to assign values can be
(2) DataPropertyName: According to MSDN, the name of the data source property is set, and I understand that this is the selection of data from the large table, using the data source property name to identify what this property is. Can write at will, or can not write, should be a hint of the role.
(3) DisplayMember: This is the most critical attribute, first of all, it must be a field in the data table, and secondly, it is the data that the foreground displays. So, it is equivalent to a SELECT statement that retrieves all the data for the field name =displaymember from the DataSource table. Select ' DisplayMember ' from ' DataSource ' ("The representation is replaced with the corresponding actual data), in fact, in theory displaymember+ DataSource can complete this Combox embed DataGridView function.
(4) ValueMember:

Gets or sets a string that specifies the property or column from which to get values that correspond to the selections in The Drop-down list.
Gets or sets a string that specifies the property or column from which to get the value corresponding to the option for the drop-down list.

This attribute should be to make a mapping relationship with DisplayMember, but according to MSDN

In this example, the DisplayMember are set to the same value as ValueMember because no mapping are necessary.
DisplayMember is set to the name of the property that is responsible for containing text that the user can view. In this example, DisplayMember sets the same value as ValueMember because no mappings are required.

Although this translation is very confusing, but can barely understand, that is, when you do not need to map, the value of ValueMember set the same as DisplayMember.

Then write a few lines of code to indicate:

 PrivatevoidForm2_load (Object sender, EventArgs e) {Auxiliary processing class, without tube evaluatehierarchymanage.evaluatehierarchymanage standard =New Evaluatehierarchymanage.evaluatehierarchymanage ();Standard binds a table in the database "Evaluatehierarchy" DataSet standard= standard. Getallinfo ();Auxiliary processing class, without tube evaluatedatamanage.evaluatedatamanage data =New Evaluatedatamanage.evaluatedatamanage ();Data is bound to a table in the database "Evaluatedata" DataSet data = data. Getallinfo ();int i = 0; for (i = 0; i < data. Tables[0]. Columns.count; i++) {//Add User selection bar Datagridviewcomboboxcolumn combox = new Datagridviewcomboboxcolumn (); Combox. DataPropertyName =  "PrimeStandard1"; //must be consistent with the database field name, and the data in this field is the data DataGridView that the foreground displays in Combox. DisplayMember =  "classification"; //and DisplayMember remain consistent combox. ValueMember =  "Primestandard"; //bind data Combox. DataSource = data. Tables[0]; //added to DataGridView control DataGridView1.Columns.Add (Combox);} } 

Then a small problem: my software design to this situation: the user's input may be irregular, I give him combox to choose, and he also need to input data, the user input data is retained as the original data, The data selected by the user is used as a basis for calculation (because the pre-fabricated option must be normative for the software development person)
Probably just like this.

Also wrote two lines of code to achieve, of course, is also very simple, is the combination of different columns, also PO to make a record

private void Form2_load (object sender, EventArgs e) {evaluatehierarchymanage. Evaluatehierarchymanage standard = new Evaluatehierarchymanage. Evaluatehierarchymanage (); DataSet standard= Standard. Getallinfo (); Evaluatedatamanage. Evaluatedatamanage data = new Evaluatedatamanage. Evaluatedatamanage (); DataSet data = Data. Getallinfo (); DataBase. Database Database = new Database. DataBase (); List<string> columnsname=new list<string> (); foreach (DataColumn cIn Data. tables[0]. Columns) {Columnsname. ADD (c. ColumnName); } foreach (String sIn columnsname) {String sql ="Select Classification,id from Evaluatehierarchy where twolevelstandard= '" + s+"‘"; SqlCommand command = new SqlCommand (sql,database. Con); SqlDataAdapter SDA = new SqlDataAdapter (command); DataSet ds = new DataSet (); Sda. Fill (DS); Add user fill-in column if (ds. tables[0]. Rows. Count! =0) {Datagridviewtextboxcolumn DGVC = new Datagridviewtextboxcolumn (); Dgvc. HeaderText = s +"User fills in"; DataGridView1. Columns. ADD (DGVC); }//Add User selection bar Datagridviewcomboboxcolumn combox = new Datagridviewcomboboxcolumn (); Combox "classification" ;//must be consistent with database field names, The data in this field is displayed in the foreground in DataGridView data combox "classification" ;//must be consistent with the DisplayMember field Combox "classification"  Combox0]      

DataGridView Embedded ComboBox and Datagridviewcomboboxcolumn data binding

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.