-2

Say if this is my Data class

class Data
{
    public int A { set; get;}
    public long B { set; get;}
    public string C { set; get;}
}

Now I have a collection of Data as data source and I want the DataGridView to bind to it.

But I only want to display A and C in the view. What's the easiest way to do this?

Lee
  • 520
  • 5
  • 13
  • 1
    Suggested reading: [BrowsableAttribute](https://learn.microsoft.com/en-us/dotnet/api/system.componentmodel.browsableattribute?view=net-7.0) – Ňɏssa Pøngjǣrdenlarp Jan 12 '23 at 03:32
  • In addition to all common solution, you may be interested in using [DataAnnotations attributes for DataGridView in Windows Forms](https://stackoverflow.com/a/59885956/3110834). – Reza Aghaei Jan 12 '23 at 08:07

2 Answers2

1

Add the columns you want to the grid in the designer. Set the DataPropertyName of each column to the name of the data source property/column that you want to bind to. Before binding the data in code, set AutoGenerateColumns to false, so that the grid doesn't create any extra columns.

jmcilhinney
  • 50,448
  • 5
  • 26
  • 46
0

Your question is how to bind DataGridView to specific properties . You mention that you have a collection of Data but don't say whether it's observable (for example BindingList<Data>). You end your post with what's the easiest way to do this? While that's a matter of opinion, one way that I personally find very easy is to allow AutoGenerateColumns and do column formatting on a bindable source in the OnLoad override of the form.


Example

screenshot

public partial class MainForm : Form
{
    public MainForm() => InitializeComponent();
    internal BindingList<Data> Rows { get; } = new BindingList<Data>();
    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);

        dataGridView.AllowUserToAddRows = false;
        dataGridView.DataSource = Rows;
        dataGridView.CellEndEdit += (sender, e) => dataGridView.Refresh();

        #region F O R M A T    C O L U M N S
        Rows.Add(new Data()); // <= Auto-generate columns
        dataGridView.Columns["A"].Width = 50;
        dataGridView.Columns["A"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
        dataGridView.Columns["A"].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
        dataGridView.Columns["C"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
        dataGridView.Columns["C"].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
        Rows.Clear();
        #endregion F O R M A T    C O L U M N S

        // Add a few items
        Rows.Add(new Data { A = 1, B = 1000L });
        Rows.Add(new Data { A = 2, B = 2000L });
        Rows.Add(new Data { A = 3, B = 3000L });
    }
}

The Data class defines whether the column is shown and editable using visibility and attributes.

class Data
{
    // A visible, editable cell.
    public int A { get; set; }

    // Non-visible because property is declared as internal.
    internal long B { get; set; }

    // Visible, read-only cell that dynamically responds
    // when cell 'A" is edited due to Refresh() 
    public string C => $"A={A} B={B}";

    // Non-visible because of attribute.
    [Browsable(false)]
    public string? D { get; set; } = "Not visible";        
}

Another alternative, provided the column exists in the first place, is that it can be shown-hidden. For example: dataGridView.Columns["C"].Visible = false.

IVSoftware
  • 5,732
  • 2
  • 12
  • 23