Set column width of a gridview in asp.net with code example

In ASP.NET web development, the GridView control is a powerful tool for displaying data in tabular format. One common requirement is to customize the column widths for better presentation. This step-by-step guide will show you how to set column widths in a GridView effortlessly.

Step-by-Step Tutorial

  1. Create a New ASP.NET Web Forms Project

    • Start by creating a new ASP.NET Web Forms project in Visual Studio.
  2. Add GridView Control to Your ASPX Page

    • Open your ASPX page and add a GridView control to it.
  3. Bind Data to the GridView

    • You can bind data to the GridView either using a DataSource control or programmatically.
  4. Set Column Widths in the GridView Markup or Code-Behind

    • In the markup, use the ItemStyle-Width attribute within the <asp:BoundField> tags to set the width of each column.
    • Alternatively, you can set column widths dynamically in the code-behind file during data binding.

Code Example

Default.aspx:

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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
                <Columns>
                    <asp:BoundField DataField="ID" HeaderText="ID" ItemStyle-Width="100px" />
                    <asp:BoundField DataField="Name" HeaderText="Name" ItemStyle-Width="200px" />
                    <asp:BoundField DataField="Age" HeaderText="Age" ItemStyle-Width="80px" />
                </Columns>
            </asp:GridView>
        </div>
    </form>
</body>
</html>

Default.aspx.cs:

using System;
using System.Collections.Generic;
using System.Web.UI;

namespace GridViewColumnWidthExample
{
    public partial class _Default : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindGridView();
            }
        }

        private void BindGridView()
        {
            // Example data
            List<Employee> employees = new List<Employee>
            {
                new Employee { ID = 1, Name = "John Doe", Age = 30 },
                new Employee { ID = 2, Name = "Jane Smith", Age = 25 },
                new Employee { ID = 3, Name = "Bob Johnson", Age = 35 }
            };

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

        public class Employee
        {
            public int ID { get; set; }
            public string Name { get; set; }
            public int Age { get; set; }
        }
    }
}

Customizing column widths in an ASP.NET GridView is straightforward and enhances the presentation of your data. By following this guide, you can easily control the width of each column to suit your requirements, improving the overall user experience of your web application.

 
Best quality Asp .Net Ajax Control Toolkit tutorials.

Give your valuable comments.

Name
Email
Comment
5 + 1 =
 

About Us | Terms of Use | Privacy Policy | Disclaimer | Contact Us Copyright © 2012-2024 CodingFusion
50+ C# Programs for beginners to practice