在Datagridview上自动输入

时间:2015-11-27 05:53:21

标签: .net vb.net datagridview

我有一个我无法解决的问题,所以我需要问一下

**问题:* 我有一个数据网格视图,并为它制作了自定义列。我在IDB列上输入数据,我希望列名,价格可以在行中自动输入,之后我可以手动输入金额,总金额为*价格

Dim dt As New DataTable()
dt.Columns.AddRange(New DataColumn(4) {New DataColumn("IDB", GetType(String)), New DataColumn("Name", GetType(String)), New DataColumn("Price", GetType(Integer)), New DataColumn("Amount", GetType(Integer)), New DataColumn("Total", GetType(Integer))})
Me.MetroGrid1.DataSource = dt

我必须手动输入一些IDB到datagridview,它会自动从mysql数据库中获取数据,我该怎么做?

我试过这个

Private Sub inputdata(sender As Object, e As DataGridViewCellEventArgs) Handles MetroGrid1.CellValueChanged
        Dim dgrow As Integer = 0


        Try
            If MetroGrid1.Rows(baris).Cells("IDB").Value Then
                mysqlConnection.ConnectionString = ServerString
                mysqlConnection.Open()
                Dim bc As MySqlDataReader
                cmdmysql.CommandText = "SELECT * FROM product where IDB = '" + MetroGrid1.Rows(dgrow).Cells("IDB").Value + "'"
                cmdmysql.Connection = mysqlConnection
                bc = cmdmysql.ExecuteReader
                If bc.HasRows Then
                    bc.Read()
                    MetroGrid1.Rows(dgrow).Cells("Name").Value = bc.Item(3)
                    MetroGrid1.Rows(dgrow).Cells("Price").Value = bc.Item(4)
                    Label3.Text = bc.Item(3)
                    Label4.Text = bc.Item(4)

                End If
            End If
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub

它可以自动输入第一个数据,但它会说一些错误

  

MySql.Data.MySqlClient.MySqlException(0x80004005):不允许   更改' ConnectionString'财产,而连接   (状态=开)。在   MySql.Data.MySqlClient.ExceptionInterceptor.Throw(异常异常)   at MySql.Data.MySqlClient.MySqlConnection.Throw(Exception ex)at at   MySql.Data.MySqlClient.MySqlConnection.set_ConnectionString(字符串   值)在SCS_MetroDesign.Pembayaran.gantidata(对象发送者,   DataGridViewCellEventArgs e)in:第69行

第69行:mysqlConnection.ConnectionString = ServerString

当我没有使用MetroGrid1.Rows(dgrow).Cells(" Name")时没有错误。值= bc.Item(3),但没有这个我仅使用IDB

自动输入

请帮我解决这个问题,提前致谢

2 个答案:

答案 0 :(得分:0)

我建议先在dataTable或dataset中保存数据,然后从dataTable或数据集中获取数据。

示例:

DataTable dt = new DataTable();
dt.Load(bc);

然后使用dt获取数据。

答案 1 :(得分:0)

您的变量mysqlConnection在项目的其他位置定义,并且它的状态是从另一个连接打开的。仔细检查你编程的Mysql-connections,使用后没有关闭。

或者要快速修复,请添加

进行检查 在mysqlConnection.close()

之前

mysqlConnection.ConnectionString = ServerString

希望你需要输入。