문제

사용자가 각 셀의 드롭다운에서 항목을 선택할 수 있는 DataGridView를 사용하여 테이블을 작성 중입니다.문제를 단순화하기 위해 열이 1개 있다고 가정하겠습니다.디자이너에서 DataGridViewComboBoxColumn을 사용하고 있습니다.해당 열의 각 행에 선택할 수 있는 다양한 항목 목록이 있도록 지원하려고 합니다.

이것이 가능한가?

도움이 되었습니까?

해결책

예.이는 DataGridViewComboBoxCell을 사용하여 수행할 수 있습니다.

다음은 전체 열이 아닌 하나의 셀에만 항목을 추가하는 방법의 예입니다.

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);
    }
}

다른 팁

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
    if (e.ColumnIndex == DataGridViewComboBoxColumnNumber)
    {
        setCellComboBoxItems(myDataGridView, e.RowIndex, e.ColumnIndex, someObj);
    }
}

누구든지 이 스레드를 발견할 경우를 대비해 이것이 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

이것은 2개의 콤보박스열이 있는 GridView의 예이며, 콤보박스칼럼1 선택 인덱스가 변경되면 데이터베이스의 서로 다른 두 열의 데이터로 콤보박스칼럼2를 로드합니다.

 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();
               }
            }

데이터 소스 설정 직후 콤보박스 셀 설정 나에게는 효과가 없어.바인딩 작업이 완료된 후에 수행되어야 합니다.나는 CellBegin을 선택했어요

빈 드롭다운의 예:

dgv1.datasource = datatable1;
dgv1.columns.add ( "cbxcol"  , typeof(string) );

//  different source for each comboboxcell in rows
var dict_rowInd_cbxDs = new Dictionary<int, object>();
dict_rowInd_cbxDs[1] = new list<string>(){"en" , "us"};
dict_rowInd_cbxDs[2] = new list<string>(){ "car", "bike"};

// !!!!!! setting comboboxcell after creating doesnt work here
foreach( row in dgv.Rows.asEnumerable() )
{ 
   var cell = res_tn.dgv.CurrentCell as DataGridViewComboBoxCell;
   cell.DataSource = dict_dgvRowI_cbxDs[res_tn.dgv.CurrentCell.RowIndex];

}

작업 예:

dgv1.datasource = datatable1;
dgv1.columns.add ( "cbxcol"  , typeof(string) );

//  different source for each comboboxcell in rows
var dict_rowInd_cbxDs = new Dictionary<int, object>();
dict_rowInd_cbxDs[1] = new list<string>(){"en" , "us"};
dict_rowInd_cbxDs[2] = new list<string>(){ "car", "bike"};


// cmboboxcell datasource Assingment Must be done after  BindingComplete (not tested )  or cellbeginEdit  (tested by me)    
res_tn.dgv.CellBeginEdit += (s1, e1) => {
    if (res_tn.dgv.CurrentCell is DataGridViewComboBoxCell) {
        if (dict_dgvRowI_cbxDs.ContainsKey(res_tn.dgv.CurrentCell.RowIndex)) 
        {
            var cll = res_tn.dgv.CurrentCell as DataGridViewComboBoxCell;
            cll.DataSource = dict_dgvRowI_cbxDs[res_tn.dgv.CurrentCell.RowIndex];

           // required if it is  list<mycustomClass>
           // cll.DisplayMember = "ColName";
           // cll.ValueMember = "This";
        }
    }

};
    //Populate the Datatable with the Lookup lists
    private DataTable typeDataTable(DataGridView dataGridView, Lookup<string, Element> type_Lookup, Dictionary<Element, string> type_dictionary, string strNewStyle, string strOldStyle, string strID, string strCount)
    {
        int row = 0;

        DataTable dt = new DataTable();

        dt.Columns.Add(strOldStyle, typeof(string));
        dt.Columns.Add(strID, typeof(string));
        dt.Columns.Add(strCount, typeof(int));
        dt.Columns.Add("combobox", typeof(DataGridViewComboBoxCell));



        //Add All Doc Types to ComboBoxes
        DataGridViewComboBoxCell CmBx = new DataGridViewComboBoxCell();
        CmBx.DataSource = new BindingSource(type_dictionary, null);
        CmBx.DisplayMember = "Value";
        CmBx.ValueMember = "Key";


        //Add Style Comboboxes
        DataGridViewComboBoxColumn Data_CmBx_Col = new DataGridViewComboBoxColumn();
        Data_CmBx_Col.HeaderText = strNewStyle;
        dataGridView.Columns.Add(addDataGrdViewComboBox(Data_CmBx_Col, type_dictionary));

        setCellComboBoxItems(dataGridView, 1, 3, CmBx);

        //Add style Rows
        foreach (IGrouping<string, Element> StyleGroup in type_Lookup)
        {
            row++;
            //Iterate through each group in the Igrouping
            //Add Style Rows
            dt.Rows.Add(StyleGroup.Key, row, StyleGroup.Count().ToString());


        }
        return dt;
    }




    private void setCellComboBoxItems(DataGridView dataGrid, int rowIndex, int colIndex, DataGridViewComboBoxCell CmBx)
    {
        DataGridViewComboBoxCell dgvcbc = (DataGridViewComboBoxCell)dataGrid.Rows[rowIndex].Cells[colIndex];
        // You might pass a boolean to determine whether to clear or not.
        dgvcbc.Items.Clear();
        foreach (DataGridViewComboBoxCell itemToAdd in CmBx.Items)
        {
            dgvcbc.Items.Add(itemToAdd);
        }
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top