1

I want to add data in Data Grid View column wise.

I am adding the column at runtime and then i want to add data to it.

Also is it possible that the columns resize themselves to fit the window.

eg-If there are 10 columns then 10 columns should fill the window else if there are 5 columns then 5 columns should fill the window

5
  • Do you already have data in the grid when you add the new column? If so how does that data get there, is the grid unbound or bound to a datasource, and if a datasource what is the type of that? Commented May 4, 2012 at 9:43
  • @DavidHall I don't have any data in the grid.The grid is empty.Now i have to add a column to it and add values to that column.The values are in a List Commented May 4, 2012 at 9:55
  • @DavidHall If it can be done in ListView then also it is fine.I just need to display data...Thanks in Advance Commented May 4, 2012 at 10:02
  • Ah ok, so you just have one list of values (strings?) that you want to display to the user, and will never have another column to display? And do you want users to be able to edit the data? And the be able to add new items. Sorry for the twenty questions but they help to know just the right control and binding method to suggest. Commented May 4, 2012 at 10:19
  • 1
    Actually, from the second question about scaling columns, it sounds like you list can sometimes have multiple items in it? Commented May 4, 2012 at 10:20

1 Answer 1

1

Here is the code for a simple example doing what you want. It is from the code behind for a simple form with one DataGridView control on it.

public partial class Form1 : Form
{

    public Form1()
    {
        InitializeComponent();

        BindingList<BindingClass> data = new BindingList<BindingClass>() { 
            new BindingClass { Name = "joe", Surname = "bloggs" }, 
            new BindingClass { Name = "sue", Surname = "bloggs" } };
        dataGridView1.DataSource = data;
        dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
    }

}   

    public class BindingClass
    {
        public string Name { get; set; }
        public string Surname { get; set; }
    }

} 

I've created a custom object to use for my data - this is where your list goes. All the public properties of lists bound to the DataGridView's datasource get created as columns - you can then hide unwanted columns.

In my example I've chosen to use a BindingList<T> rather than a List<T> - this is generally preferred since it gives you editing and can be extended to support sorting.

I also set the AutoSizeColumnsMode of the grid to Fill - there are several other options including setting the width or fill properties of each column individually, and these can also be set through the designer.


I don't recommend using a ListView since it doesn't offer automatic databinding.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.