Results 1 to 7 of 7

Thread: [RESOLVED] [2008] Datagridview & comboboxcell

  1. #1

    Thread Starter
    Fanatic Member onlyGirl's Avatar
    Join Date
    Sep 2006
    Location
    Houston, TX
    Posts
    743

    Resolved [RESOLVED] [2008] Datagridview & comboboxcell

    I have a datagridview that has a comboboxcell in a column and I'm having trouble retrieving the value from it. The comboboxcell has a different datasource from the other data on the datagridview. Can anybody tell me what I'm doing wrong? I only retrieve blank info from it using the code below:
    Code:
     Private Sub DataGridView1_DefaultValuesNeeded(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowEventArgs) Handles DataGridView1.DefaultValuesNeeded
            Me.GetAPI()
            cmbAPI.DataSource = dsAPI.Tables("Well_Header")
            cmbAPI.ValueMember = "API"
            e.Row.Cells(0) = cmbAPI
            
        End Sub
    Code:
    Public Sub GetDefaultVal()
            If FutWellType = "Production" Then
    
                prodStartDt = DataGridView1.Rows(lasRow).Cells(1).Value.ToString() 'StartDate
                prodEndDt = DataGridView1.Rows(lasRow).Cells(2).Value.ToString() 'EndDate
                prodIniRt = DataGridView1.Rows(lasRow).Cells(3).Value.ToString() 'InitialRate
                prodFinRt = DataGridView1.Rows(lasRow).Cells(4).Value.ToString() 'FinalRate
                prodDecRt = DataGridView1.Rows(lasRow).Cells(5).Value.ToString() 'DeclineRate
                prodDecTy = DataGridView1.Rows(lasRow).Cells(6).Value.ToString() 'DeclineType
                prodHypFac = DataGridView1.Rows(lasRow).Cells(7).Value.ToString() 'HypFactor
                prodGasOilRt = DataGridView1.Rows(lasRow).Cells(8).Value.ToString() 'GasOilRatio
                prodExtDt = DataGridView1.Rows(lasRow).Cells(9).Value.ToString() 'ExtendDate
                prodGasOil = DataGridView1.Rows(lasRow).Cells(10).Value.ToString() 'GasOil
                prodConFlVl = DataGridView1.Rows(lasRow).Cells(11).Value.ToString() 'ConstFlVol
                prodInitWtr = DataGridView1.Rows(lasRow).Cells(12).Value.ToString() 'InitialWater
            ElseIf FutWellType = "Injection" Then
                'injAPI = DataGridView1.Rows(newRow).Cells(0).Value.ToString()
                injStartDt = DataGridView1.Rows(lasRow).Cells(1).Value.ToString()
                injEndDt = DataGridView1.Rows(lasRow).Cells(2).Value.ToString()
                injInitRt = DataGridView1.Rows(lasRow).Cells(3).Value.ToString()
                injFinRt = DataGridView1.Rows(lasRow).Cells(4).Value.ToString()
                injDeclineRt = DataGridView1.Rows(lasRow).Cells(5).Value.ToString()
                injDeclTyp = DataGridView1.Rows(lasRow).Cells(6).Value.ToString()
                injHypFactor = DataGridView1.Rows(lasRow).Cells(7).Value.ToString()
                injExtendDt = DataGridView1.Rows(lasRow).Cells(8).Value.ToString()
            End If
    Code:
     Public Sub AutoFillRow()
            Dim cmdAutoFillRow, cmdAutoFillRowInj As New OleDbCommand
            cmdAutoFillRow.Connection = gConn
            cmdAutoFillRow.Transaction = rlBack
            cmdAutoFillRowInj.Transaction = rlBack
    
            If FutWellType = "Production" Then
                cmdAutoFillRow.CommandText = "INSERT INTO Param_Prod " _
                & "(API, StartDate, EndDate, Reservoir, " _
                & "InitialRate, FinalRate, DeclineRate, DeclineType, " _
                & "HypFactor, ExtendDate, GasOilRatio, GasOil, " _
                & "ConstFlVol, TemplateName, LastUpdated, InitialWater)" _
                & "VALUES (@prAPI, @prStartDt, @prEndDt, '" & Resvr & "', " _
                & "@prInitialRt, @prFinalRt, @prDeclineRt, @prDeclineTy, " _
                & "@prHypFactor, @prExtendDate, @prGasOilRat, @prGasOil, " _
                & "@prConstFlVol, '" & PrmName & "', 'Y', @prInitialWater)"
    
                cmdAutoFillRow.Parameters.AddWithValue("@prAPI", cmbAPI.Value.ToString())
                cmdAutoFillRow.Parameters.AddWithValue("@prStartDt", prodStartDt)
                cmdAutoFillRow.Parameters.AddWithValue("@prEndDt", prodEndDt)
                cmdAutoFillRow.Parameters.AddWithValue("@prInitialRt", prodIniRt)
                cmdAutoFillRow.Parameters.AddWithValue("@prFinalRt", prodFinRt)
                cmdAutoFillRow.Parameters.AddWithValue("@prDeclineRt", prodDecRt)
                cmdAutoFillRow.Parameters.AddWithValue("@prDeclineTy", prodDecTy)
                cmdAutoFillRow.Parameters.AddWithValue("@prHypFactor", prodHypFac)
                cmdAutoFillRow.Parameters.AddWithValue("@prExtendDate", prodExtDt)
                cmdAutoFillRow.Parameters.AddWithValue("@prGasOilRt", prodGasOilRt)
                cmdAutoFillRow.Parameters.AddWithValue("@prGasOil", prodGasOil)
                cmdAutoFillRow.Parameters.AddWithValue("@prConstFlVol", prodConFlVl)
                cmdAutoFillRow.Parameters.AddWithValue("@prInitialWater", prodInitWtr)
                cmdAutoFillRow.ExecuteNonQuery()
    EndIf
    Using Visual Studio 2008

    Please mark your thread RESOLVED if you no longer need help.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2008] Datagridview & comboboxcell

    Just like any cell in a DataGridView, you get the value in a DataGridViewComboBoxCell from its Value property.

    Here's how a DataGridViewComboBoxCell works. Just like all other cells in a DataGridView it doesn't contain a control by default. It simply renders its Value. Now, when you start editing a cell a control is created, if one does not already exist, and it is embedded in the cell. In the case of a combo box cell, the control's DataSource, DisplayMember, ValueMember and Items properties are set from cell's properties, which are set from the column's properties, which you set yourself. The cell's Value property is then assigned to the control's SelectedValue property. You then merrily edit the combo box itself without affecting the cell hosting it. When you press Enter or leave the cell the editing sessions ends. At that point the control is removed from the cell and its SelectedValue property is assigned to the cell's Value property.

    In principle, this is how ALL data cells in a DataGridView work. The only difference is what property values get passed from the cell to the editing control and then back to the cell. Regardless of the type of cell, you get its value from its rather aptly named Value property.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Fanatic Member onlyGirl's Avatar
    Join Date
    Sep 2006
    Location
    Houston, TX
    Posts
    743

    Re: [2008] Datagridview & comboboxcell

    Quote Originally Posted by jmcilhinney
    Just like any cell in a DataGridView, you get the value in a DataGridViewComboBoxCell from its Value property.

    Here's how a DataGridViewComboBoxCell works. Just like all other cells in a DataGridView it doesn't contain a control by default. It simply renders its Value. Now, when you start editing a cell a control is created, if one does not already exist, and it is embedded in the cell. In the case of a combo box cell, the control's DataSource, DisplayMember, ValueMember and Items properties are set from cell's properties, which are set from the column's properties, which you set yourself. The cell's Value property is then assigned to the control's SelectedValue property. You then merrily edit the combo box itself without affecting the cell hosting it. When you press Enter or leave the cell the editing sessions ends. At that point the control is removed from the cell and its SelectedValue property is assigned to the cell's Value property.

    In principle, this is how ALL data cells in a DataGridView work. The only difference is what property values get passed from the cell to the editing control and then back to the cell. Regardless of the type of cell, you get its value from its rather aptly named Value property.
    I have also tried the code that specifies the row of where the combobox is located and it still shows up blank. It is retrieving the value but the value is coming back blank. Intellisence also doesn't show me SelectedValue as an option for the combobox.
    Last edited by onlyGirl; May 28th, 2008 at 10:04 AM.
    Using Visual Studio 2008

    Please mark your thread RESOLVED if you no longer need help.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2008] Datagridview & comboboxcell

    You seem to have completely missed the point. In the very first sentence of my reply I have you the solution. You get a cell's value from its Value property, end of story. The rest was just some background to try to enlighten you about how the cell interacts with the control. You don't actually need any of that to get or set a cell's Value.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Fanatic Member onlyGirl's Avatar
    Join Date
    Sep 2006
    Location
    Houston, TX
    Posts
    743

    Re: [2008] Datagridview & comboboxcell

    Quote Originally Posted by jmcilhinney
    You seem to have completely missed the point. In the very first sentence of my reply I have you the solution. You get a cell's value from its Value property, end of story. The rest was just some background to try to enlighten you about how the cell interacts with the control. You don't actually need any of that to get or set a cell's Value.
    No I didn't miss the point. I know what exactly you were saying. Although that didn't work for me. I don't know why but it didn't. I had to add a handler in order to retrieve the value.
    Using Visual Studio 2008

    Please mark your thread RESOLVED if you no longer need help.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2008] Datagridview & comboboxcell

    Quote Originally Posted by onlyGirl
    No I didn't miss the point. I know what exactly you were saying. Although that didn't work for me. I don't know why but it didn't. I had to add a handler in order to retrieve the value.
    Huh? You get a cell's value from its Value property, end of story. Where or when you do it is another matter because it depends where and when you need it. There is absolutely no reason that should have been trying to get a SelectedValue, which you said you were. As I explained earlier, it is the SelectedValue of the ComboBox control embedded in the cell that gets pushed down to the cell's Value property, but ONLY once the user has ended the edit session. The edit session will end when the user leaves the cell and the ComboBox control is removed. I think you'll find that pressing Enter will do so also.

    So, the fact that you had to add an event handler is irrelevant. You would still be getting the cell's value from its Value property within that event handler. I didn't say anything about where or when you get the value, just that it is gotten from the Value property.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7
    Frenzied Member
    Join Date
    Feb 2004
    Location
    in the heart of the Mediterranean
    Posts
    1,143

    Re: [2008] Datagridview & comboboxcell

    I dont know whether this is referring to the same problem I had about a week ago. JMC had indicated to me to use the

    comboname.text --------- to get the actual data.

    See if this works for you too. I have a limited knowledge of VB.Net so pardon me if I misunderstood your point.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width