Add a new column to my GridView

Mickeys
Mickeys used Ask the Experts™
on
How do I add an extra column to my gridview?

I have the code below.

This is what it looks like right now
Name    Phone     Adress

And I want to add one more column lilke this
Name    Phone     Adress     E-mail

How?
/// <summary>
        /// Updater updates the gui
        /// </summary>
        private void Updater()
        {
            //Update the gridview
            AnimalDataGridView.DataSource = null;
            AnimalDataGridView.AutoGenerateColumns = true;
            AnimalDataGridView.DataSource = animalManager.AnimalList;
            nickNameTextBox.Text = "";
            nrOfItems.Text = Convert.ToString(animalManager.Counter);
        
}

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Senior Consultant - Deloitte
Commented:
You can enhance your resultset like

select
    Column1,
    Column2,
    Column3,
    Column4
from
   Table

Or you can do one thing..that you can create columns by code like


BoundColumn nameColumn = new BoundColumn();
nameColumn.HeaderText = "Name";
nameColumn.DataField = "UserName";
myDataGrid.Columns.Add(nameColumn);


Author

Commented:
I am not using ASP so I cant use BoundColumn I guess.

I am not using a sql data bas. My DataGridView is using a List<Contacts>
Rahul Goel ITILSenior Consultant - Deloitte

Commented:
Then you need to create a new property in class Contact called EmailAddress or your other columns.

Or

You need to mannually add columns in gridview of windows app and then bind to the specific property of the contact class.

Author

Commented:
well I have managed to put it into the gridview manually but......how do I choose where it should be? Right now it is the first cell. I want it to be the last

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial