如何从数据表中填充组合框 [英] How to populate a combobox from a datatable

查看:94
本文介绍了如何从数据表中填充组合框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家下午好;如果你不介意,我想问一个新问题:



我正在尝试将旧的Access 2010系统移植到我应用的.NET应用程序中用VB net 2017开发。



在Access应用程序中,从本Combo的Source Row属性填充一个组合框,其中包含以下sql语句:



Good Afternoon everybody; I'd like to ask a new question if you don´t mind:

I'm trying to emigrate an old Access 2010 system to a NET application that I´m developing with VB net 2017.

In the Access app, a combobox is populated from the Source Row property of this Combo, with the following sql statement:

SELECT
      dbo_articulos.Id_art,
      dbo_articulos.desc As Descripción,
      dbo_articulos.fecha_compra As Fecha,
      IIf(Not IsNull([dbo_articulos].[fac_Origen]),[dbo_proveedor].[nombre] & "  [Compra Nacional]",IIf([dbo_articulos_factura].[estatus] = 99,"CANCELADA","ACTIVA")) AS Status,
      dbo_articulo_proveedor.nombre
FROM
      dbo_articulos INNER JOIN dbo_proveedores ON dbo_articulos.proveedor_id = dbo_proveedores.proveedor_id
WHERE
      (((dbo_articulos_grupo) = 1))
ORDER BY
      dbo_articulos.Id ASC,
      dbo_articulos.factura ASC;



我在字符串变量(Qrye)中使用相同的代码来填充dataTable,使用此代码:




I'm using the same code in a string variable (Qrye) to fill a dataTable, with this code:

Da = New SqlDataAdapter(Qrye, Cn)
        Da.Fill(Dt)
        Combo_Articulos.DataSource = Dt



但是在代码中的'IIf'指令中(包含Not IsNull的指令),它发送我有一条错误消息告诉我错误的语法在'Not'附近,就好像它不能识别Not IsNull功能一样。



我是一直试图改变它是Is NotDBNull,IsNot Null,但错误仍然存​​在,或告诉我NotDbNull不是一个公认的功能。我想我只需要编码那里编码的Not Null函数。它在Access中运行良好,但在VB网络中没有。



我真的很感激任何人都可以给我的任何帮助,或者可能有更好的方法填充这个组合比这个;就像ADO实体一样。



我还需要显示我正在访问的所有列甚至标题,我仍然无法找到怎么做。



我尝试过的事情:



我我已经做了很多测试并改变了代码,因为我可以获得相同的结果。我甚至想过用表中的完整数据填充数据表,然后按顺序循环它并进行我需要的测试和比较,然后使用 .AddItem 方法,但是我我确定它必须是一种比这更好的方式而且更少元素。


But in the 'IIf' instruction in the code, (the one containing "Not IsNull"), it sends me an error message telling me "Incorrect Syntax Near the word 'Not'", like if it´s not recognizing the "Not IsNull" function.

I'd been trying to change it for Is NotDBNull, IsNot Null, but the error keeps there, or telling me that NotDbNull is not a recognized function. I think I'd only need how to code the "Not Null" function coded there. It works fine in Access, but noy in VB net.

I'd really appreciate any kind of help anybody could give me, or maybe there´s a better way to populate this combo than this; like an ADO Entity or something.

I also need to show all of the columns that I'm accessing and even the headers, which I just still can´t find how to do it.

What I have tried:

I've been doing a lot of tests and changing the code as far as I could get the same results. I even thought of populate the datatable with the complete data in the tables and then looping it sequentially and doing the tests and comparisons I require and then use the .AddItem method, but I'm sure it must be a better way and less elemental than this.

推荐答案

我不是VB.NET / Access专家,但也许这会回答你的问题问题: VB .net和Microsoft Access - NULL ISSUE-VBForums [ ^ ]

我认为您需要使用 IsDBNull



如果您有兴趣在 PostgreSQL 中,这里有一些关于CodeProject的有趣文章:

搜索 [ ^ ]

虽然文章有点过时,但我建议从在C#(.NET)应用程序中使用PostgreSQL(简介) [ ^ ]
I'm not a VB.NET / Access specialist, but maybe this will answer your question: VB .net and Microsoft Access - NULL ISSUE-VBForums[^]
I think you need to use IsDBNull.

If you are interested in PostgreSQL, here are some interesting articles on CodeProject:
Search[^]
Although the article is a bit outdated, I would recommend starting with Using PostgreSQL in your C# (.NET) application (An introduction)[^]


我在你的查询中看到一些错误:



I see few mistakes in your query:

SELECT
      dbo_articulos.Id_art,
      dbo_articulos.desc As Descripción,
      dbo_articulos.fecha_compra As Fecha,
      IIf(Not IsNull([dbo_articulos].[fac_Origen]),[dbo_proveedor].[nombre] & "  [Compra Nacional]",IIf([dbo_articulos_factura].[estatus] = 99,"CANCELADA","ACTIVA")) AS Status,
      dbo_articulo_proveedor.nombre
FROM
      dbo_articulos INNER JOIN dbo_proveedores ON dbo_articulos.proveedor_id = dbo_proveedores.proveedor_id
WHERE
      (((dbo_articulos_grupo) = 1))
ORDER BY
      dbo_articulos.Id ASC,
      dbo_articulos.factura ASC;







1. [dbo_proveedor ] 在当前上下文中不存在,因为表名是: dbo_proveedores

2.到什么表 dbo_articulos_grupo 字段属于哪个?

3. & [Compra Nacional] - 如果 [Compra Nacional] 是一个字段,你使用了错误的连接!



你必须确保你的查询是正确的。



我强烈建议使用表别名,例如:




1. [dbo_proveedor] does not exists in current context, becuase a table name is: dbo_proveedores
2. To what table dbo_articulos_grupo field belongs?
3. & " [Compra Nacional]" - if [Compra Nacional] is a field, you used wrong concatenation!

You have to be sure that your query is proper.

I'd strongly suggest to use table aliases, for example:

SELECT tb1.Field1, tb2.Field1
FROM TableName1 AS tb1 INNER JOIN TableName2 AS tb2 ON tb1.PrimaryKey = tb2.ForeignKey





详情请见:使用表别名 [ ^ ]


这篇关于如何从数据表中填充组合框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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