从sql表中填写数据网格视图 [英] Fill data grid view from sql table

查看:113
本文介绍了从sql表中填写数据网格视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Datagridview 中有四列。我想用SQL数据库中的数据填充前两列。我尝试填写 Datagridview 。它不显示数据,但它生成行。

I have four columns in Datagridview. I want to fill first two columns with data from sql database. I try to fill Datagridview. It not display data, but it generate rows.

这是我的代码:

getConnect()
    Try
        Conn.Open()
        Dim strSQL As String = "SELECT EMP_ID, EMP_NAME FROM EMPLOYEE ORDER BY EMP_NAME ASC"
        Conn.Close()
        Dim da As New SqlDataAdapter(strSQL, Conn)
        Dim dt As New DataTable("EMPLOYEE")
        da.Fill(dt)
        ATCGRID.DataSource = dt
    Catch ex As SqlException
        MsgBox(ex.Message, MsgBoxStyle.Critical, "SQL Error")
    Catch ex As Exception
        MsgBox(ex.Message, MsgBoxStyle.Critical, "General Error")
    End Try

请检查我的代码,并给我解决方案...

Please check my code and give me solution...

推荐答案

尝试此代码

getConnect()
Try
    Conn.Open()
    Dim strSQL As String = "SELECT EMP_ID, EMP_NAME FROM EMPLOYEE ORDER BY EMP_NAME ASC"
    Conn.Close()
    Dim da As New SqlDataAdapter(strSQL, Conn)
    Dim ds As new Dataset
    da.Fill(ds,"EMPLOYEE")
    ATCGRID.DataSource = ds.tables(0)
Catch ex As SqlException
    MsgBox(ex.Message, MsgBoxStyle.Critical, "SQL Error")
Catch ex As Exception
    MsgBox(ex.Message, MsgBoxStyle.Critical, "General Error")
End Try

这篇关于从sql表中填写数据网格视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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