I have a datagridview(with 5 columns: 4 textbox and 1 combobox) and it loads data in there from mysql. This is the code I used, when I tried it in all textbox column, it works fine. But when I change the last column into a combobox, I got an error saying:
"The following exception occured in the DataGridView:
System.ArgumentException: DataGridViewComboBoxCell value is not valid.
To replace this default dialog please handle the DataError event."
Private Sub Main_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
sqlconn.ConnectionString = "server=localhost;userid=root;password=12345;database=db_scheduling"
teacherQuery("SELECT * FROM tbl_teacher ORDER BY id")
End Sub
Sub teacherQuery(ByVal strings)
Try
Dim sqlcommand As New MySqlCommand
Dim sqladapter As New MySqlDataAdapter
sqlcommand.Parameters.AddWithValue("@id", AddTeacher.txt_AddTeacher_ID.Text)
sqlcommand.Parameters.AddWithValue("@lname", AddTeacher.txt_AddTeacher_Lname.Text)
sqlcommand.Parameters.AddWithValue("@fname", AddTeacher.txt_AddTeacher_Fname.Text)
sqlcommand.Parameters.AddWithValue("@mname", AddTeacher.txt_AddTeacher_Mname.Text)
sqlcommand.Parameters.AddWithValue("@subject_to_teach", AddTeacher.cmb_AddTeacher_Subject.SelectedItem)
Dim Table As New DataTable
With sqlcommand
.CommandText = strings
.Connection = sqlconn
End With
With sqladapter
.SelectCommand = sqlcommand
.Fill(Table)
End With
'----HERES THE PROBLEM-----'
datagrid_Teacher.Rows.Clear()
For x As Integer = 0 To Table.Rows.Count - 1
datagrid_Teacher.Rows.Add(Table(x)("id"),
Table(x)("lname"),
Table(x)("fname"),
Table(x)("mname"),
Table(x)("subject_to_teach"))
Next
'---------------------------'
'Bold header
With datagrid_Teacher.ColumnHeadersDefaultCellStyle
.Font = New Font(datagrid_Teacher.Font, FontStyle.Bold)
End With
Catch ex As Exception
MessageBox.Show("ERROR: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
EDIT: The problem in here is adding data in the datagridview (combobox (subject_to_teach column))
Try this instead of the datagrid_Teacher.Rows.Add way:
datagrid_Teacher.Datasource = Table;
datagrid_Teacher.Databind();
Alternatively add this on your code (event handler for Gridview Dataerror):
private void ctrldataGridView_DataError(object sender, DataGridViewDataErrorEventArgs e)
{
//do nothing
}
I managed to do it by removing the combobox column in design and add the combobox column in code, and modified my code into this:
Sub teacherQuery(ByVal strings)
Try
Dim sqlcommand As New MySqlCommand
Dim sqladapter As New MySqlDataAdapter
sqlcommand.Parameters.AddWithValue("@id", AddTeacher.txt_AddTeacher_ID.Text)
sqlcommand.Parameters.AddWithValue("@lname", AddTeacher.txt_AddTeacher_Lname.Text)
sqlcommand.Parameters.AddWithValue("@fname", AddTeacher.txt_AddTeacher_Fname.Text)
sqlcommand.Parameters.AddWithValue("@mname", AddTeacher.txt_AddTeacher_Mname.Text)
sqlcommand.Parameters.AddWithValue("@subject_to_teach", AddTeacher.cmb_AddTeacher_Subject.SelectedItem)
sqlcommand.Parameters.AddWithValue("@search", "" + txt_TeacherSearch.Text + "%")
Dim Table As New DataTable
With sqlcommand
.CommandText = strings
.Connection = sqlconn
End With
With sqladapter
.SelectCommand = sqlcommand
.Fill(Table)
End With
'---THIS IS WHAT I ADDED
Dim combo As New DataGridViewComboBoxColumn
Dim row As DataRow
With combo
For Each row In Table.Rows
.Items.Add(row.Item("subject_to_teach").ToString)
.Name = "Teaching Subject"
Next
End With
datagrid_Teacher.Columns.Add(combo)
'-----------------------
datagrid_Teacher.Rows.Clear()
For x As Integer = 0 To Table.Rows.Count - 1
datagrid_Teacher.Rows.Add(Table(x)("id"),
Table(x)("lname"),
Table(x)("fname"),
Table(x)("mname"),
Table(x)("subject_to_teach"))
Next
'Bold header
With datagrid_Teacher.ColumnHeadersDefaultCellStyle
.Font = New Font(datagrid_Teacher.Font, FontStyle.Bold)
End With
Catch ex As Exception
MessageBox.Show("ERROR: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
and this
Private Sub datagrid_Teacher_DataError(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewDataErrorEventArgs) Handles datagrid_Teacher.DataError
'ERROR HANDLER
End Sub
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.