自动将数据输入到Datagridview中. [英] Automatic input data into Datagridview.

查看:68
本文介绍了自动将数据输入到Datagridview中.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

开发人员!

请帮助我创建VB.net编码.我有DataGridview名称"dgvCharges",带有六个列的代码",费用",说明",数量",单价",金额",和SQL数据库表 "tblCharges"在代码",费用",说明",数量",单价",金额"列中具有数据.

Please help me to create VB.net coding. I have the DataGridview name "dgvCharges" with six columns "Code","Charges","Description","Quantity","Unit Price","Amount" and SQL Database table "tblCharges" with a data in column "Code","Charges","Description","Quantity","Unit Price","Amount".

我希望用户在代码"列的每个第一行中输入一个值.可能像"10001或10002"其具有来自"tblCharges"的等效数据.然后,在将值输入到每一行之后,在"Enter"栏中输入"Enter".钥匙.其他数据 来自"tblCharges"的列还将自动输入到datagridview的列中.

I want the users to input a value into each first row of column "Code" probably like "10001 or 10002" which have equivalent data from "tblCharges". Then after the value inputs to each row, "Enter" key. The other data of columns from "tblCharges" will also automatic input into columns of datagridview.

请有人帮我解决这个问题.感谢您的解决方案和答案.谢谢!

Please someone help me through with this. Appreciate your solution and answer. Thank you!

推荐答案

Jaron2016

Hi Jaron2016,

感谢您在MSDN论坛中发帖.

Thank you for posting in MSDN Forum.

根据您的描述,我建议您可以使用

According to your description, I suggest that you could use CellEndEdit event in DataGridView to achieve the value of current cell. Then reads a row in the database that matches the current cell. Please check and refer the following sample demo (I used Access database in this case, but it same to SQL database):

Private Sub DataGridView1_CellEndEdit(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellEndEdit
        If e.ColumnIndex = 0 Then
            Dim ConnStr As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Documents\Database1.accdb;Persist Security Info = False;"
            Dim Conn As OleDbConnection = New OleDbConnection(ConnStr)
            Dim com As OleDbCommand = New OleDbCommand("SELECT * FROM Table1 Where ID=" & DataGridView1.CurrentCell.Value, Conn)
            Conn.Open()
            Dim da As OleDbDataAdapter = New OleDbDataAdapter(com)
            Dim ds As DataSet = New DataSet()
            da.Fill(ds)
            If ds.Tables(0).Rows.Count > 0 Then
                Dim dr As DataRow = ds.Tables(0).Rows(0)
                DataGridView1.CurrentRow.Cells(1).Value = dr(1)
                DataGridView1.CurrentRow.Cells(2).Value = dr(2)
            End If
        End If
    End Sub

希望是有帮助的.

最好的问候


这篇关于自动将数据输入到Datagridview中.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆