16

ユーザーが各セルのドロップダウンから項目を選択できる DataGridView を使用してテーブルを作成しています。問題を単純化するために、1 つの列があるとします。デザイナーで DataGridViewComboBoxColumn を使用しています。その列の各行に、選択できるアイテムのリストが異なるようにすることをサポートしようとしています。

これは可能ですか?

4

6 に答える 6

20

はい。これは、DataGridViewComboBoxCell を使用して行うことができます。

列全体ではなく、1 つのセルだけにアイテムを追加する方法の例を次に示します。

private void setCellComboBoxItems(DataGridView dataGrid, int rowIndex, int colIndex, object[] itemsToAdd)
{
    DataGridViewComboBoxCell dgvcbc = (DataGridViewComboBoxCell) dataGrid.Rows[rowIndex].Cells[colIndex];
    // You might pass a boolean to determine whether to clear or not.
    dgvcbc.Items.Clear();
    foreach (object itemToAdd in itemsToAdd)
    {
        dgvcbc.Items.Add(itemToAdd);
    }
}
于 2008-10-02T16:13:14.520 に答える
3
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
    if (e.ColumnIndex == DataGridViewComboBoxColumnNumber)
    {
        setCellComboBoxItems(myDataGridView, e.RowIndex, e.ColumnIndex, someObj);
    }
}
于 2009-10-29T03:20:07.027 に答える
1

誰かがこのスレッドを見つけた場合に備えて、これが VB 2008 での私のソリューションです。これが提供する利点は、コンボボックスの各値に ID を割り当てることができることです。

  Private Sub FillGroups()
    Try
        'Create Connection and SQLCommand here.

        Conn.Open()
        Dim dr As SqlDataReader = cm.ExecuteReader

        dgvGroups.Rows.Clear()

        Dim PreviousGroup As String = ""

        Dim l As New List(Of Groups)

        While dr.Read

            Dim g As New Groups
            g.RegionID = CheckInt(dr("cg_id"))
            g.RegionName = CheckString(dr("cg_name"))
            g.GroupID = CheckInt(dr("vg_id"))
            g.GroupName = CheckString(dr("vg_name"))
            l.Add(g)

        End While
        dr.Close()
        Conn.Close()

        For Each a In (From r In l Select r.RegionName, r.RegionID).Distinct

            Dim RegionID As Integer = a.RegionID 'Doing it this way avoids a warning

            dgvGroups.Rows.Add(New Object() {a.RegionID, a.RegionName})

            Dim c As DataGridViewComboBoxCell = CType(dgvGroups.Rows(dgvGroups.RowCount - 1).Cells(colGroup.Index), DataGridViewComboBoxCell)
            c.DataSource = (From g In l Where g.RegionID = RegionID Select g.GroupID, g.GroupName).ToArray
            c.DisplayMember = "GroupName"
            c.ValueMember = "GroupID"
        Next

    Catch ex As Exception
    End Try
End Sub

Private Class Groups

    Private _RegionID As Integer
    Public Property RegionID() As Integer
        Get
            Return _RegionID
        End Get
        Set(ByVal value As Integer)
            _RegionID = value
        End Set
    End Property

    Private _RegionName As String
    Public Property RegionName() As String
        Get
            Return _RegionName
        End Get
        Set(ByVal value As String)
            _RegionName = value
        End Set
    End Property

    Private _GroupName As String
    Public Property GroupName() As String
        Get
            Return _GroupName
        End Get
        Set(ByVal value As String)
            _GroupName = value
        End Set
    End Property

    Private _GroupID As Integer
    Public Property GroupID() As Integer
        Get            
            Return _GroupID
        End Get
        Set(ByVal value As Integer)
            _GroupID = value
        End Set
    End Property

End Class
于 2012-01-16T11:30:26.847 に答える
0

これは、2 つのコンボボックス列を持つ gridView の例です。comboBoxColumns1 の選択されたインデックスが変更された場合、データベースの 2 つの異なる列からのデータを使って、comboBoxColumns2 をロードします。

 private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
              if (dataGridView1.Rows[e.RowIndex].Cells[0].Value != null && dataGridView1.CurrentCell.ColumnIndex == 0)
            {

                SqlConnection conn = new SqlConnection("data source=.;initial catalog=pharmacy;integrated security=true");
                SqlCommand cmd = new SqlCommand("select [drugTypeParent],[drugTypeChild] from [drugs] where [drugName]='" + dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString() + "'", conn);
                conn.Open();
                SqlDataReader dr = cmd.ExecuteReader();
                while (dr.Read())
                {

                    object[] o = new object[] { dr[0].ToString(),dr[1].ToString() };
                    DataGridViewComboBoxCell dgvcbc = (DataGridViewComboBoxCell)dataGridView1.Rows[e.RowIndex].Cells[1];

                    dgvcbc.Items.Clear();
                    foreach (object itemToAdd in o)
                    {
                        dgvcbc.Items.Add(itemToAdd);
                    }
                }
                dr.Close();
                conn.Close();
               }
            }
于 2016-02-14T19:07:20.077 に答える