rupinder18
rupinder18

Reputation: 815

GridView visibility on first time loading of page

I am working on a project,in that I have a search option and submit button.On submission the gridview gets visible if there is some record corresponding to the selection.But the problem I am facing is when the page is loaded for the first time the gridview doesnot appear even if it has data but while debugging I found data is loaded in it,but on second selection the gridview becomes visible.

The following screenshot shows my page

Following is the code on submit button click

enter image description here

protected void btnsubmitclass_OnClick(object sender,EventArgs e)
{
    if(drpfromclass.SelectedIndex>0 && drptoclass.SelectedIndex>0)
    {
        ds = objpromote.selectclasswise(ConfigurationManager.AppSettings["schoolcode"].ToString(),drpfromclass.SelectedItem.Value,drptoclass.SelectedItem.Value);
        if(ds.Tables[0].Rows.Count>0)
        {
            foreach(GridViewRow gr in grdstudents.Rows)
            {
                grdstudents.Columns[0].Visible = true;
                grdstudents.Columns[1].Visible = true;
                grdstudents.Columns[2].Visible = false;
                grdstudents.Columns[3].Visible = false;
                grdstudents.Columns[4].Visible = true;
            }
            grdstudents.DataSource = ds;
            grdstudents.DataBind();
            grdstudents.Visible = true;
            lblheading.Text = "Showing list of students moved from " + drpfromclass.SelectedItem.Text + " to " + drptoclass.SelectedItem.Text;
            pnlgrd.Visible = true;
        }
        else
        {
            pnlgrd.Visible = false;
            lblalerts.Text = "No record found";
            lblalerts.Visible = true;
            divalerts.Visible = true;
        }
        drpfromclass.ClearSelection();
        drptoclass.ClearSelection();
    }
    else
    {
        lblalerts.Text = "Select fromclass and toclass";
        lblalerts.Visible = true;
        divalerts.Visible = true;
        pnlgrd.Visible = false;
    }
}

Following is the aspx code

<asp:Panel ID="pnlgrd" runat="server" Visible="false">
            <div style="width:700px;height:30px;" class="blueheadingdiv"><asp:Label ID="lblheading" runat="server" Text="Label" CssClass="lblheading"></asp:Label></div>
        <div style="width:610px; height:500px;margin-top:10px; overflow-y:scroll;">
        <asp:GridView ID="grdstudents" runat="server"  Width="590"  ForeColor="#333333" BorderColor="#6abb00" GridLines="None" AutoGenerateColumns="False" CellPadding="5">
    <Columns>
       <asp:BoundField HeaderText="Student Code" DataField="studentcode" Visible="false"/>
       <asp:BoundField HeaderText="Student Name" DataField="studentname" Visible="false"/>
       <asp:BoundField HeaderText="From Class" DataField="fromclass" Visible="false"/>
       <asp:BoundField HeaderText="To Class" DataField="toclass" Visible="false"/> 
       <asp:BoundField HeaderText="Date" DataField="date" Visible="false"/>            
    </Columns>
    <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
        <EditRowStyle BackColor="#999999" />
        <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#4382EB" Font-Bold="True" ForeColor="White" CssClass="gridviewheader" HorizontalAlign="Left"/>
        <PagerStyle BackColor="#4382EB" Font-Bold="True" ForeColor="White" CssClass="gridviewheader" HorizontalAlign="Center" />
        <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
        <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
        <SortedAscendingCellStyle BackColor="#E9E7E2" />
        <SortedAscendingHeaderStyle BackColor="#506C8C" />
        <SortedDescendingCellStyle BackColor="#FFFDF8" />
        <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
        </div>
        </asp:Panel>

Pageload code is shown below

protected void Page_Load(object sender, EventArgs e)
{
    if(!IsPostBack)
    {
        filldrpclass();
    }
}

Upvotes: 0

Views: 3623

Answers (3)

VISHMAY
VISHMAY

Reputation: 709

I Dont Know What you have done in your code but i know your problems.

Problem 1. When Parent Gridview is visible false than rows & columns remains visible falser even if you set grdstudents.Columns[0].Visible = true; it is false Problem 2

 foreach(GridViewRow gr in grdstudents.Rows)
        {
            grdstudents.Columns[0].Visible = true;
            grdstudents.Columns[1].Visible = true;
            grdstudents.Columns[2].Visible = false;
            grdstudents.Columns[3].Visible = false;
            grdstudents.Columns[4].Visible = true;
        }

There is no need of foreach why are you wasting your CPU time. remove foreach and braces keep code within

Problem 3. Main Problem for you Change Code from

 foreach(GridViewRow gr in grdstudents.Rows)
        {
            grdstudents.Columns[0].Visible = true;
            grdstudents.Columns[1].Visible = true;
            grdstudents.Columns[2].Visible = false;
            grdstudents.Columns[3].Visible = false;
            grdstudents.Columns[4].Visible = true;
        }
        grdstudents.DataSource = ds;
        grdstudents.DataBind();

To

        grdstudents.Visible = true;
        grdstudents.Columns[2].Visible = true;
        grdstudents.Columns[3].Visible = true;
        grdstudents.DataSource = ds;
        grdstudents.DataBind();
        grdstudents.Columns[0].Visible = ;
        grdstudents.Columns[1].Visible = true;
        grdstudents.Columns[2].Visible = false;
        grdstudents.Columns[3].Visible = false;
        grdstudents.Columns[4].Visible = true;

Your Grid will be visible Problem was Grid visible=true should be before databind

Upvotes: 2

rupinder18
rupinder18

Reputation: 815

I have modified my code earlier I was setting the visibilty of gridview columns from codebehind but now I have included only the required columns.I am updating both my aspx code and cs code

aspx code

<asp:Panel ID="pnlgrd" runat="server" Visible="false">
            <div style="width:700px;height:30px;" class="blueheadingdiv"><asp:Label ID="lblheading" runat="server" Text="Label" CssClass="lblheading"></asp:Label></div>
        <div style="width:610px; height:500px;margin-top:10px; overflow:auto;">
        <asp:GridView ID="grdstudents" runat="server"  Width="590"  ForeColor="#333333" BorderColor="#6abb00" GridLines="None" CellPadding="5">       
    <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
        <EditRowStyle BackColor="#999999" />
        <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#4382EB" Font-Bold="True" ForeColor="White" CssClass="gridviewheader" HorizontalAlign="Left"/>
        <PagerStyle BackColor="#4382EB" Font-Bold="True" ForeColor="White" CssClass="gridviewheader" HorizontalAlign="Center" />
        <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
        <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
        <SortedAscendingCellStyle BackColor="#E9E7E2" />
        <SortedAscendingHeaderStyle BackColor="#506C8C" />
        <SortedDescendingCellStyle BackColor="#FFFDF8" />
        <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
        </div>
        </asp:Panel>

cs code

protected void btnsubmitclass_OnClick(object sender,EventArgs e)
{
    if(rbchoice.SelectedIndex==0)
    {
    if(drpfromclass.SelectedIndex>0 && drptoclass.SelectedIndex>0)
    {
        ds = objpromote.selectclasswise(ConfigurationManager.AppSettings["schoolcode"].ToString(),drpfromclass.SelectedItem.Value,drptoclass.SelectedItem.Value);
        if(ds.Tables[0].Rows.Count>0)
        {
            grdstudents.Visible = true;
            grdstudents.DataSource = ds;
            grdstudents.DataBind();
            lblheading.Text = "Showing list of students moved from " + drpfromclass.SelectedItem.Text + " to " + drptoclass.SelectedItem.Text;
            pnlgrd.Visible = true;
        }
        else
        {
            pnlgrd.Visible = false;
            lblalerts.Text = "No record found";
            lblalerts.Visible = true;
            divalerts.Visible = true;
        }
        drpfromclass.ClearSelection();
        drptoclass.ClearSelection();
    }
    else
    {
        lblalerts.Text = "Select fromclass and toclass";
        lblalerts.Visible = true;
        divalerts.Visible = true;
        pnlgrd.Visible = false;
    }
    }
    else
    {
        if (drpclass.SelectedIndex > 0 && drpstudent.SelectedIndex > 0)
    {
        ds = objpromote.selectstudentwise(ConfigurationManager.AppSettings["schoolcode"].ToString(),drpstudent.SelectedItem.Value);
        if (ds.Tables[0].Rows.Count > 0)
        {
            grdstudents.Visible = true;
            grdstudents.DataSource = ds;
            grdstudents.DataBind();
            lblheading.Text = "Showing detailed list of "+drpstudent.SelectedItem.Text+" of class- "+drpclass.SelectedItem.Text;
            pnlgrd.Visible = true;
        }
        else
        {
            lblalerts.Text = "No record found";
            lblalerts.Visible = true;
            divalerts.Visible = true;
            pnlgrd.Visible = false;
        }
        drpclass.ClearSelection();
        drpstudent.ClearSelection();
    }
    else
    {
        lblalerts.Text = "Select fromclass and toclass";
        lblalerts.Visible = true;
        divalerts.Visible = true;
        pnlgrd.Visible = false;
    }
    }
}

Upvotes: 0

Zo Has
Zo Has

Reputation: 13028

There is no point in setting visibility of your gridview to false as with no records it would display no data at all. What I can understand is that on initial pageLoad, you specify the visibility after DataBind event. For some odd reason, this fails to display the data. Now the grid being visible with first attempt, data is displayed on the second attempt.

Try removing the visible property from your code & set visibility to true in your aspx page.

        grdstudents.DataSource = ds;
        grdstudents.DataBind();
        //grdstudents.Visible = true;    

Also, there is no need to show messages in a label control when you can utilize GridView's EmptyDataTemplateProperty and EmptyDataText properties.

Here is a working example without visibility or panels. No scrollbars are shown when grid is empty.

Aspx

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="gridtest.aspx.cs" Inherits="gridtest" %>

    <!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></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <br />
            <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Search Records" />
            <br />
            <div style="width: 100%; height: 100px; width: 400px; overflow: auto">
                <asp:GridView ID="GridView1" runat="server" Width="400px">
                </asp:GridView>
            </div>
        </div>
        </form>
    </body>
    </html>    

Codebehind

     public class Customer
    {
        public int CustId { get; set; }
        public string CustomerName { get; set; }
        public string Address { get; set; }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        List<Customer> customers= new List<Customer>()
        {
            new Customer {CustId=1 ,CustomerName= "John Doe", Address= "ACME Street"},
            new Customer {CustId=2 ,CustomerName= "David Bowie", Address= "abcd lane"},
            new Customer {CustId=3 ,CustomerName= "Sarah Lynn", Address= "House 84"},
            new Customer {CustId=1 ,CustomerName= "John Doe", Address= "ACME Street"},
            new Customer {CustId=2 ,CustomerName= "David Bowie", Address= "abcd lane"},
            new Customer {CustId=3 ,CustomerName= "Sarah Lynn", Address= "House 84"}
        };

        GridView1.DataSource = customers;
        GridView1.DataBind();
    }

Sample

Upvotes: 1

Related Questions