通过选择usnig组合框在一个datagridview中显示多个表 [英] to display multiple tables in one datagridview by selecting usnig combobox

查看:83
本文介绍了通过选择usnig组合框在一个datagridview中显示多个表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在数据库中有多个表,并且我正在使用一个datagridview,我想通过使用组合框选择一个特定的表来显示特定表.该组合框将包含数据库中的表列表.我编写的代码仅适用对于一个表,即,当您单击按钮时,它仅显示一个表,但是当您从组合框中选择另一个表并单击按钮时,它显示同一表,而不会刷新表的内容.

请帮帮我..

i have multiple tables in database and i am using one datagridview in which i want to display a particular table by selecting it using a combo box.the combo box will contain list of tables in database.The code which i wrote works only for one table i.e. it displays only one table when u click on button but when u select other table from combo box and click on button it shows same table without refreshing contets of tables.

plz help me out..

thanx in advance..

推荐答案

尝试将所有表加载到DataSet中,然后根据您的选择更改DataGridView的DataSource属性,例如

DataGridView1.DataSource = dataSet.Tables(ComboBox.SelectedIndex)
Try loading all tables into a DataSet and According to your selection Change the DataSource Property of DataGridView like

DataGridView1.DataSource= dataSet.Tables(ComboBox.SelectedIndex)




为什么每次都动态添加DataGridView?为什么在设计器中没有一个gridview并只是按解决方案1中的说明更改DataGridView的datasource属性.
Hi,

why are you adding the DataGridView Dynamically every time? why dont you have only one gridview in the designer and just change the datasource property of the DataGridView as given in Solution 1.


由于未清除您的数据集,因此出现了此问题.使用之前,请务必先进行操作以清除数据集.

This problem is coming because your Dataset is not getting cleared. Always make a habbit to clear dataset before using.

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Dim fl As Integer=0 
Dim t1 As Integer = TextBox1.Text 
Dim waterarr(200) As String
sqlconn.ConnectionString = "Data Source=ADMIN-PC;Initial Catalog=TABLE NAME;User Id=sa;Password=;MultipleActiveResultSets=True" 
Try 
sqlconn.Open() 
Catch ex As Exception 
MsgBox(ex.Message, "Connection is failed") 
End Try 
For i As Integer = 0 To t1 - 1 
TABLEarr(i) = "table" & i.ToString 
Next 
Dim adapter1 As New SqlDataAdapter() 
Dim ds As New DataSet() 
fl = ComboBox1.SelectedIndex

ds.Clear() // Add this line, hope your problem will get solved.

Dim strCmd3 As String = "SELECT * FROM" + "[" + TABLEarr(fl) + "]" 
sqlCmd3 = New SqlCommand(strCmd3, sqlconn) 
adapter1.SelectCommand = sqlCmd3 
adapter1.Fill(ds, TABLEarr(fl)) 
Dim DataGridView1 As New DataGridView 
Dim x As Integer = 100, y As Integer = 200 
DataGridView1.Location = New System.Drawing.Point(x, y) 
DataGridView1.Width = 250 
DataGridView1.Height = 260 
DataGridView1.DataSource = ds.DefaultViewManager 
DataGridView1.DataSource = ds.Tables(waterarr(fl)) 
DataGridView1.Visible = True 
Me.Controls.Add(DataGridView1) 
sqlconn.Close() 
End Sub




谢谢
灰烬




Thanks
Ashish


这篇关于通过选择usnig组合框在一个datagridview中显示多个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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