Windows Form DataGridView ComboBoxColumn does not open DropDown on Click

Last Reply 7 months ago By pandeyism

Posted 7 months ago

I have datagridview which has comboboxcolumn.But it won't dropping down. My code is:

    Private Sub returndetailsDGV_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles returndetailsDGV.CellContentClick
        If e.RowIndex >= 0 Then
            Dim index As Integer
            index = e.RowIndex
            Dim row As DataGridViewRow
            row = returndetailsDGV.Rows(index)
            Try
                returnBillDetailsDGV.DataSource = Nothing
                dbConnection()
                sql = "SELECT Product_ID_Barcode,Product_Name,Quantity,Unit_Price,Subtotal,Tax_Amount,ReturnType FROM purchasereturn_transaction_database WHERE Bill_ref_No='" & row.Cells(0).Value & "';"
                cmd = New MySqlCommand(sql, conn)
                dt = New DataTable
                da = New MySqlDataAdapter
                Dim bsource As New BindingSource
                da.SelectCommand = cmd
                da.Fill(dt)
                returnBillDetailsDGV.AutoGenerateColumns = False

                'Set Columns Count
                returnBillDetailsDGV.ColumnCount = 7

                'Add Columns
                returnBillDetailsDGV.Columns(0).Name = "Product_ID_Barcode"
                returnBillDetailsDGV.Columns(0).HeaderText = "Product ID"
                returnBillDetailsDGV.Columns(0).DataPropertyName = "Product_ID_Barcode"

                returnBillDetailsDGV.Columns(1).Name = "Product_Name"
                returnBillDetailsDGV.Columns(1).HeaderText = "Product"
                returnBillDetailsDGV.Columns(1).DataPropertyName = "Product_Name"

                returnBillDetailsDGV.Columns(2).Name = "Quantity"
                returnBillDetailsDGV.Columns(2).HeaderText = "Quantity"
                returnBillDetailsDGV.Columns(2).DataPropertyName = "Quantity"

                returnBillDetailsDGV.Columns(3).Name = "Unit_Price"
                returnBillDetailsDGV.Columns(3).HeaderText = "Unit Price"
                returnBillDetailsDGV.Columns(3).DataPropertyName = "Unit_Price"

                returnBillDetailsDGV.Columns(4).Name = "Subtotal"
                returnBillDetailsDGV.Columns(4).HeaderText = "SubTotal"
                returnBillDetailsDGV.Columns(4).DataPropertyName = "Subtotal"

                returnBillDetailsDGV.Columns(5).Name = "Tax_Amount"
                returnBillDetailsDGV.Columns(5).HeaderText = "Tax_Amount"
                returnBillDetailsDGV.Columns(5).DataPropertyName = "Tax_Amount"

                returnBillDetailsDGV.Columns(6).Name = "ReturnType"
                returnBillDetailsDGV.Columns(6).HeaderText = "ReturnType"
                returnBillDetailsDGV.Columns(6).DataPropertyName = "ReturnType"

                Dim Status As New DataGridViewComboBoxColumn
                Status.Name = "ReturnType"
                Status.Items.Add("Yes")
                Status.Items.Add("No")
                returnBillDetailsDGV.Columns.Insert(7, Status)
                bsource.DataSource = dt
                returnBillDetailsDGV.DataSource = dt


            Catch ex As MySqlException
                Dim path1 As String = IO.Path.Combine(My.Application.Info.DirectoryPath, "Errorlogs")
                Dim path As String = IO.Path.Combine(My.Application.Info.DirectoryPath, "Errorlogs\Error.txt")
                path = getfileName(path)
                If (Not System.IO.Directory.Exists(path1)) Then
                    System.IO.Directory.CreateDirectory(path1)
                    Using sw = New StreamWriter(path, False)
                        sw.Write(String.Format("Message:{0}{1}\n Date:{2}{1}------------------------------------------------------------------------", ex.Message, Environment.NewLine, DateTime.Now.ToString(), Environment.NewLine))
                    End Using
                    EncryptFile(path, path)
                    MessageBox.Show("Error is occured! Please get error file at : " + path)
                    Application.Exit()
                Else
                    Using sw = New StreamWriter(path, False)
                        sw.Write(String.Format("Message:{0}{1}\n Date:{2}{1}------------------------------------------------------------------------", ex.Message, Environment.NewLine, DateTime.Now.ToString(), Environment.NewLine))
                    End Using
                    EncryptFile(path, path)
                    MessageBox.Show("Error is occured! Please get error file at : " + path)
                    Application.Exit()
                End If
            Finally
                conn.Close()
                cmd.Dispose()
            End Try
        End If
    End Sub

 

 

You are viewing reply posted by: pandeyism 7 months ago.
Posted 7 months ago

Hi PSowmiya,

Refer below sample.

Namespaces

C#

using System.Data.SqlClient;

VB.Net

Imports System.Data.SqlClient

Code

C#

private void Form1_Load(object sender, EventArgs e)
{
    dataGridView1.DataSource = GetData("SELECT CustomerId, Name FROM Customers");
    DataGridViewComboBoxColumn checkBoxColumn = new DataGridViewComboBoxColumn();
    checkBoxColumn.HeaderText = "";
    checkBoxColumn.Width = 70;
    checkBoxColumn.Name = "ComboBoxColumn";
    dataGridView1.Columns.Insert(0, checkBoxColumn);
}

private void dgv_CellClick(object sender, DataGridViewCellEventArgs e)
{
    if (e.ColumnIndex > -1)
    {
        DataGridViewComboBoxCell dgvComboBoxCell = new DataGridViewComboBoxCell();
        if (dataGridView1.Columns[e.ColumnIndex].Name.Contains("ComboBoxColumn"))
        {
            dataGridView1[e.ColumnIndex, e.RowIndex] = dgvComboBoxCell;
            dgvComboBoxCell.DataSource = GetData("SELECT CustomerId , Country FROM Customers");
            dgvComboBoxCell.ValueMember = "CustomerId";
            dgvComboBoxCell.DisplayMember = "Country";
        }
    }
}

private DataTable GetData(string query)
{
    using (SqlConnection con = new SqlConnection(ConnectionString))
    {
        using (SqlCommand cmd = new SqlCommand(query, con))
        {
            cmd.CommandType = CommandType.Text;
            using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
            {
                using (DataTable dt = new DataTable())
                {
                    sda.Fill(dt);
                    return dt;
                }
            }
        }
    }
}

VB.Net

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    dataGridView1.DataSource = GetData("SELECT CustomerId, Name FROM Customers")
    Dim checkBoxColumn As DataGridViewComboBoxColumn = New DataGridViewComboBoxColumn()
    checkBoxColumn.HeaderText = ""
    checkBoxColumn.Width = 70
    checkBoxColumn.Name = "ComboBoxColumn"
    dataGridView1.Columns.Insert(0, checkBoxColumn)
End Sub
Private Sub dgv_CellClick(ByVal sender As Object, ByVal e As DataGridViewCellEventArgs) Handles dataGridView1.CellClick
    If e.ColumnIndex > -1 Then
        Dim dgvComboBoxCell As DataGridViewComboBoxCell = New DataGridViewComboBoxCell()
        If dataGridView1.Columns(e.ColumnIndex).Name.Contains("ComboBoxColumn") Then
            dataGridView1(e.ColumnIndex, e.RowIndex) = dgvComboBoxCell
            dgvComboBoxCell.DataSource = GetData("SELECT CustomerId , Country FROM Customers")
            dgvComboBoxCell.ValueMember = "CustomerId"
            dgvComboBoxCell.DisplayMember = "Country"
        End If
    End If
End Sub

Private Function GetData(ByVal query As String) As DataTable
    Using con As SqlConnection = New SqlConnection(ConnectionString)
        Using cmd As SqlCommand = New SqlCommand(query, con)
            cmd.CommandType = CommandType.Text
            Using sda As SqlDataAdapter = New SqlDataAdapter(cmd)
                Using dt As DataTable = New DataTable()
                    sda.Fill(dt)
                    Return dt
                End Using
            End Using
        End Using
    End Using
End Function

Screenshot