방법: Windows Forms DataGridViewComboBoxCell 드롭다운 목록에서 개체 액세스

ComboBox 컨트롤과 마찬가지로, DataGridViewComboBoxColumnDataGridViewComboBoxCell 형식을 사용하면 드롭다운 목록에 임의 개체를 추가할 수 있습니다. 이 기능을 사용하면, 해당 개체를 별도의 컬렉션에 저장하지 않고도 드롭다운 목록에서 복합 상태를 나타낼 수 있습니다.

ComboBox 컨트롤과 달리, DataGridView 형식에는 현재 선택된 개체를 검색하기 위한 SelectedItem 속성이 없습니다. 대신 DataGridViewComboBoxColumn.ValueMember 또는 DataGridViewComboBoxCell.ValueMember 속성을 비즈니스 개체의 속성 이름으로 설정해야 합니다. 사용자가 선택을 할 때 비즈니스 개체의 표시된 속성이 셀 Value 속성을 설정합니다.

셀 값을 통해 비즈니스 개체를 검색하려면 ValueMember 속성이 비즈니스 개체 자체에 대한 참조를 반환하는 속성을 나타내야 합니다. 따라서 비즈니스 개체의 형식이 제어되지 않는 경우 상속을 통해 형식을 확장하여 이러한 속성을 추가해야 합니다.

다음 절차에서는 드롭다운 목록을 비즈니스 개체로 채우고 셀 Value 속성을 통해 개체를 검색하는 방법을 보여줍니다.

드롭다운 목록에 비즈니스 개체를 추가하려면

  1. DataGridViewComboBoxColumn을 만들고 해당 Items 컬렉션을 채웁니다. 또는 열 DataSource 속성을 비즈니스 개체의 컬렉션으로 설정할 수 있습니다. 그러나, 이 경우 컬렉션에서 해당 비즈니스 개체를 만들지 않고는 드롭다운 목록에 “할당되지 않음”을 추가할 수 없습니다.

    DataGridViewComboBoxColumn assignedToColumn =
        new DataGridViewComboBoxColumn();
    
    // Populate the combo box drop-down list with Employee objects.
    foreach (Employee e in employees) assignedToColumn.Items.Add(e);
    
    // Add "unassigned" to the drop-down list and display it for
    // empty AssignedTo values or when the user presses CTRL+0.
    assignedToColumn.Items.Add("unassigned");
    assignedToColumn.DefaultCellStyle.NullValue = "unassigned";
    
    Dim assignedToColumn As New DataGridViewComboBoxColumn()
    
    ' Populate the combo box drop-down list with Employee objects. 
    For Each e As Employee In employees
        assignedToColumn.Items.Add(e)
    Next
    
    ' Add "unassigned" to the drop-down list and display it for 
    ' empty AssignedTo values or when the user presses CTRL+0. 
    assignedToColumn.Items.Add("unassigned")
    assignedToColumn.DefaultCellStyle.NullValue = "unassigned"
    
  2. DisplayMemberValueMember 속성을 설정합니다. DisplayMember는 드롭다운 목록에 표시할 비즈니스 개체의 속성을 나타냅니다. ValueMember는 비즈니스 개체에 대한 참조를 반환하는 속성을 나타냅니다.

    assignedToColumn.DisplayMember = "Name";
    assignedToColumn.ValueMember = "Self";
    
    assignedToColumn.DisplayMember = "Name"
    assignedToColumn.ValueMember = "Self"
    
  3. 비즈니스 개체 형식에 현재 인스턴스에 대한 참조를 반환하는 속성이 포함되어 있는지 확인합니다. 이 속성의 이름은 이전 단계에서 ValueMember에 할당된 값으로 지정되어야 합니다.

    public Employee Self
    {
        get { return this; }
    }
    
    Public ReadOnly Property Self() As Employee
        Get
            Return Me
        End Get
    End Property
    

현재 선택한 비즈니스 개체를 검색하려면

  • Value 속성을 가져와 비즈니스 개체 형식으로 캐스팅합니다.

    // Retrieve the Employee object from the "Assigned To" cell.
    Employee assignedTo = dataGridView1.Rows[e.RowIndex]
        .Cells["Assigned To"].Value as Employee;
    
    ' Retrieve the Employee object from the "Assigned To" cell.
    Dim assignedTo As Employee = TryCast(dataGridView1.Rows(e.RowIndex) _
        .Cells("Assigned To").Value, Employee)
    

예제

전체 예제에서는 드롭다운 목록에서 비즈니스 개체를 사용하는 방법을 보여줍니다. 이 예제에서 DataGridView 컨트롤은 Task 개체의 컬렉션으로 바인딩됩니다. 각 Task 개체에는 현재 해당 작업에 할당된 Employee 개체를 나타내는 AssignedTo 속성이 있습니다. Assigned To 열에는 할당된 각 직원에 대한 Name 속성 값을 표시하거나, Task.AssignedTo 속성 값이 null인 경우 “할당되지 않음”을 표시합니다.

이 예제의 동작을 보려면 다음 단계를 수행합니다.

  1. 드롭다운 목록에서 다른 값을 선택하거나 콤보 상자 셀에서 Ctrl+0을 눌러 Assigned To 열의 할당을 변경합니다.

  2. Generate Report를 클릭하여 현재 할당을 표시합니다. 이것은 Assigned To 열의 변경이 자동으로 tasks 컬렉션을 업데이트한다는 것을 보여줍니다.

  3. Click a Request Status 단추를 클릭하여 해당 행에 대한 현재 Employee 개체의 RequestStatus 메서드를 호출합니다. 이것은 선택한 개체가 성공적으로 검색되었음을 보여줍니다.

using System;
using System.Text;
using System.Collections.Generic;
using System.Windows.Forms;

public class Form1 : Form
{
    private List<Employee> employees = new List<Employee>();
    private List<Task> tasks = new List<Task>();
    private Button reportButton = new Button();
    private DataGridView dataGridView1 = new DataGridView();

    [STAThread]
    public static void Main()
    {
        Application.Run(new Form1());
    }

    public Form1()
    {
        dataGridView1.Dock = DockStyle.Fill;
        dataGridView1.AutoSizeColumnsMode =
            DataGridViewAutoSizeColumnsMode.AllCells;
        reportButton.Text = "Generate Report";
        reportButton.Dock = DockStyle.Top;
        reportButton.Click += new EventHandler(reportButton_Click);

        Controls.Add(dataGridView1);
        Controls.Add(reportButton);
        Load += new EventHandler(Form1_Load);
        Text = "DataGridViewComboBoxColumn Demo";
    }

    // Initializes the data source and populates the DataGridView control.
    private void Form1_Load(object sender, EventArgs e)
    {
        PopulateLists();
        dataGridView1.AutoGenerateColumns = false;
        dataGridView1.DataSource = tasks;
        AddColumns();
    }

    // Populates the employees and tasks lists.
    private void PopulateLists()
    {
        employees.Add(new Employee("Harry"));
        employees.Add(new Employee("Sally"));
        employees.Add(new Employee("Roy"));
        employees.Add(new Employee("Pris"));
        tasks.Add(new Task(1, employees[1]));
        tasks.Add(new Task(2));
        tasks.Add(new Task(3, employees[2]));
        tasks.Add(new Task(4));
    }

    // Configures columns for the DataGridView control.
    private void AddColumns()
    {
        DataGridViewTextBoxColumn idColumn =
            new DataGridViewTextBoxColumn();
        idColumn.Name = "Task";
        idColumn.DataPropertyName = "Id";
        idColumn.ReadOnly = true;

        DataGridViewComboBoxColumn assignedToColumn =
            new DataGridViewComboBoxColumn();

        // Populate the combo box drop-down list with Employee objects.
        foreach (Employee e in employees) assignedToColumn.Items.Add(e);

        // Add "unassigned" to the drop-down list and display it for
        // empty AssignedTo values or when the user presses CTRL+0.
        assignedToColumn.Items.Add("unassigned");
        assignedToColumn.DefaultCellStyle.NullValue = "unassigned";

        assignedToColumn.Name = "Assigned To";
        assignedToColumn.DataPropertyName = "AssignedTo";
        assignedToColumn.AutoComplete = true;
        assignedToColumn.DisplayMember = "Name";
        assignedToColumn.ValueMember = "Self";

        // Add a button column.
        DataGridViewButtonColumn buttonColumn =
            new DataGridViewButtonColumn();
        buttonColumn.HeaderText = "";
        buttonColumn.Name = "Status Request";
        buttonColumn.Text = "Request Status";
        buttonColumn.UseColumnTextForButtonValue = true;

        dataGridView1.Columns.Add(idColumn);
        dataGridView1.Columns.Add(assignedToColumn);
        dataGridView1.Columns.Add(buttonColumn);

        // Add a CellClick handler to handle clicks in the button column.
        dataGridView1.CellClick +=
            new DataGridViewCellEventHandler(dataGridView1_CellClick);
    }

    // Reports on task assignments.
    private void reportButton_Click(object sender, EventArgs e)
    {
        StringBuilder report = new StringBuilder();
        foreach (Task t in tasks)
        {
            String assignment =
                t.AssignedTo == null ?
                "unassigned" : "assigned to " + t.AssignedTo.Name;
            report.AppendFormat("Task {0} is {1}.", t.Id, assignment);
            report.Append(Environment.NewLine);
        }
        MessageBox.Show(report.ToString(), "Task Assignments");
    }

    // Calls the Employee.RequestStatus method.
    void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
    {
        // Ignore clicks that are not on button cells.
        if (e.RowIndex < 0 || e.ColumnIndex !=
            dataGridView1.Columns["Status Request"].Index) return;

        // Retrieve the task ID.
        Int32 taskID = (Int32)dataGridView1[0, e.RowIndex].Value;

        // Retrieve the Employee object from the "Assigned To" cell.
        Employee assignedTo = dataGridView1.Rows[e.RowIndex]
            .Cells["Assigned To"].Value as Employee;

        // Request status through the Employee object if present.
        if (assignedTo != null)
        {
            assignedTo.RequestStatus(taskID);
        }
        else
        {
            MessageBox.Show(String.Format(
                "Task {0} is unassigned.", taskID), "Status Request");
        }
    }
}

public class Task
{
    public Task(Int32 id)
    {
        idValue = id;
    }

    public Task(Int32 id, Employee assignedTo)
    {
        idValue = id;
        assignedToValue = assignedTo;
    }

    private Int32 idValue;
    public Int32 Id
    {
        get { return idValue; }
        set { idValue = value; }
    }

    private Employee assignedToValue;
    public Employee AssignedTo
    {
        get { return assignedToValue; }
        set { assignedToValue = value; }
    }
}

public class Employee
{
    public Employee(String name)
    {
        nameValue = name;
    }

    private String nameValue;
    public String Name
    {
        get { return nameValue; }
        set { nameValue = value; }
    }

    public Employee Self
    {
        get { return this; }
    }

    public void RequestStatus(Int32 taskID)
    {
        MessageBox.Show(String.Format(
            "Status for task {0} has been requested from {1}.",
            taskID, nameValue), "Status Request");
    }
}
Imports System.Text
Imports System.Collections.Generic
Imports System.Windows.Forms

Public Class Form1
    Inherits Form

    Private employees As New List(Of Employee)
    Private tasks As New List(Of Task)
    Private WithEvents reportButton As New Button
    Private WithEvents dataGridView1 As New DataGridView

    <STAThread()> _
    Public Sub Main()
        Application.Run(New Form1)
    End Sub

    Sub New()
        dataGridView1.Dock = DockStyle.Fill
        dataGridView1.AutoSizeColumnsMode = _
            DataGridViewAutoSizeColumnsMode.AllCells
        reportButton.Text = "Generate Report"
        reportButton.Dock = DockStyle.Top

        Controls.Add(dataGridView1)
        Controls.Add(reportButton)
        Text = "DataGridViewComboBoxColumn Demo"
    End Sub

    ' Initializes the data source and populates the DataGridView control.
    Private Sub Form1_Load(ByVal sender As Object, _
        ByVal e As EventArgs) Handles Me.Load

        PopulateLists()
        dataGridView1.AutoGenerateColumns = False
        dataGridView1.DataSource = tasks
        AddColumns()

    End Sub

    ' Populates the employees and tasks lists. 
    Private Sub PopulateLists()
        employees.Add(New Employee("Harry"))
        employees.Add(New Employee("Sally"))
        employees.Add(New Employee("Roy"))
        employees.Add(New Employee("Pris"))
        tasks.Add(New Task(1, employees(1)))
        tasks.Add(New Task(2))
        tasks.Add(New Task(3, employees(2)))
        tasks.Add(New Task(4))
    End Sub

    ' Configures columns for the DataGridView control.
    Private Sub AddColumns()

        Dim idColumn As New DataGridViewTextBoxColumn()
        idColumn.Name = "Task"
        idColumn.DataPropertyName = "Id"
        idColumn.ReadOnly = True

        Dim assignedToColumn As New DataGridViewComboBoxColumn()

        ' Populate the combo box drop-down list with Employee objects. 
        For Each e As Employee In employees
            assignedToColumn.Items.Add(e)
        Next

        ' Add "unassigned" to the drop-down list and display it for 
        ' empty AssignedTo values or when the user presses CTRL+0. 
        assignedToColumn.Items.Add("unassigned")
        assignedToColumn.DefaultCellStyle.NullValue = "unassigned"

        assignedToColumn.Name = "Assigned To"
        assignedToColumn.DataPropertyName = "AssignedTo"
        assignedToColumn.AutoComplete = True
        assignedToColumn.DisplayMember = "Name"
        assignedToColumn.ValueMember = "Self"

        ' Add a button column. 
        Dim buttonColumn As New DataGridViewButtonColumn()
        buttonColumn.HeaderText = ""
        buttonColumn.Name = "Status Request"
        buttonColumn.Text = "Request Status"
        buttonColumn.UseColumnTextForButtonValue = True

        dataGridView1.Columns.Add(idColumn)
        dataGridView1.Columns.Add(assignedToColumn)
        dataGridView1.Columns.Add(buttonColumn)

    End Sub

    ' Reports on task assignments. 
    Private Sub reportButton_Click(ByVal sender As Object, _
        ByVal e As EventArgs) Handles reportButton.Click

        Dim report As New StringBuilder()
        For Each t As Task In tasks
            Dim assignment As String
            If t.AssignedTo Is Nothing Then
                assignment = "unassigned"
            Else
                assignment = "assigned to " + t.AssignedTo.Name
            End If
            report.AppendFormat("Task {0} is {1}.", t.Id, assignment)
            report.Append(Environment.NewLine)
        Next
        MessageBox.Show(report.ToString(), "Task Assignments")

    End Sub

    ' Calls the Employee.RequestStatus method.
    Private Sub dataGridView1_CellClick(ByVal sender As Object, _
        ByVal e As DataGridViewCellEventArgs) _
        Handles dataGridView1.CellClick

        ' Ignore clicks that are not on button cells. 
        If e.RowIndex < 0 OrElse Not e.ColumnIndex = _
            dataGridView1.Columns("Status Request").Index Then Return

        ' Retrieve the task ID.
        Dim taskID As Int32 = CInt(dataGridView1(0, e.RowIndex).Value)

        ' Retrieve the Employee object from the "Assigned To" cell.
        Dim assignedTo As Employee = TryCast(dataGridView1.Rows(e.RowIndex) _
            .Cells("Assigned To").Value, Employee)

        ' Request status through the Employee object if present. 
        If assignedTo IsNot Nothing Then
            assignedTo.RequestStatus(taskID)
        Else
            MessageBox.Show(String.Format( _
                "Task {0} is unassigned.", taskID), "Status Request")
        End If

    End Sub

End Class

Public Class Task

    Sub New(ByVal id As Int32)
        idValue = id
    End Sub

    Sub New(ByVal id As Int32, ByVal assignedTo As Employee)
        idValue = id
        assignedToValue = assignedTo
    End Sub

    Private idValue As Int32
    Public Property Id() As Int32
        Get
            Return idValue
        End Get
        Set(ByVal value As Int32)
            idValue = value
        End Set
    End Property

    Private assignedToValue As Employee
    Public Property AssignedTo() As Employee
        Get
            Return assignedToValue
        End Get
        Set(ByVal value As Employee)
            assignedToValue = value
        End Set
    End Property

End Class

Public Class Employee

    Sub New(ByVal name As String)
        nameValue = name
    End Sub

    Private nameValue As String
    Public Property Name() As String
        Get
            Return nameValue
        End Get
        Set(ByVal value As String)
            nameValue = value
        End Set
    End Property

    Public ReadOnly Property Self() As Employee
        Get
            Return Me
        End Get
    End Property

    Public Sub RequestStatus(ByVal taskID As Int32)
        MessageBox.Show(String.Format( _
            "Status for task {0} has been requested from {1}.", _
            taskID, nameValue), "Status Request")
    End Sub

End Class

코드 컴파일

이 예제에는 다음 사항이 필요합니다.

  • System 및 System.Windows.Forms 어셈블리에 대한 참조

참고 항목