简体   繁体   English

如何将 DataGridView 绑定到特定属性?

[英]How to bind DataGridView to specific properties?

Say if this is my Data class说如果这是我的数据 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.现在我有一个Data集合作为数据源,我希望DataGridView绑定到它。

But I only want to display A and C in the view.但我只想在视图中显示AC What's the easiest way to do this?最简单的方法是什么?

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.将每列的DataPropertyName设置为要绑定到的数据源属性/列的名称。 Before binding the data in code, set AutoGenerateColumns to false , so that the grid doesn't create any extra columns.在代码中绑定数据之前,将AutoGenerateColumns设置为false ,这样网格就不会创建任何额外的列。

Your question is how to bind DataGridView to specific properties .您的问题是如何将 DataGridView 绑定到特定属性 You mention that you have a collection of Data but don't say whether it's observable (for example BindingList<Data> ).您提到您有一个Data集合,但没有说明它是否可观察(例如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.虽然这是一个意见问题,但我个人认为非常简单的一种方法是允许AutoGenerateColumns并在表单的OnLoad覆盖中对可绑定源进行列格式化。


Example例子

截屏

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. Data class 定义列是否显示以及是否可使用可见性和属性进行编辑。

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 .例如: dataGridView.Columns["C"].Visible = false

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM