Gridview表带有一个下拉菜单以显示数据 [英] Gridview table with a dropdown to show data

查看:55
本文介绍了Gridview表带有一个下拉菜单以显示数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,程序员在这里遇到了问题,因为您看到我在数据库上创建的字段名称,即名称,地址,年龄,联系人,部门,课程,公司
现在我的老师想要的是在gridview上冻结名称,而有一个下拉列表只有两个选项(个人,工作)
当我单击个人时,它只会显示[姓名(冻结),地址,年龄,联系方式]
当我单击工作时,它将仅显示[名称(冻结),部门,课程,公司]
关于此事有任何教程吗?.(先生,女士)
我可以冻结标题和列(通过CSS),但是我想不出任何可能的解决方案.
我的考验:
我认为ive遇到了问题,因为ive声明了所有模板,并且当我对它们进行排序(我认为)时,它还会显示其他模板的标题.

谢谢您,还有先生和女士,谢谢.
关于一个脑袋会炸毁的学生^ _ ^
请原谅我的语法

这是我的试用代码:
用于源代码:

hello programmers, got a problem here as you see ive created field names on database namely name, address, age, contact, department, course, company
now my teacher wants is that the name is freeze on gridview while there is a dropdownlist with just two choices (personal,work)
when i click the personal it will just show [name(freeze),address,age,contact]
and when i click work it will just show [name(freeze),department,course,company]
is there any tutorial regarding this matter?.(sir''s madamme''s)
i could freeze the header and the colummns [via css] but i cant think of any possible solution regarding this matter.
my trials:
I think ive got a problem since ive declared all of them template and when i sort them (i think) it will also show the header of others.

Thanks and more power sir and madamme.
regards to a student whos brain is gonna blow up ^_^
plss excuse my grammars

this is my trial codes:
for source code:

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>

    <style type="text/css">
        /* A scrolable div */
        .GridViewContainer
        {
            overflow: auto;
        }
        /* to freeze column cells and its respective header*/
        .FrozenCell
        {
            background-color:yellow;
            position: relative;
            cursor: default;
            left: expression(document.getElementById("GridViewContainer").scrollLeft-2);
        }

        /* for freezing column header*/
        .FrozenHeader
        {
         background-color:yellow;
            position: relative;
            cursor: default;
            top: expression(document.getElementById("GridViewContainer").scrollTop-2);
            z-index: 10;
        }
        /*for the locked columns header to stay on top*/
        .FrozenHeader.locked
        {
            z-index: 99;
        }

    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <div style="left: 32px; overflow: auto; width: 1030px; position: relative; top: 225px;
            height: 428px">
            <asp:GridView ID="GridView1" runat="server" Height="408px" Width="1006px">
            <HeaderStyle CssClass="FrozenHeader" />
            <Columns>
            <asp:BoundField DataField="name" HeaderText="Name" >
                       <HeaderStyle CssClass="FrozenCell" />
                       <ItemStyle CssClass="FrozenCell" />
                   </asp:BoundField>
            <asp:BoundField DataField="address" HeaderText="Address" ></asp:BoundField>
             <asp:BoundField DataField="age" HeaderText="Age" ></asp:BoundField>
             <asp:BoundField DataField="contact" HeaderText="Contact" ></asp:BoundField>
             <asp:BoundField DataField="department" HeaderText="Department" ></asp:BoundField>
             <asp:BoundField DataField="course" HeaderText="Course" ></asp:BoundField>
             <asp:BoundField DataField="company" HeaderText="Company" ></asp:BoundField>
            </Columns>

            </asp:GridView>
        </div>

    </div>
        <div style="left: 372px; width: 286px; position: relative; top: -268px; height: 29px">
            <asp:DropDownList ID="DropDownList1" runat="server" Width="283px" AutoPostBack="True">
                <asp:ListItem>Choose here..</asp:ListItem>
                <asp:ListItem>personal</asp:ListItem>
                <asp:ListItem>work</asp:ListItem>
            </asp:DropDownList></div>
    </form>
</body>
</html>


对于vb代码:


for vb codes:

Imports System.Math
Imports System.Web
Imports System.Data.SqlClient
Imports System.Data
Partial Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If DropDownList1.SelectedValue = "personal" Then
            personalData()
        ElseIf DropDownList1.SelectedValue = "work" Then
            workData()
        Else
            BindData()
        End If
    End Sub
    Public Sub BindData()
        Dim strSQL As String
        Dim connection As SqlConnection = New SqlConnection("Data Source=ML0003135586;Initial Catalog=TestSQL;Integrated Security=True")
        strSQL = "SELECT * FROM [tryingtable]"
        connection.Open()
        Dim command As New SqlCommand(strSQL, connection)
        GridView1.DataSource = command.ExecuteReader
        GridView1.DataBind()
    End Sub
    Public Sub workData()
        Dim strSQL As String
        Dim connection As SqlConnection = New SqlConnection("Data Source=ML0003135586;Initial Catalog=TestSQL;Integrated Security=True")
        strSQL = "SELECT [name],[department],[course],[company] FROM [tryingtable]"
        connection.Open()
        Dim command As New SqlCommand(strSQL, connection)
        GridView1.DataSource = command.ExecuteReader
        GridView1.DataBind()
    End Sub
    Public Sub personalData()
        Dim strSQL As String
        Dim connection As SqlConnection = New SqlConnection("Data Source=ML0003135586;Initial Catalog=TestSQL;Integrated Security=True")
        strSQL = "SELECT [name],[address],[age],[contact] FROM [tryingtable]"
        connection.Open()
        Dim command As New SqlCommand(strSQL, connection)
        GridView1.DataSource = command.ExecuteReader
        GridView1.DataBind()
    End Sub
End Class


就像我说的那样,有一个错误,指出模板必须具有数据,例如关于workdata
A field or property with the name ''address'' was not found on the selected data source.
反之亦然^ _ ^


like I said there is an error saying the template must have a data for example on workdata
A field or property with the name ''address'' was not found on the selected data source.
and vice versa ^_^

推荐答案

好,我已经找到了必须在每个select语句中声明在gridview中创建的列的解决方案.
示例:

ok i already find the solution you have to declare the column you made in your gridview in every select statement.

Example:

strSQL = "SELECT [name],[address],[age],[contact] FROM [tryingtable]"



正如您在声明中看到的那样,您不包括部门,课程和公司.因此,您必须采用这种方式



as you can see in your statement you did not include the department,course and company. So you have to make it this way

strSQL = "SELECT [name],[address],[age],[contact],'' as department,'' as course, '' as company FROM [tryingtable]"



确保您的gridview中所有定义的列都必须包含在select语句中.



Make sure that all the defined column in your gridview must be included in your select statement.


这篇关于Gridview表带有一个下拉菜单以显示数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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