使用现有列(包括组合框列)从数据库填充datagridview [英] Populate datagridview from database with existing columns including combobox column

查看:84
本文介绍了使用现有列(包括组合框列)从数据库填充datagridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,



我想从数据库填充datagridview,datagridview有colums产品,数量,价格,总计,描述



产品列是一个Combobox,我希望product_name为displayMember,product_id为ValueMember,但select sholud应根据查询。



我试过以下但是它不起作用,我想要更新,好像用户想要更改产品或数量等





提前致谢。



如果我确实清楚地描述了对不起我想用英语做什么,然后再尝试解释一下。 

我有mysql表

产品有字段product_id,product_name
dailysale有字段product_id,数量,价格,总计,描述

我有一个datagridview有列1.产品,2.数量,3。价格,4。总计5.描述,和6. billno

表dailysale可以根据product_id
检索product_name
产品列是一个ComboBox,其中Display成员是product_name,值成员是product_id,我在dailysale中保存product_id。

现在我想要的是当用户想要编辑任何销售订单时,他输入billno并且账单检索到datagridview,我不知道如何在组合框单元中检索产品。

我尝试了上面的代码但是在下面的行中它说对象引用没有设置为对象的实例





我尝试过:



 sql =   SELECT product.product_name,dailysale.product_id,dailysale.qty,dailysale.price,dailysale.total,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 = MySqlCommand(sqL,conn)
dr = cmd.ExecuteReader

dim i as Integer = 0
span> dr.Read = True
i + = 1
Dim dgvcc 作为 DataGridViewComboBoxCell
dgvcc = DirectCast (DataGridView1.Rows(DataGridView1.CurrentRow.Index).Cells( 1 ),DataGridViewComboBoxCell)
Dim dt 作为 DataTable = DataTable

dt.Load(dr )
使用 dgvcc
.DataSource = dt
.ValueMember = product_id
.DisplayMember = product_name
.Selected =
结束 使用
DataGridView1.Rows.Add(i,dgvcc.Value,dr( qty),dr( price),dr( 总数),dr( description))
y + = 17
循环
DataGridView1.Height + = y

解决方案

您没有解释您的代码有什么问题。我怀疑 DataGridViewComboBoxColumn 数据与特定行中的数据不对应。



我建议阅读这: DataGridViewComboBoxColumn.DataSource属性(System.Windows.Forms) [ ^ ] < br $>




MSDN写道:

获取或设置此项property获取或设置CellTemplate属性返回的对象的DataSource属性。设置此属性还会设置列中每个单元格的DataSource属性并刷新列显示。 要覆盖单个单元格的指定值,请在设置列值后设置单元格值。





祝你好运!


Hello,

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.total, 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
i+=1
                    Dim dgvcc As New DataGridViewComboBoxCell
                    dgvcc = DirectCast(DataGridView1.Rows(DataGridView1.CurrentRow.Index).Cells(1), DataGridViewComboBoxCell)
                    Dim dt As DataTable = New DataTable

                    dt.Load(dr)
                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
		Loop
					 DataGridView1.Height += y

解决方案

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!


这篇关于使用现有列(包括组合框列)从数据库填充datagridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆