HOW CAN I ADD A COLUMN NAME TO MY GRID?

mohan kumar

Total Post:10

Points:70
Posted by  mohan kumar
C# 
C#
 1605  View(s)
Ratings:
Rate this:
How can i add a column name to my Grid when i populate my grid control from a list of array values?
This is my code snippet..

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            loadMyGrid();
        }
    }

    public void loadMyGrid()
    {
        string[] stuff = new string[] {"Mohan","Arul Prakash","Lakshman","Ravindran","Senthil","Nagaraj","Bala","Suresh","Ashok" };
       
        //GridView1.Columns[0].HeaderText = "My Customized Grid Control";
        GridView1.DataSource = stuff;
        GridView1.DataBind();
    }
}

When my above code is executed, it is displaying the following error.
"Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index"
Can anyone there to help me???

Thanks in advance...
  1. Arun Singh

    Post:68

    Points:476
    Re: How can i add a column name to my Grid?

    Hi Mohan Kumar,

    I had modified your code which works as you want..

    Please check this code and let me know if further any problem.

    public partial class _Default : System.Web.UI.Page

    {

        protected void Page_Load(object sender, EventArgs e)

        {

            if (!IsPostBack)

            {

                loadMyGrid();

            }

        }

        public void loadMyGrid()

        {

            string[] stuff = new string[] { "Mohan", "Arul Prakash", "Lakshman", "Ravindran", "Senthil", "Nagaraj", "Bala", "Suresh", "Ashok" };

       GridView1.DataSource = stuff;

      GridView1.DataBind();

      }

      protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)

      {

      if (e.Row.RowType == DataControlRowType.Header)

      {

    e.Row.Cells[0].Text = "My Customized Grid Control";

      }

    }

    }

     

     

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!