大家好, 我在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");
更多推荐
需要动态获取列作为输出
发布评论