我有一个datagridview(有5列:4个文本框和1个组合框),它从mysql中加载数据。这是我使用的代码,当我在所有textbox列中尝试它时,它工作正常。但当我将最后一列更改为组合框时,我收到一条错误消息:
" DataGridView中出现以下异常:
System.ArgumentException:DataGridViewComboBoxCell值无效。
要替换此默认对话框,请处理DataError事件。"
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
编辑:这里的问题是在datagridview中添加数据(组合框(subject_to_teach列))
答案 0 :(得分:0)
尝试使用此方法而不是datagrid_Teacher.Rows.Add方式:
datagrid_Teacher.Datasource = Table;
datagrid_Teacher.Databind();
或者在你的代码上添加它(Gridview Dataerror的事件处理程序):
private void ctrldataGridView_DataError(object sender, DataGridViewDataErrorEventArgs e)
{
//do nothing
}
答案 1 :(得分:0)
我设法通过删除设计中的组合框列并在代码中添加组合框列,并将我的代码修改为:
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
和这个
Private Sub datagrid_Teacher_DataError(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewDataErrorEventArgs) Handles datagrid_Teacher.DataError
'ERROR HANDLER
End Sub