Home > DeveloperSection > Forums > How can i add a column name to my Grid?
mohan kumar

Total Post:10

Points:70
Posted on    December-21-2011 5:31 PM

 C# C# 
Ratings:


 1 Reply(s)
 1389  View(s)
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...


Arun Singh

Total Post:68

Points:476
Posted on    December-22-2011 1:14 AM

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";

  }

}

}

 

 


Don't want to miss updates? Please click the below button!

Follow MindStick