需要动态获取列作为输出

编程入门 行业动态 更新时间:2024-10-07 06:51:14
本文介绍了需要动态获取列作为输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

大家好, 我在Excel工作表中有一个带有项的列表框控件. 根据选定的项目,我将查询数据库并获取输出. 问题在于输出应该是动态的. 对于例如 清单框 --------

Hi all, I have a list box control with items in an excel sheet. Based on the selected items, i will query the database and fetch the output. The problem is that the output should be dynamic. For Eg List box --------

A B C D

表格-项目

Table - Items

A B C D E F G H -------------------------------------- 10 20 30 40 50 60 70 80 100 210 350 40 50 60 720 801 10 20 30 40 501 601 70 803

从列表框中,我将选择"A"和"B".现在,我需要表"items"中的列A和B作为输出. 同样,如果我选择"C",那么我应该只获得列C作为输出. 在此先谢谢您..

From the listbox i will select "A" and "B". Now i need columns A and B from the table "items" as Output. Similarly if i select "C", then i should get only column C as output. Thanks in advance..

推荐答案

从列表框中获取选定的项目. 我假设您有一个listbix&一键生成数据 在按钮上单击 Get selected items from listbox. I am assuming that you have one listbix & one button to generate data on button click string selectedItem = ""; if (ListBox1.Items.Count > 0) { for (int i = 0; i < ListBox1.Items.Count; i++) { if (ListBox1.Items[i].Selected) { if(selectedItem == "") { selectedItem = ListBox1.Items[i].Text + ","; } else { selectedItem = selectedItem + "," + ListBox1.Items[i].Text ; } } } } // use this query to get data SqlCommand cmd = new DqlCommand("Select "+selectedItem +" from items");

如果要动态获取列名,则 首先在列表框中添加列名 1.动态地.

If you want to get column name dynamically then First add column name in listbox 1.dynamically.

SqlCommand cmd = new SqlCommand(); cmd.Connection = conn; cmd.CommandType = CommandType.Text; cmd.CommandText = "select name from syscolumns where id = (SELECT id FROM sysobjects WHERE [Name] = 'items')"; Dataset ds =new Dataset(); SqlDataAdapter adp = new SqlDataAdapter(cmd); adp.Fill(ds); if(ds.Tables[0].Rows.Count>0) { for(int i = 0 ; i< ds.Tables[0].Rows.Count; i++) { listBox1.item.Add(ds.Tables[0].Rows[0]["name"].ToString()); } }

2.或手动添加列名称

2. or add column name manually

listBox1.item.Add("A"); listBox1.item.Add("B"); listBox1.item.Add("C"); listBox1.item.Add("D");

更多推荐

需要动态获取列作为输出

本文发布于:2023-10-23 02:36:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1519490.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:动态

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!