Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Problem with Datagridview with combobox column
 

Problem with Datagridview with combobox column

Hi,

I have a data grid view which includes a combo box column. The combo box column is the second column in the grid view. I have a button which fills the grid based on some conditions entered in a text box. The combo box is bound to a dataset and then the grid is bound to another dataset. This works fine the first time I click the button. However when I click the button the second time I get this error

Unable to cast object of type 'System.Windows.Forms.DataGridViewTextBoxColumn' to type 'System.Windows.Forms.DataGridViewComboBoxColumn'."

Could anyone let me know why this happens
The code behind the button is shown below

Private

Sub btnAssignGetPackage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAssignGetPackage.Click

Try

Dim ds As DataSet = DB.GetPackagesUnionConfig(Me.cboEnviron.SelectedValue, Me.txtAssignFilter.Text)

Dim dsAreas As DataSet = DB.GetAreas(Me.cboEnviron.SelectedValue)

If ds.Tables(0).Rows.Count > 0 Then

Me.dgv_AssignSelectPackage.Visible = True

Me.btnAssignSavePackage.Visible = True

Dim lst As DataGridViewComboBoxColumn = CType(dgv_AssignSelectPackage.Columns(1), DataGridViewComboBoxColumn)

lst.DataSource = Nothing

lst.DataSource = dsAreas.Tables(0)

lst.DisplayMember = "AREA"

lst.ValueMember = "AREA"

Me.dgv_AssignSelectPackage.DataSource = Nothing

Me.dgv_AssignSelectPackage.DataSource = ds

Me.dgv_AssignSelectPackage.DataMember = ds.Tables(0).TableName

Else

Me.dgv_AssignSelectPackage.Visible = False

Me.btnAssignSavePackage.Visible = False

End If

Catch ex As Exception

MsgBox(ex.Message, MsgBoxStyle.Critical, "Error when retreiving Packages")

End Try

End Sub


Thanks in advance

Cooper


Cooperjv  Thursday, August 06, 2009 12:57 AM

Hi,

Does the project throw the error at
Dim lst As DataGridViewComboBoxColumn = CType(dgv_AssignSelectPackage.Columns(1), DataGridViewComboBoxColumn) ?


Which way do you used to set the DataGridViewComboboxColumn? Setting DisplayIndex? If so, displayIndex is not the true index, it only changes the display column order. You can get the index by DataGridViewColumn.Index property. You'd better set a breakpoint at the CType method to check whether the columns(1) is typed of DataGridViewComboBoxColumn.

More information:

DataGridViewColumn.DisplayIndex Property

http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcolumn.displayindex.aspx

DataGridViewColumn Properties

http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcolumn_properties.aspx

If I misunderstood you, or you have further questions, please feel free to tell me.

Best regards,

Ling Wang


Please remember to click “Mark as Answer�on the post that helps you, and to click “Unmark as Answer�if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
Ling Wang  Tuesday, August 11, 2009 3:08 PM

Hi,

Does the project throw the error at
Dim lst As DataGridViewComboBoxColumn = CType(dgv_AssignSelectPackage.Columns(1), DataGridViewComboBoxColumn) ?


Which way do you used to set the DataGridViewComboboxColumn? Setting DisplayIndex? If so, displayIndex is not the true index, it only changes the display column order. You can get the index by DataGridViewColumn.Index property. You'd better set a breakpoint at the CType method to check whether the columns(1) is typed of DataGridViewComboBoxColumn.

More information:

DataGridViewColumn.DisplayIndex Property

http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcolumn.displayindex.aspx

DataGridViewColumn Properties

http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcolumn_properties.aspx

If I misunderstood you, or you have further questions, please feel free to tell me.

Best regards,

Ling Wang


Please remember to click “Mark as Answer�on the post that helps you, and to click “Unmark as Answer�if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
Ling Wang  Tuesday, August 11, 2009 3:08 PM

You can use google to search for other answers

Custom Search

More Threads

• What is the most efficient way to do arithmatic on DataTable cell?
• Windows Forms Datagrid Red Cross Issue
• Print Datagrid
• Datagridview: is there a way to remove the focus of the first row?
• i need to open msoutlook in my project and a .doc file should be the body of the mail text
• Finding a Datarow in a Datatable
• Datagrid to Excel
• BindingSource, DatagridView and Regional Settings
• Button in DataGrid Column
• displaying selected columns in a datagrid