Click here to Skip to main content
16,019,107 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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..
Posted
Comments
[no name] 24-Jun-12 19:07pm    
Help you with what? You describe a problem but did not bother posting the code that you are having trouble with. Or ask a question for that matter.

"u" and "plz" are not words in the English language.
saeel chary 25-Jun-12 3:45am    
i am extremely sorry for using such language.Do you have any suggestion for above query.
here is my code.

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
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

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)
 
Share this answer
 
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.
 
Share this answer
 
This problem is coming because your Dataset is not getting cleared. Always make a habbit to clear dataset before using.

C#
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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900