Click here to Skip to main content
14,736,834 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)

I wanted to fill a datagridview from database, the datagridview has colums Product,Qty,Price,Total,Description

The Product Column is a Combobox in which I want product_name as displayMember and product_id as ValueMember but the select sholud be according to the query.

I tried the below but its not working, I want this for update, as if the user wants to change the product or qty etc.

Thanks in advance.

Sorry if I did clearly described what I wanted to do becoz of my English, then I try to explain it once again.

I have mysql tables

product have fields product_id,product_name
dailysale have fields product_id,qty,price,total,description

I have a datagridview with having columns 1. Product, 2. Qty, 3. Price, 4. Total 5. Desc, and 6. billno

Table dailysale can retrieve product_name based on product_id

The Product Columns is a ComboBox in which the Display member is product_name and the value member is product_id, I save product_id in dailysale.

Now What I want is to that When a user wanted to edit any sale order, He enter a billno and the bill retrieves into the datagridview where I dont know how to retrieve the products in the combobox cell.

I tried the above code but in the below line it say object reference is not set to an instance of an object

What I have tried:

sql = "SELECT product.product_name,dailysale.product_id,dailysale.qty, dailysale.price,, dailysale.description FROM dailysale INNER JOIN product ON product.ID = dailysale.product_id WHERE billno ='" & txtbillno.Text & "'"
cmd = new MysqlCommand(sql,conn)
dr = cmd.excmd = New MySqlCommand(sqL, conn)
                dr = cmd.ExecuteReader

dim i as Integer = 0
 Do While dr.Read = True
                    Dim dgvcc As New DataGridViewComboBoxCell
                    dgvcc = DirectCast(DataGridView1.Rows(DataGridView1.CurrentRow.Index).Cells(1), DataGridViewComboBoxCell)
                    Dim dt As DataTable = New DataTable

                With dgvcc
                        .DataSource = dt
                        .ValueMember = "product_id"
                        .DisplayMember = "product_name"
                        .Selected = ""
                End With
                    DataGridView1.Rows.Add(i, dgvcc.Value, dr("qty"), dr("price"), dr("total"), dr("description"))
                    y += 17
					 DataGridView1.Height += y
Updated 24-May-17 20:31pm
Maciej Los 24-May-17 13:55pm
"but its not working" - is not descriptive at all!
[no name] 24-May-17 14:05pm
It becomes a waste of our time to continually answer people that do not listen to what they are told.
nyt1972 26-May-17 0:12am
Ok thanks a lot

1 solution

You didn't explain what's wrong with your code. I suspect that DataGridViewComboBoxColumn data does not correspond to the data in specific row.

I'd suggest to read this: DataGridViewComboBoxColumn.DataSource Property (System.Windows.Forms)[^]

MSDN wrote:
Getting or setting this property gets or sets the DataSource property of the object returned by the CellTemplate property. Setting this property also sets the DataSource property of every cell in the column and refreshes the column display. To override the specified value for individual cells, set the cell values after you set the column value.

Good luck!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900