DataGridViewColumn.DataPropertyName 属性

定义

获取或设置数据源属性的名称或与 DataGridViewColumn 绑定的数据库列的名称。

public:
 property System::String ^ DataPropertyName { System::String ^ get(); void set(System::String ^ value); };
[System.ComponentModel.Browsable(true)]
[System.ComponentModel.TypeConverter("System.Windows.Forms.Design.DataMemberFieldConverter, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
public string DataPropertyName { get; set; }
[System.ComponentModel.Browsable(true)]
[System.ComponentModel.TypeConverter("System.Windows.Forms.Design.DataMemberFieldConverter, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
public string DataPropertyName { get; set; }
[System.ComponentModel.Browsable(true)]
[System.ComponentModel.TypeConverter("System.Windows.Forms.Design.DataMemberFieldConverter, System.Windows.Forms.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
public string DataPropertyName { get; set; }
[<System.ComponentModel.Browsable(true)>]
[<System.ComponentModel.TypeConverter("System.Windows.Forms.Design.DataMemberFieldConverter, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")>]
member this.DataPropertyName : string with get, set
[<System.ComponentModel.Browsable(true)>]
[<System.ComponentModel.TypeConverter("System.Windows.Forms.Design.DataMemberFieldConverter, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")>]
member this.DataPropertyName : string with get, set
[<System.ComponentModel.Browsable(true)>]
[<System.ComponentModel.TypeConverter("System.Windows.Forms.Design.DataMemberFieldConverter, System.Windows.Forms.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")>]
member this.DataPropertyName : string with get, set
Public Property DataPropertyName As String

属性值

DataGridViewColumn 关联的属性或数据库列的不区分大小写的名称。

属性

示例

下面的代码示例演示如何选择每列将表示的属性。

public enum Title
{
    King,
    Sir
};
Public Enum Title
    King
    Sir
End Enum
private void EnumsAndComboBox_Load(object sender, System.EventArgs e)
{
    // Populate the data source.
    bindingSource1.Add(new Knight(Title.King, "Uther", true));
    bindingSource1.Add(new Knight(Title.King, "Arthur", true));
    bindingSource1.Add(new Knight(Title.Sir, "Mordred", false));
    bindingSource1.Add(new Knight(Title.Sir, "Gawain", true));
    bindingSource1.Add(new Knight(Title.Sir, "Galahad", true));

    // Initialize the DataGridView.
    dataGridView1.AutoGenerateColumns = false;
    dataGridView1.AutoSize = true;
    dataGridView1.DataSource = bindingSource1;

    dataGridView1.Columns.Add(CreateComboBoxWithEnums());

    // Initialize and add a text box column.
    DataGridViewColumn column = new DataGridViewTextBoxColumn();
    column.DataPropertyName = "Name";
    column.Name = "Knight";
    dataGridView1.Columns.Add(column);

    // Initialize and add a check box column.
    column = new DataGridViewCheckBoxColumn();
    column.DataPropertyName = "GoodGuy";
    column.Name = "Good";
    dataGridView1.Columns.Add(column);

    // Initialize the form.
    this.Controls.Add(dataGridView1);
    this.AutoSize = true;
    this.Text = "DataGridView object binding demo";
}

DataGridViewComboBoxColumn CreateComboBoxWithEnums()
{
    DataGridViewComboBoxColumn combo = new DataGridViewComboBoxColumn();
    combo.DataSource = Enum.GetValues(typeof(Title));
    combo.DataPropertyName = "Title";
    combo.Name = "Title";
    return combo;
}
#region "business object"
private class Knight
{
    private string hisName;
    private bool good;
    private Title hisTitle;

    public Knight(Title title, string name, bool good)
    {
        hisTitle = title;
        hisName = name;
        this.good = good;
    }

    public Knight()
    {
        hisTitle = Title.Sir;
        hisName = "<enter name>";
        good = true;
    }

    public string Name
    {
        get
        {
            return hisName;
        }

        set
        {
            hisName = value;
        }
    }

    public bool GoodGuy
    {
        get
        {
            return good;
        }
        set
        {
            good = value;
        }
    }

    public Title Title
    {
        get
        {
            return hisTitle;
        }
        set
        {
            hisTitle = value;
        }
    }
}
#endregion
    Private Sub SetupGrid()
        knights = New List(Of Knight)
        knights.Add(New Knight(Title.King, "Uther", True))
        knights.Add(New Knight(Title.King, "Arthur", True))
        knights.Add(New Knight(Title.Sir, "Mordred", False))
        knights.Add(New Knight(Title.Sir, "Gawain", True))
        knights.Add(New Knight(Title.Sir, "Galahad", True))

        ' Initialize the DataGridView.
        dataGridView1.AutoGenerateColumns = False
        dataGridView1.AutoSize = True
        dataGridView1.DataSource = knights

        dataGridView1.Columns.Add(CreateComboBoxWithEnums())

        ' Initialize and add a text box column.
        Dim column As DataGridViewColumn = _
            New DataGridViewTextBoxColumn()
        column.DataPropertyName = "Name"
        column.Name = "Knight"
        dataGridView1.Columns.Add(column)

        ' Initialize and add a check box column.
        column = New DataGridViewCheckBoxColumn()
        column.DataPropertyName = "GoodGuy"
        column.Name = "Good"
        dataGridView1.Columns.Add(column)

        ' Initialize the form.
        Controls.Add(dataGridView1)
        Me.AutoSize = True
        Me.Text = "DataGridView object binding demo"
    End Sub

    Private Function CreateComboBoxWithEnums() As DataGridViewComboBoxColumn
        Dim combo As New DataGridViewComboBoxColumn()
        combo.DataSource = [Enum].GetValues(GetType(Title))
        combo.DataPropertyName = "Title"
        combo.Name = "Title"
        Return combo
    End Function

#Region "business object"
    Private Class Knight
        Private hisName As String
        Private good As Boolean
        Private hisTitle As Title

        Public Sub New(ByVal title As Title, ByVal name As String, _
            ByVal good As Boolean)

            hisTitle = title
            hisName = name
            Me.good = good
        End Sub

        Public Property Name() As String
            Get
                Return hisName
            End Get

            Set(ByVal Value As String)
                hisName = Value
            End Set
        End Property

        Public Property GoodGuy() As Boolean
            Get
                Return good
            End Get
            Set(ByVal Value As Boolean)
                good = Value
            End Set
        End Property

        Public Property Title() As Title
            Get
                Return hisTitle
            End Get
            Set(ByVal Value As Title)
                hisTitle = Value
            End Set
        End Property
    End Class
#End Region

注解

当属性 AutoGenerateColumns 设置为 true时,每个列都会自动将其 DataPropertyName 属性设置为属性指定的 DataSource 数据源中属性或数据库列的名称。 也可以手动执行此绑定,如果只想显示数据源中可用的属性或数据库列的子集,这非常有用。 在这种情况下,请将 AutoGenerateColumns 属性设置为 false,然后手动添加每个 DataGridViewColumn,将每个 DataPropertyName 属性的值设置为要显示的数据源中的属性或数据库列。

适用于

另请参阅