Click here to Skip to main content
16,004,761 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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:
XML
<%@ 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>

for vb codes:
VB
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

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 ^_^
Posted
Updated 24-May-11 15:10pm
v5
Comments
janwel 24-May-11 21:43pm    
sirs i think there a solution regarding this matter. im reading how to hide columns in gridview

1 solution

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

Example:

SQL
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

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


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

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900