Sponsored By
MindStick Cleaner
Advertise with Us
Advertisement
Advertise with Us
Follow Us
Follow _MindStick_ on Twitter View MindStick Software's LinkedIn profile View MindStick Software's Facebook profile
Mindstick
Article Article  Forum Forum  Blog Blog  Quiz Quiz  Beginner Beginner  Careers Careers  Contact Contact  Login Login  
Home | Product | Services | About Us | Interview | DeveloperSection | Submit an Article | Submit Blog
Report Abuse Form
Reason:    
   

Home >> .Net >> C# >> How can i add a column name to my Grid?
Author Post

mohan kumar


Total Post: 10
Member Since: 10/19/2011
Points: 51
How can i add a column name to my Grid?
Posted On: 12/21/2011 5:31:09 PM
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...
Modified On: 12/21/2011 5:33:22 PM
Author Post

Arun Singh


Total Post: 48
Member Since: 7/1/2011
Points: 331
Re: How can i add a column name to my Grid?
Posted On: 12/22/2011 1:14:17 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";

  }

}

}

 

 

Report Abuse Form
Reason:    
 
Total Online Users: 4197
Advertisement
MindStick DataConverter
Advertise with Us
  
Copyright © 2013MindStick. All Rights Reserved.