Apostrofix
Apostrofix

Reputation: 2180

Populate DataGridView with 2 columns from list

I am trying to bind a list of items to a DataGridView. The problem is that it shows all columns in the list, and I want to show just 2 of them and keep the others(maybe to make them invisible), because they contain some IDs important for the logic behind. This is what I have so far:

private void BindFormData(object sender, EventArgs e)
{
    //returns a list of items
    var extraParamters = context.getExtraParameters(scheduleId, exportId);

    if (extraParamters.Count > 0)
    {
            dataGridView1.DataSource = extraParamters;
    }
}

The list of items looks like:

Id | accId | Name | Value | custId | 
 1    150    Name    15      10
 2    200    Name    25      10

And so on..

How do I show just the Name and the Value columns? My idea is that I'll have them editable in the DataGridView, and that I'll also be able to insert new entries.

Upvotes: 0

Views: 449

Answers (1)

Darren Wainwright
Darren Wainwright

Reputation: 30757

Let your grid view auto generate the columns, and then just remove them.

  • you can also build it all in code, though this might be easier for you until you get used to it.

So something like this:

 dataGridView1.AutoGenerateColumns = true;
 dataGridView1.DataSource = extraParamters;
 dataGridView1.Columns.Remove("accId");
 dataGridView1.Columns.Remove("custId");
 dataGridView1.Columns["Id"].Visible = false;

Notice I didn't remove the id column - just hid it. You'll be needed that one for your updating in the future.

Or just hide the ones you don't want

 dataGridView1.AutoGenerateColumns = true;
 dataGridView1.DataSource = extraParamters;
 dataGridView1.Columns["accId"].Visible = false;
 dataGridView1.Columns["custId"].Visible = false;
 dataGridView1.Columns["Id"].Visible = false;

Upvotes: 1

Related Questions