Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to set selected index for DataGridViewTextBoxColumn

Status
Not open for further replies.

annarene

Programmer
Jul 7, 2008
17
US
Hello: I'm currently working with an unbound datagridview and when the form loads, I populate the DataGridViewTextBoxColumns using code similar to this:

I need to set the selected index and I'm not sure how to do this. I don't see a selectedIndex property for this control.


Code:
  Sub PopulateSpeed(ByVal iColIndex)
        Dim myDB As New OleDb.OleDbConnection(sConnectionString)
        Dim myDA As New OleDb.OleDbDataAdapter("Select Speed from tblSpeed order by Speed", myDB)
        Dim myDS As New Data.DataSet
        Dim cbn As DataGridViewComboBoxColumn = dgProfiles.Columns(iColIndex)

        myDA.Fill(myDS, "tblSpeed")
        With cbn
            .DataSource = myDS.Tables("tblSpeed")
            .DisplayMember = "Speed"
            .ValueMember = "Speed"
        End With
        myDB.Close()
    End Sub



Any help is truly appreciated.
annarene
 
You can't set the selected index. Now what you can do depends on what you want to do. Are you trying to select a row, trying to pull data from a specific row, or did you mean you wanted to know how to put all of the data into the data grid?

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Hi Sorwen: Thanks for your reply. I just noticed that I put
DataGridViewTextBoxColumn and I meant to put
DataGridViewComboBoxColumn in my post. I apologize for my sloppy post.

I wanted to set the selected index for each drop-down in my grid so when the form first loads, each drop-down is set to "Select"


I think I just figured out how to accomplish this using the following code:

Code:
 Private Sub LoadWheelDataGrid_New()

        With Me.dgColorWheel.Rows
            .Add()
            .Add()
            .Add()
            .Add()
        End With

        For j As Integer = 0 To (Me.dgColorWheel.RowCount - 1)
            For i As Integer = 0 To Me.dgColorWheel.ColumnCount - 1
                Select Case i
                    Case 0
                        Me.dgColorWheel.Item(i, j).Value = j + 1
                    Case Else
                        Me.dgColorWheel.Item(i, j).Value = "Select"
                End Select
            Next
        Next


    End Sub

Before I resolved this issue, when the form first loaded, it populated my drop-downs; however, it displayed a blank item in each drop-down, and was causing issues on save.

anyway, thanks again for your time in posting and I apolgize again for not posting correctly.

annarene
 
I wanted to set the selected index for each drop-down in my grid so when the form first loads, each drop-down is set to "Select"
So if I under stand in each row of the DataGrid you have a ComboBox and you want that ComboBox to say Select? If that is the case the best way depends on how you are loading the data into the DataGrid.

I have no clue what you are trying to say about the color wheel or how you are relating it to your question.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Sorwen, she has a lot of little posts all about the same issue.

Annarene, when you are loading the comboboxes into the datagridrow you can specify the selectedIndex.

-Sometimes the answer to your question is the hack that works
 
I though I was goind to have to totally retract what I said, but nope. There is a way to do it as is. Really with the way you did it most of your datagrid is unbound, but when you did .DataSource = myDS.Tables("tblSpeed") you just late bound that ComboBox. I was going to have to suggest that you add "Select" as a value to your table, but I did find another way. Do this:

Code:
Sub PopulateSpeed(ByVal iColIndex)
        Dim myDB As New OleDb.OleDbConnection(sConnectionString)
        Dim myDA As New OleDb.OleDbDataAdapter("Select Speed from tblSpeed order by Speed", myDB)
        Dim myDS As New Data.DataSet
        Dim cbn As DataGridViewComboBoxColumn = dgProfiles.Columns(iColIndex)

        myDA.Fill(myDS, "tblSpeed")
        With cbn
            .DataSource = myDS.Tables("tblSpeed")
            .DisplayMember = "Speed"
            .ValueMember = "Speed"
            [RED].DefaultCellStyle.NullValue = "Select"[/RED]
        End With
        myDB.Close()
    End Sub
That should get what I think you are asking for.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Sorwen, she has a lot of little posts all about the same issue.
Posting at the same time. I saw the one about color, but kept away from it because I don't know anything about coloring anything in a DGV.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
@Sorwen, Annarene is working on a very complex page that has to do a lot of things that I have never tried to mix in that format. All the little pieces, though, are not that bad.

Things like a combobox in a datagrid is still a combobox, so you should be able to treat it as such.

@Annerene, does Sorwen's suggestion do what you needed?

-Sometimes the answer to your question is the hack that works
 
Ahhhh....I get it now. That was where all the color wheel stuff was coming from. I'm slow today. I keep frying my brain on my own projects the last few days so people here get what brain cells are left. :)

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Sorwen and Qik3Coder, thank you both for all the great input and responses.

I did try to use the following as suggested:


.DefaultCellStyle.NullValue = "Select"

however, here's what I experienced when I did that:
the page loads and the combos in the dg are set to "Select"
; this is perfect;however, after, i make a selection from one of the combos.
Then, I change my mind about the selection I just made...I didnt' mean to select anything...so want to set it back to "Select". "select is no longer there...."


after I tried this, I ended up placing "Select" in my tables and if the user clicks the New button, I set the dropdown's selected value to "Select" via the following code placed in my sub LoadWheelDataGrid_New sub seen above...

This seems to be working perfect now...so I believe my issue is now resolved!!! yeah!!!

thank you both again for the help you've given me. I'm the only programmer at my work, so it really helps and I appreciate getting help from you.

annarene
 
Yea, the way I showed was simply for no value selected. For something where you must have a value it is one step in helping to make sure your likely to have a value. Adding it to the table really sounds like the best way for what you want if you want to allow them to select a no value. Either way you likely want to code to ignore values of select.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top