gpt4 book ai didi

mysql - 如何从 mysql 填充 datagridview 组合框 - vb.net

转载 作者:行者123 更新时间:2023-11-29 22:30:42 24 4
gpt4 key购买 nike

我有一个 datagridview(有 5 列:4 个文本框和 1 个组合框),它从 mysql 加载数据。这是我使用的代码,当我在所有文本框列中尝试它时,它工作正常。但是当我将最后一列更改为组合框时,出现错误:

“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 列))中添加数据

最佳答案

尝试这个而不是 datagrid_Teacher.Rows.Add 方式:

datagrid_Teacher.Datasource = Table;
datagrid_Teacher.Databind();

或者将其添加到您的代码中(Gridview Dataerror 的事件处理程序):

private void ctrldataGridView_DataError(object sender, DataGridViewDataErrorEventArgs e)
{
//do nothing
}

关于mysql - 如何从 mysql 填充 datagridview 组合框 - vb.net,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29819587/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com