The final Solution for adding a ComboBox in WinForm DataGridView (click ComboBox to display the contents of the current row by default)

Source: Internet
Author: User

First:

Data-bound ComboBox control

Drag a ComboBox control on the form design first, and then set the items in the items you want to drop, or bind from a table in the database, this is an estimate.

Second:

Add a drop-down list box to the DataGridView control, which is written after binding DataGridView. In the form's Load method, add: G_DataGridView.Controls.Add (G_combobox); Adds a ComBoBox control to the DataGridView control
Third: Write the following code in the CurrentCellChanged method of the DataGridView control:

DataGridViewCell Currnetcell = G_view.currentcell;
if (Currnetcell! = null && CurrnetCell.OwningColumn.Name = = "Column name")
{
Rectangle tmprect = G_datagridview.getcelldisplayrectangle (Currnetcell.columnindex, Currnetcell.rowindex, true);
G_combobox.text = CurrnetCell.Value.ToString ();
G_combobox.size = tmprect.size;
G_combobox.top = Tmprect.top;
G_combobox.left = Tmprect.left;
G_combobox.visible = true;
}
Else
{
G_combobox.visible = false;
}

pay special attention to this sentence: G_combobox.text = CurrnetCell.Value.ToString ();

If the ComboBox is bound to two values, that is, the encoding and the name, then change the sentence to

g_combobox.selectedvalue= CurrnetCell.Value.ToString () Otherwise, the default value after clicking the ComboBox control is not the value selected in the current table.

Fourth:

Write in the SelectedIndexChanged method of the ComboBox control:
G_DataGridView.CurrentCell.Value = G_combobox.text;

Note: If a code is changed to:

G_DataGridView.CurrentCell.Value = G_ComBoBox.SelectedValue.ToString ();

All right, it's done.

From: http://blog.csdn.net/hfzsjz/article/details/2836979

The final Solution for adding a ComboBox in WinForm DataGridView (click ComboBox to display the contents of the current row by default)

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.