Dynamically generate a drop-down list for the Combobox column of the DataGridview

Source: Internet
Author: User

Always go into misunderstanding on this issue

Misunderstanding 1: always try to get the specific control of each row through FindControl in ASP. Net.

Misunderstanding 2: always try to avoid generating the DataError event of the DataGridview.

 

After reading many articles, I finally accepted the clone-like relationship between controls in the cells in the Combobox column.

After accepting this reality, if you still have more personalized requirements for the Combobox columns and are unwilling to do too much rewrite work, the performance sacrifice cannot be avoided.

You can do this:

First, add the Combobox drop-down list processing in the appropriate place

 

 Private Sub DataGridView1_CellBeginEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellCancelEventArgs) Handles DataGridView1.CellBeginEdit

If e.ColumnIndex = 1 Then
Dim TempDataGridViewComboBoxCell As DataGridViewComboBoxCell = CType(DataGridView1(e.ColumnIndex, e.RowIndex), DataGridViewComboBoxCell)
TempDataGridViewComboBoxCell.Dispose()
TempDataGridViewComboBoxCell.Items.Add(Rnd)
TempDataGridViewComboBoxCell.Items.Add(Rnd)
TempDataGridViewComboBoxCell.Items.Add(Rnd)
TempDataGridViewComboBoxCell.Items.Add(Rnd)

End If

End Sub

 

 

The second is to add a DataError event to handle possible errors. The solution to the error is to do nothing.

 

 
Private Sub maid (ByVal sender As Object, ByVal e As System. Windows. Forms. DataGridViewDataErrorEventArgs) Handles maid
'Leave it empty.
End Sub

 

 

 

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.