Click here to Skip to main content
15,902,854 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
after Binding() GridView i want to insert column at some specific location.



gv1.DataSource = ds.Tables[0];
gv1.DataBind();
gv1.Visible = true;
TemplateField tf = new TemplateField();
gv1.Columns.Insert(3, tf);



it throws exception (out of bound) while columns in table is more then 3
Posted

I believe the issue is that you need to add the column before you call the databind method on the grid.

Below is some sample code to add a template field and also a datacontrolfield

Add this code to your page and it should add the columns to you grid

Good Luck

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        LoadData()

    End Sub

    Private Sub LoadData()

        Dim pi As New PersonInfo
        Dim listOfPI As New List(Of PersonInfo)

        pi.Name = "JOEY"
        pi.Password = "Password01"
        pi.Phone = "555-555-5555"
        pi.Gender = "Male"

        listOfPI.Add(pi)

        pi = New PersonInfo
        pi.Name = "Johnny"
        pi.Password = "Password02"
        pi.Phone = "555-555-6666"
        pi.Gender = "Male"
        listOfPI.Add(pi)

        pi = New PersonInfo
        pi.Name = "Jane"
        pi.Password = "Password03"
        pi.Phone = "555-555-7777"
        pi.Gender = "Female"
        listOfPI.Add(pi)

        pi = New PersonInfo
        pi.Name = "Joannie"
        pi.Password = "Password03"
        pi.Phone = "555-555-8888"
        pi.Gender = "Female"
        listOfPI.Add(pi)

        Dim ngcf As New NewgvColumnField
        Dim dcf As DataControlField = ngcf.InsertNewColumn
        dcf.HeaderText = "THIS IS THE NEW COLUMN"
        dcf.Visible = True
        dcf.ShowHeader = True

        Me.GridView1.Columns.Insert(2, dcf)

        Dim tf As New TemplateField
        tf.HeaderText = "TEMPLATE FIELD"
        tf.Visible = True

        Me.GridView1.Columns.Insert(1, tf)

        Me.GridView1.DataSource = listOfPI
        Me.GridView1.DataBind()

        



    End Sub

    Public Class NewgvColumnField
        Inherits DataControlField

        Protected Overrides Function CreateField() As System.Web.UI.WebControls.DataControlField
            Return New NewgvColumnField

        End Function

        Public Function InsertNewColumn() As DataControlField
            Return CreateField()

        End Function
    End Class


    Private Class PersonInfo
        Property Name As String
        Property Password As String
        Property Phone As String
        Property Gender As String

    End Class
 
Share this answer
 
sorry i forgot that you wrote this in c#

Try this.

protected void  // ERROR: Handles clauses are not supported in C#
Page_Load(object sender, System.EventArgs e)
{
	LoadData();

}


private void LoadData()
{
	PersonInfo pi = new PersonInfo();
	List<PersonInfo> listOfPI = new List<PersonInfo>();

	pi.Name = "JOEY";
	pi.Password = "Password01";
	pi.Phone = "555-555-5555";
	pi.Gender = "Male";

	listOfPI.Add(pi);

	pi = new PersonInfo();
	pi.Name = "Johnny";
	pi.Password = "Password02";
	pi.Phone = "555-555-6666";
	pi.Gender = "Male";
	listOfPI.Add(pi);

	pi = new PersonInfo();
	pi.Name = "Jane";
	pi.Password = "Password03";
	pi.Phone = "555-555-7777";
	pi.Gender = "Female";
	listOfPI.Add(pi);

	pi = new PersonInfo();
	pi.Name = "Joannie";
	pi.Password = "Password03";
	pi.Phone = "555-555-8888";
	pi.Gender = "Female";
	listOfPI.Add(pi);

	NewgvColumnField ngcf = new NewgvColumnField();
	DataControlField dcf = ngcf.InsertNewColumn;
	dcf.HeaderText = "THIS IS THE NEW COLUMN";
	dcf.Visible = true;
	dcf.ShowHeader = true;

	this.GridView1.Columns.Insert(2, dcf);

	TemplateField tf = new TemplateField();
	tf.HeaderText = "TEMPLATE FIELD";
	tf.Visible = true;

	this.GridView1.Columns.Insert(1, tf);

	this.GridView1.DataSource = listOfPI;
	this.GridView1.DataBind();

}

public class NewgvColumnField : DataControlField
{

	protected override System.Web.UI.WebControls.DataControlField CreateField()
	{
		return new NewgvColumnField();

	}

	public DataControlField InsertNewColumn()
	{
		return CreateField();

	}
}

    Private Class PersonInfo
{

	private string _name;
	public string Name {
		get { return _name; }
		set { _name = value; }
	}

	private string _password;
	public string Password {
		get { return _password; }
		set { _password = value; }
	}

	private string _phone;
	public string Phone {
		get { return _phone; }
		set { _phone = value; }
	}

	private string _gender;
	public string Gender {
		get { return _gender; }
		set { _gender = value; }
	}


}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900