Datagridview with Combobox


R

Robert

Using VB.Net 2008

I have this code in a form, to create the cells on my datagridview.
Depending on the users selection, the grid is created.

This sample is for the Creditors.

Snipett:
The following works and populates the combo box, and the text boxes.
Need to find the ID value to enter it in the txtgrFixedID field once an
asset
is selected.


Try
With frmMain.grdSelections

..Columns.Clear()

'The commented code works to add a combo to the grid.

Dim txtgrFixedID As New DataGridViewTextBoxColumn

Dim colgrExpress As New DataGridViewComboBoxColumn

Dim txtgrMnthly As New DataGridViewTextBoxColumn

Dim txtgrYearly As New DataGridViewTextBoxColumn

txtgrFixedID.Name = "AssetsID"

With colgrExpress

'.Name = "Select a Variable Expense"

..HeaderText = "Select an Asset"

..ReadOnly = False

..AutoComplete = True

..DropDownWidth = 250

End With

With txtgrMnthly

..Name = "Value of Asset"

..DefaultCellStyle.NullValue = FormatNumber("0.00")

..ReadOnly = False

End With

With txtgrYearly

..Name = "Maturity Date"

..DefaultCellStyle.NullValue = Format(Now(), "MMM dd, yyyy")

..ReadOnly = True

End With

'Open the connection.

con.Open()

Dim rea As OleDbDataReader

Dim cmd As New OleDb.OleDbCommand("SELECT * FROM AssetsItems", con)

rea = cmd.ExecuteReader

colgrExpress.Items.Clear()

'Load the DataGridView.

While rea.Read()

colgrExpress.Items.Add(rea("NAssets"))

colgrExpress.DisplayMember = "NAssets"

colgrExpress.ValueMember = "AssetsID"

End While

'Close the connection.

con.Close()

..Columns.Add(txtgrFixedID)

..Columns.Add(colgrExpress)

..Columns.Add(txtgrMnthly)

..Columns.Add(txtgrYearly)

..Columns(0).Width = 0

..Columns(1).Width = 150

..Columns(2).Width = 100

etc.

I was able to use the ItemData in VB6.

I need to be able to fnd the AssetsID when user selects a value from the
combo box.

Any tips on how to get this.

Was so easy in VB6
 
Ad

Advertisements

C

Cor Ligthert

Robert,

I had the problem that I found it so difficult in VB6.

But as I see your code you make it in my idea senseless dificult.
A simple datatable or dataset with tableadapter or dataadapter should be
enough.
Don't assume that your program becomes quicker by writting more rows of code
your own.

http://www.vb-tips.com/DataGridViewSample.aspx

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Datagridview 1

Top