VB.Net和Sql Server数据库中的Gridview显示问题 [英] Gridview display problem in VB.Net and Sql Server database

查看:70
本文介绍了VB.Net和Sql Server数据库中的Gridview显示问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我的问题是,查询正确地获取了行数,但是当我将查询结果与gridview绑定时,它会显示两倍的否.结果集中实际存在的行数,第一行仅显示主键值,第二行显示所有字段值.
例如我有一个雇员表,并且表中有两行.但是在将结果集与Gridview绑定后,它显示4行,前2行仅显示员工ID,接下来的2行显示应显示的实际2行.当我在数据库上执行查询时,我的查询工作正常,但是在应用程序端出现问题.

我的代码是这样的.

ASP.NET页:

Hi,
My problem is that, the query is correctly fetching the number of rows but when i bind the query result with the gridview, it displays double the no. of rows actually present in the resultset with the first row showing just the primary key value and the next row displaying all the field values.
for e.g. I have an employee table and I have two rows in table. But after I bind the resultset with the Gridview it shows 4 rows,the first 2 rows display only the employee ids and the next 2 rows display the actual 2 rows that it is supposed to display. My query works fine when I execute it on the Database but there is problem at application end.

My code goes something like this.

ASP.NET Page:

<tr>
     <td style="width: 90px">
         <asp:GridView ID="gv" runat="server">
         </asp:GridView>
                </td>
     <td style="width: 118px">
     </td>
</tr>



背后的代码:



Codebehind:

Protected Sub PopulateGridView()
        gv.DataSource = oDb.ListEmployeeDetails()
        gv.DataBind()


 Public Function ListEmployeeDetails() As DataTable

        command.Connection = OpenConn()
        command.CommandType = Data.CommandType.Text
        command.CommandText = "Select emp_id,emp_name,address,doj,dept_id,proj_id from Employee"
        reader = command.ExecuteReader()
        If (reader.HasRows) Then
            dt.Load(reader)
        End If
        Return dt

    End Function


Public Function OpenConn() As SqlConnection
        conn = New SqlConnection(Connection())
        Try
            If (conn.State = System.Data.ConnectionState.Closed) Then
                conn.Open()
                Return conn
            End If
        Catch ex As Exception
            Return Nothing
        End Try
        Return Nothing
    End Function



 Public ReadOnly Property Connection() As String
        Get
            Dim s As String
            s = ConfigurationManager.ConnectionStrings("DbConn").ConnectionString
            Return s
        End Get
    End Property

    End Sub

推荐答案

您不需要在表格中设置网格,因为默认情况下它会渲染表格!
you don''t need to set grid in to a table , because it render a table by default!


在gridview中使用模板字段
use template field in gridview


这篇关于VB.Net和Sql Server数据库中的Gridview显示问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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