Linkage treatment of datagridviewtextboxcolumn in Winfrom DataGridView

Source: Internet
Author: User

There are two ways to do this: the first is to use the Datagridviewtextboxcolumn control that comes with DataGridView, and the second is to dynamically add a ComboBox control method, one of which is to first drag a DataGridView then adds two columns datagridviewtextboxcolumn in this DataGridView (the first column is called A, the second column is B) and then binds a code a.datasource= ds. tables[0].            DefaultView; A.displaymember="table_name"; A.valuemember="table_name"; ((datagridviewcomboboxcolumn) datagridview1.columns[0]). Defaultcellstyle.nullvalue ="--Please choose- -";//Default Valuefollowed by binding B code//The second column of the currently selected row is assigned a value((DataGridViewComboBoxCell) Datagridview1.rows[datagridview1.currentcell.rowindex]. cells[1]). DataSource = ds. tables[0].            DefaultView; ((DataGridViewComboBoxCell) Datagridview1.rows[datagridview1.currentcell.rowindex]. cells[1]). DisplayMember ="Comments"; ((DataGridViewComboBoxCell) Datagridview1.rows[datagridview1.currentcell.rowindex]. cells[1]). ValueMember ="column_name"; ((datagridviewcomboboxcolumn) datagridview1.columns[2]). Defaultcellstyle.nullvalue ="--Please choose- -"; then add the SelectedIndexChanged event codePrivate voidDatagridview1_editingcontrolshowing (Objectsender, Datagridvieweditingcontrolshowingeventargs e) {DataGridView DGV=(DataGridView) sender; if(DGV. CurrentCell.OwningColumn.Name = ="A") {ComboBox CB=(ComboBox) E.control; Cb. SelectedIndexChanged+=NewEventHandler (combobox_selectedindexchanged); }} SelectedIndexChanged event code Public voidCombobox_selectedindexchanged (Objectsender, EventArgs e) {ComboBox ComboBox=(ComboBox) sender; if(DataGridView1.CurrentCell.OwningColumn.Name = ="Table name")            {                if(Combobox.text! ="")                {                    //This is the method of binding BDbfieldnote (Combobox.text); }}} Method Two: Instantiate the ComboBox object FirstPrivateComboBox combobox =NewComboBox (); PrivateComboBox cb =NewComboBox (); second: This. DATAGRIDVIEW1.CONTROLS.ADD (ComboBox);//to add a control to the DataGridViewDbtablename ();//Binding ComboBoxCombobox.visible =false; Combobox.selectedindexchanged+=NewEventHandler (combobox_selectedindexchanged);//Add Event Private voidCombobox_selectedindexchanged (Objectsender, EventArgs e) {            stringName =((ComboBox) sender).            Text; DataGridView1.CurrentCell.Value= name;//adds the selected value to the currently selected cell in DataGridViewDbfieldnote (name);//bind a second combobox (aka CB)        }  Public voidDbfieldnote (stringtablename) {             This. DATAGRIDVIEW1.CONTROLS.ADD (CB); Cb. Visible=false; Cb. SelectedIndexChanged+=NewEventHandler (cb_selectedindexchanged); ......        } Private voidCb_selectedindexchanged (Objectsender, EventArgs e) {DataGridView1.CurrentCell.Value=CB.        Text; }    Private voidDatagridview1_currentcellchanged (Objectsender, EventArgs e) {            if(Datagridview1.currentcell! =NULL)            {                if(DataGridView1.CurrentCell.ColumnIndex = =1)//If the first column is selected, the first ComboBox is displayed{System.Drawing.Rectangle rect= Datagridview1.getcelldisplayrectangle (DataGridView1.CurrentCell.ColumnIndex, DataGridView1.CurrentCell.RowIndex,false);//gets the properties of the currently selected cell (wide, high)Combobox.left =rect.                    Left; Combobox.top=rect.                    Top; Combobox.width=rect.                    Width; Combobox.height=rect.                    Height; Combobox.visible=true; }                Else if(datagridview1.currentcell.columnindex==2)//If the second column is selected, the CB is displayed{System.Drawing.Rectangle Rect1= Datagridview1.getcelldisplayrectangle (Comboxindex +1, DataGridView1.CurrentCell.RowIndex,false); Cb. Left=rect1.                    Left; Cb. Top=rect1.                    Top; Cb. Width=rect1.                    Width; Cb. Height=rect1.                    Height; Cb. Visible=true; }                           }            Else{combobox.visible=false; }        }

Linkage treatment of datagridviewtextboxcolumn in Winfrom DataGridView

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.