get value in combobox

Blesson

Member
Joined
Jan 2, 2009
Messages
18
Programming Experience
Beginner
I am new to VB.Net and i have a problem. i have created a combobox in a datagrid.
Now i want to view/display/retrieve the value in the combobox. but i am not able to do it .

In VB6.0 we had an option like combobox.Text to get the text but in VB.NET there is no such option for a combox in a datagird.

I am using Visual Studio 2008 and 3.5 .NET framework.

thanks in advance

John A. Blesson
 
You can use the Value, or FormattedValue if you have different value/display members.
VB.NET:
Dim display As String = Me.DataGridView1.CurrentRow.Cells(0).FormattedValue.ToString
 
Datagrid Issue

Let me first make my objective clear. I have a datagrid, and the first column is Item Code and it is a combo box. The second column is Item Description and it is Text box. What i want is, when i select an Item code from the combo box, the Item description must be populated correspondingly from the Ms Acesss Databse.

The Name of the colum is Col_ItemCode and name of the Datagrid is Datagrid_Billing.

This is my code
================

VB.NET:
Private Sub dgvEquipments_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles Datagrid_Billing.EditingControlShowing

Dim cb As ComboBox = TryCast(e.Control, ComboBox)

        If cb IsNot Nothing Then

            AddHandler cb.SelectedIndexChanged, AddressOf ComboBox_SelectedIndexChanged
        End If

End Sub


Private Sub ComboBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)

Dim cb As ComboBox = sender

sqlstatement="SELECT ITEM_DESC FROM ITEM_details where item_code='" & Me.Datagrid_Billing.CurrentRow.Cells(0).FormattedValue.ToString()  & "'"

executequery(sqlstatement)

        If cb.SelectedItem IsNot Nothing Then
            Me.Datagrid_Billing.CurrentRow.Cells(Me.Col_ItemDesc.Index).Value = rs.fields(0).value



        Else
            Me.Datagrid_Billing.CurrentRow.Cells(Me.Col_ItemDesc.Index).Value = ""

        End If
    End Sub




The problem is that Me.Datagrid_Billing.CurrentRow.Cells(0).FormattedValue.ToString() is returning NULL string and so the query fails and causes a run time error.


If it was VB6.0 i would have written it as "Col_ItemCode.text" which would have not caused a problem. But in VB.Net I am unable do that.


Please help me.
 
Got it

Hi,
I got it . the code i used is :


VB.NET:
Private Sub DataGridView1_CellValueChanged(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellValueChanged

    If Me.Visible = True Then


        Dim ColumnIndex

        ColumnIndex = DataGridView1.CurrentCell.ColumnIndex
        If ColumnIndex = 2 Then
            IndexValue = DataGridView1.CurrentRow.Cells(2).Value
            DataGridView1.CurrentRow.Cells(3).Value = IndexValue
            DataGridView1.CurrentRow.Cells(4).Value = IndexValue
        End If
        If ColumnIndex = 3 Then
            IndexValue = DataGridView1.CurrentRow.Cells(3).Value
            DataGridView1.CurrentRow.Cells(2).Value = IndexValue
            DataGridView1.CurrentRow.Cells(4).Value = IndexValue
        End If
        If ColumnIndex = 4 Then
            IndexValue = DataGridView1.CurrentRow.Cells(4).Value
            DataGridView1.CurrentRow.Cells(2).Value = IndexValue
            DataGridView1.CurrentRow.Cells(3).Value = IndexValue
        End If
    Else
        Exit Sub
    End If
End Sub

Private Sub DataGridView1_CurrentCellDirtyStateChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DataGridView1.CurrentCellDirtyStateChanged
    DataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit)
End Sub
How do i add the values of a particualr column in VB.NET ( Visual studio 2008, .NET3.5)
 
You know.. I wouldn't have gone this route. Instead I'd make both columns a combobox type, but only one is editable/shows a dropdown. They would both be bound through the same bindingsource, thus using the same data. Changing one would cause a change in the other
 
Take a read of the DW2 link in my signature, section on Creating a Simple Data App. Pay particular attention to what happens when you drop a datagridview on a form
 
Hi,

I am creating a retail application. i have a datagird that has item code , item desc, price ...etc.

after i have entered all the details for a customer and i click the refresh button i want the data grid to be cleared or all the previous entries so i can enter the data for new cusotmer. i would i appreciate if i wil be able to delete all the rows on the click of refresh button or keeping the rows delete only the entries.

i tried using refresh method but it did not work.

i am also unable to delete a row or all the rows at run time.

please help me.

thanks in advance


John A. Blesson
Software Engineer.
 
hi,
i got it. my code is

For icounter = 0 To (Datagrid_Billing.Rows.Count - 1)
Datagrid_Billing.Rows.Remove(Datagrid_Billing.CurrentRow)
Next


However i would appreciate any other better solution to this.
 
Back
Top