本文介绍了使用现有列(包括组合框列)从数据库填充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 解决方案 祝你好运! Hello,I wanted to fill a datagridview from database, the datagridview has colums Product,Qty,Price,Total,DescriptionThe 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 tablesproduct have fields product_id,product_namedailysale have fields product_id,qty,price,total,descriptionI have a datagridview with having columns 1. Product, 2. Qty, 3. Price, 4. Total 5. Desc, and 6. billnoTable dailysale can retrieve product_name based on product_idThe 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 objectWhat 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.ExecuteReaderdim i as Integer = 0 Do While dr.Read = Truei+=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 += 17Loop DataGridView1.Height += y 解决方案 Good luck! 这篇关于使用现有列(包括组合框列)从数据库填充datagridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-28 03:00