I am creating a website using ASP.net with C# 2005 as the language.

In my ASP.net page, I have displayed a GridView. I have dynamically populated the GridView during runtime using Dataset and SQLDataAdapter.

Since the GridView displays ALL columns at a fixed width, it does not look pretty to me (even after applying AutoFormat style). I need to hide a few columns, change the width of a few columns and change the heading of ALL columns. I know, I can solve the first requirement of displaying only selected columns by using a Select statement with the required fieldnames only (as against Select *), but I do not know how to change the Column width and change the Column header.

I tried using the syntax like,

GridView1.Columns[0].visible = false;

GridView1.Columns[1].ItemStyle.Width = 50;

GridView1.Columns[2].HeaderText = "Result Time";

but it gave me error messages like "index was out of range".

Can anybody please help me?

Thank you.

Lalit Kumar Barik

the problem can be this, when you set one server-side element's visibility to false, that element's html markup is not sent to client browser, which basically means that that element does not exist any more, meaning that your column count changes, maybe instead of setting the column index as one and two you can try zero and one after setting the visibility to false in the first line. i will try this momentarily.

man i found the problem, though not the solution :

Default.aspx:

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" 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>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server">
        </asp:GridView>
    
    </div>
    </form>
</body>
</html>

Default.aspx.cs :

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("column1");
        dt.Columns.Add("column2");
        dt.Columns.Add("column3");
        dt.Rows.Add(new object[] {1,2,3 });
        dt.Rows.Add(new object[] {4,5,6 });
        
        GridView1.DataSource = dt;
        
        GridView1.DataBind();
        Response.Write(GridView1.Columns.Count);

        
    }
}

you see that column count is zero, so you should somehow tell the gridview how many columns it has after binding, i will try to find it.

Thank you, "Serkan Sendur" for your replies. Yes, I agree with your last comment. But it should not be so and there should be an easier implementation. I am going through the link given by you and try to implement it.

Thank you very much for your reply and time.

Lalit Kumar Barik
India

using the link that i gave you, i created a web custom control, and it worked good except for the headertext, i am gonna contact the author to ask about it. here i am attaching the dll, all you need to do is right click your toolbox and choose items then browse to dll i provided and that is it, GridViewExt will be added to your toolbox, then use it the way i use in the sample.

Default.aspx :

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

<%@ Register Assembly="GridviewExt" Namespace="GridviewExt" TagPrefix="cc1" %>

<!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>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <cc1:gridviewext id="GridViewExt1" runat="server"></cc1:gridviewext>
    
    </div>
    </form>
</body>
</html>

Default.aspx.cs :

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("first");
        dt.Columns.Add("second");
        dt.Columns.Add("third");
        dt.Rows.Add(new object[] { 1, 2, 3 });
        dt.Rows.Add(new object[] { 4, 5, 6 });
        GridViewExt1.DataSource = dt;
        GridViewExt1.DataBind();
        GridViewExt1.BoundColumns[0].Visible = false;
        GridViewExt1.BoundColumns[1].ItemStyle.Width = 100;
    }
}

tell me the results please.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.