Event handling for various types of cell controls in DataGridView (selection of Datagridviewcheckboxcell, DataGridViewComboBoxCell, and event triggers)

Source: Internet
Author: User
Tags button type

There are several types of columns in a DataGridView control, such as a datagridviewbuttoncolumn that represents a button type, a datagridviewcheckboxcolumn that represents a check box type, Represents the Datagridviewcomboboxcolumn of a Drop-down box type, a datagridviewimagecolumn representing a picture type, a datagridviewlinkcolumn that represents a hyperlink type, Datagridviewtextboxcolumn that represents the type of text box. The types of cells in these columns are Datagridviewbuttoncell, Datagridviewcheckboxcell, DataGridViewComboBoxCell, DataGridViewImageCell and Datagridviewlinkcell.

These types of column can, to some extent, correspond to WinForm button controls, CheckBox controls, PictureBox controls, LinkLabel controls, and TextBox controls, but there are significant differences, especially in the handling of events, such as When we use the WinForm CheckBox control, it is easy to get the checkbox's selected state through the CheckedChanged event, with some other control, but the checkbox in DataGridView The state of "CheckBox" can be indirectly judged and controlled only by means of DataGridView triggering events. Here are a few common scenarios:

1 When you click the button, link, and checkbox in a cell, you can do something by using the DataGridView CellContentClick event to implement the feature. As shown in the following code:

Assume that the embedded Datagridviewbuttoncell, Datagridviewlinkcell, and Datagridviewcheckboxcell are in the first column of DataGridView, the second and third columns respectively.

private void Datagridview1_cellcontentclick (object sender, DataGridViewCellEventArgs e)

{

if (e.columnindex==0)

{

MessageBox.Show ("You pressed a button.") ");

}

else if (e.columnindex==1)

{

MessageBox.Show ("You clicked a hyperlink.") ");

}

else if (E.columnindex = 2)

{

BOOL B = (bool) this.datagridview1.rows[0]. CELLS[1]. Editedformattedvalue;

if (b)//To determine whether the checkbox is selected

{

MessageBox.Show ("You have selected the checkbox.") ");

}

Else

{

MessageBox.Show ("You make the checkbox lose the selected." ");

}

}

}

There are two things to note:

(a) First, the columnindex of E is judged, and then the operation is carried out;

b When you get a cell in DataGridView, be sure to use the Editedformattedvalue property, which gets the current formatted value of the cell, regardless of whether the cell is in edit mode or not, and the value And FormattedValue return is often to edit the previous value (the main reason is the focus of the problem, the current cell loses focus after the edited value will be submitted), so be sure to use Editedformattedvalue to get the value.

2 Select an item in the ComboBox and make a specific action corresponding to the selected item, which can be implemented using the DataGridView Cellendedi event. As shown in the following code:

Suppose the embedded Datagridviewcombobxcell is in the first column of DataGridView.

private void Datagridview1_cellendedit (object sender, DataGridViewCellEventArgs e)

{

if (e.columnindex==2)

{

MessageBox.Show ("The item you selected is:" +datagridview1.rows[e.rowindex]. CELLS[2]. Editedformattedvalue.tostring ());

}

}

The code in the function needs to cause the current cell to lose focus before it is executed because the edit mode will not end until the focus is lost.

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.