DataGridViewComboBoxColumn data selection
-
Hi,
If I got a datagridview like this:-------------------------------------------------------------
The column 1 has the primary key. How can I get corresponding data of the primary key in the column 2?|Column 1 |Column 2 |
|This column is |This column is |
|DataGridViewComboBoxColumn |DataGridViewTextBoxColumn |
Example:
Database table:----------------------------
If I select the number 51 in the column 1 in the datagridview the column 2 get the name Alexis Méndez. How can I do that?|id |name |
|51 |Alexis Mendez |
|12 |Viviana Melendez |
There is no properties configuration for this? -
That's not really the way it is supposed to work. The idea is that you bind a DataTable to your grid and one column is a foreign key. You would then make that column a combo box column and bind the parent table to it. That allows you to select a parent value from the combo box and have the foreign key value of that child row set to the corresponding value.
For what you're asking you shouldn't be using a grid at all. You should just have a ComboBox and a TextBox bound to the same source. Then when you select an ID in the ComboBox the TextBox will be updated automatically.
-
Hi,
I have one datagridview with the next schema---------------------------------------------------------------------- |Item # |Description |Date |Due Date |Price | ----------------------------------------------------------------------
Where the display data corresponds to the transactions table that I have in my database and the item information comes other table called items.
What I want is that if I select one Item # from the combobox the description and the price of the item displays automatically.
All the data displayed in the datagridview is going to be store in the transaction table even the description and the price of the item because some transactions not necessary come from items some can be input manually like debts that only need the debt description and the amount.
Please post code samples.
-
Hi,
I have done this, but the datagridview get the values when the combobox column lost the focus not when the value changes.
Imports System.Data
Imports System.Data.Odbc
Public Class Form1
Dim cn As OdbcConnection
Dim ds As DataSet
Private WithEvents DataGridView1 As New DataGridView()
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim ConnectionString As String = "FILEDSN=C:\Documents and Settings\Alexis\Desktop\Video System\videodb.dsn;"
cn = New OdbcConnection(ConnectionString)
cn.Open()
Dim da As OdbcDataAdapter = New OdbcDataAdapter("SELECT id, description, new_rel_charge FROM items ORDER BY id; ", cn)
ds = New DataSet
da.FillSchema(ds, SchemaType.Mapped, "Items")
da.Fill(ds, "Items")
Dim da2 As OdbcDataAdapter = New OdbcDataAdapter("SELECT item_id, description, date_done, due_date, return_date, amount_to_pay FROM rents ORDER BY id; ", cn)
da2.FillSchema(ds, SchemaType.Mapped, "Rents")
da2.Fill(ds, "Rents")
cn.Close()
dataGridView1.AutoGenerateColumns = False
DataGridView1.DataSource = ds.Tables("Rents").DefaultView
Dim ComboBoxColumn1 As New DataGridViewComboBoxColumn
With ComboBoxColumn1
.HeaderText = "ITEM #"
.DataPropertyName = "item_id"
.DataSource = ds.Tables("Items").DefaultView
.DisplayMember = "id"
End With
Dim TextBoxColumn1 As New DataGridViewTextBoxColumn
TextBoxColumn1.HeaderText = "DESCRIPTION/TITLE"
TextBoxColumn1.Width = 200
TextBoxColumn1.DataPropertyName = "description"
Dim TextBoxColumn2 As New DataGridViewTextBoxColumn
TextBoxColumn2.HeaderText = "DATE"
TextBoxColumn2.DataPropertyName = "date_done"
Dim TextBoxColumn3 As New DataGridViewTextBoxColumn
TextBoxColumn3.HeaderText = "DUE DATE"
TextBoxColumn3.DataPropertyName = "due_date"
Dim TextBoxColumn4 As New DataGridViewTextBoxColumn
TextBoxColumn4.HeaderText = "RETURN DATE"
TextBoxColumn4.DataPropertyName = "return_date"
Dim TextBoxColumn5 As New DataGridViewTextBoxColumn
TextBoxColumn5.HeaderText = "PRICE"
TextBoxColumn5.DataPropertyName = "amount_to_pay"
DataGridView1.Columns.Add(ComboBoxColumn1)
DataGridView1.Columns.Add(TextBoxColumn1)
DataGridView1.Columns.Add(TextBoxColumn2)
DataGridView1.Columns.Add(TextBoxColumn3)
DataGridView1.Columns.Add(TextBoxColumn4)
DataGridView1.Columns.Add(TextBoxColumn5)
DataGridView1.SetBounds(12, 12, 410, 362)
Me.Controls.Add(DataGridView1)
End Sub
Private Sub DataGridView1_CellValueChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellValueChanged
If e.ColumnIndex = 0 Then
Dim dr As DataRow = ds.Tables("Items").Rows.Find(DataGridView1.Item(0, DataGridView1.CurrentRow.Index).Value)
If Not dr Is Nothing Then
DataGridView1.Item(1, e.RowIndex).Value = dr("description")
DataGridView1.Item(2, e.RowIndex).Value = Now.Date
DataGridView1.Item(5, e.RowIndex).Value = dr("new_rel_charge")
End If
End If
End Sub
End Class
Post a reply
Quick links
Recent activity
- arif ahmad replied to How to receive data in web ...
- William Thompson replied to What is the name of the Win...
- Sameera Piyadigamage replied to Point of Sale Developers: H...
- Scott Carline replied to 4 x C# Developers for large...
- Rajendra Dhakal replied to Restore SQL Server text dat...
- cloud rainda replied to How to convert between TS f...
Enter your message below
Sign in or Join us (it's free).