Click here to Skip to main content
15,033,147 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a datagridview having 2 buttons ADD and VIEW.
On click of VIEW button, it fetches the data from .csv file.
Now I want to change a column into combobox at run time means if I click on ADD button it will show a new row and on 2 columns it shows the combobox which is having some dropdown values in it(user selects the value from dropdown).

The below code is able to read a file into datagrid but what should I do to add a new row with combobox.
VB
Public Class frmGrid
    Dim dt As DataTable = New DataTable
    Dim ds As DataSet = New DataSet
    Dim dv As DataView = New DataView
    ' Dim dgv2Cb As New DataGridViewComboBoxCell
    Dim ColumnCB As New DataGridViewComboBoxColumn
    Dim ChkBox As New DataGridViewCheckBoxColumn
    
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim fName As String = ""
    Dim strline As String
    Dim charArray As Char = ","
    Dim strArray() As String

    OpenFileDialog1.FilterIndex = 2
    OpenFileDialog1.RestoreDirectory = True

    If (OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK) Then

        fName = OpenFileDialog1.FileName
        Dim aFile As FileStream = New FileStream(fName, FileMode.Open)
        Dim sr As StreamReader = New StreamReader(aFile)

        strline = sr.ReadLine
        strArray = strline.Split(charArray)
        For i As Integer = 0 To strArray.GetUpperBound(0)
            dt.Columns.Add(strArray(i).Trim())
            dt.TableName = "BPSurv"
           
        Next
        Me.grdTest.DataSource = dt
        strline = sr.ReadLine()
        ChkBox.Name = "Select"
        ChkBox.Selected = False
        grdTest.Columns.Add(ChkBox)
        While strline <> Nothing
            strArray = strline.Split(charArray)
            Dim dr As DataRow = dt.NewRow
            'Dim dc As DataColumn
            For i As Integer = 0 To strArray.GetUpperBound(0)
                'Dim dgvc As DataGridViewComboBoxCell
                dr(i) = strArray(i).Trim
            Next
            dt.Rows.Add(dr)
            strline = sr.ReadLine()

        End While

    End If
    
End Sub
Posted
Updated 13-Mar-13 0:08am
v3
Comments
Karthik Harve 13-Mar-13 6:08am
   
[Edit] pre tags added.

1 solution

One thing that you could do is add the combobox columns at design time, set their visible property to false, then set it to true at run-time when the ADD button is clicked.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900