aletheia Posted June 27, 2003 Posted June 27, 2003 Hi everyone, I have an easy question (but I'm going crazy!): I do a query and I put data in a DataGrid; if I make another query on another table (WITH DIFFERENT COLUMNS NAMES) it shows me the columns of the 1st query, too! How can I clear the STRUCTURE of the DataGrid? Thank you very much :) Quote
Mothra Posted June 27, 2003 Posted June 27, 2003 Clearing a datagrid Give this a try: DataGrid1.DataSource = Nothing Quote Being smarter than you look is always better than looking smarter than you are.
JABE Posted June 27, 2003 Posted June 27, 2003 I did a simple test; I just assigned the DataSource to the other datatable and the grid was correctly refreshed: DataGrid1.DataSource = myDS.Tables("OtherTable") Quote
Mothra Posted June 29, 2003 Posted June 29, 2003 I guess that would be the quick route, huh?Guess it depends on how much you like to type! ;) Quote Being smarter than you look is always better than looking smarter than you are.
aletheia Posted June 30, 2003 Author Posted June 30, 2003 No, no, it doesn't work anyway!!! Here is my sub: what's wrong??? Private Sub btnVai_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnVai.Click Try ds.Clear() ds.Dispose() DataGrid1.DataSource = Nothing cn.ConnectionString = cnString cn.Open() If txtQuery.SelectedText = "" Then da.SelectCommand.CommandText = txtQuery.Text Else da.SelectCommand.CommandText = txtQuery.SelectedText End If da.Fill(ds, "Temp") DataGrid1.DataSource = ds DataGrid1.DataMember = "Temp" lblRighe.Text = ds.Tables("Temp").Rows.Count & " righe selezionate" Catch ex As OleDb.OleDbException MsgBox("Istruzione SQL non valida", MsgBoxStyle.Critical, "Errore di sintassi SQL") Finally cn.Close() cn.Dispose() End Try End Sub Quote
aletheia Posted June 30, 2003 Author Posted June 30, 2003 Pleaseee... help me... nobody seems to know... Call Microsoft... call someone.... :eek: Quote
JABE Posted June 30, 2003 Posted June 30, 2003 Try specifying a different datatable name instead of using "Temp" for both queries. Quote
*Experts* jfackler Posted June 30, 2003 *Experts* Posted June 30, 2003 Alethia, If a Data Table is created by a DataAdapter's .Fill or .FillSchema method, the Columns collection will be generated automatically. Your DataTable "Temp" is persisting the column names try: If txtQuery.SelectedText = "" Then da.SelectCommand.CommandText = txtQuery.Text Else da.SelectCommand.CommandText = txtQuery.SelectedText 'my assumption is this if/else allows you to create the query you described ds.Tables("Temp").Columns.Clear() 'this will clear the columns collection and with the da.Fill(ds, "Temp") call below, the "Temp" table column collection will be recreated. End If da.Fill(ds, "Temp") DataGrid1.DataSource = ds DataGrid1.DataMember = "Temp" lblRighe.Text = ds.Tables("Temp").Rows.Count & " righe selezionate" Catch ex As OleDb.OleDbException Jon Quote
Leaders dynamic_sysop Posted June 30, 2003 Leaders Posted June 30, 2003 you need to specify the table , rather than just "Ds" i think . eg: 1 of my form's i can update the datagrid to show the table i wish like this : DBAdapt.Fill(DBset, "UserTable") DataGrid1.DataSource = DBset.Tables("UserTable")'/// update the datagrid to hold the new dataset. hope this helps. Quote
*Experts* jfackler Posted July 1, 2003 *Experts* Posted July 1, 2003 Could you please post the code you used so future users can learn from this thread also? Thanks, Jon Quote
aletheia Posted July 1, 2003 Author Posted July 1, 2003 Jon, this sub works fine: Private Sub btnVai_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnVai.Click Try ds.Clear() ds.Dispose() ds = New DataSet() DataGrid1.DataSource = Nothing DataGrid1.DataMember = Nothing DataGrid1.SetDataBinding(Nothing, "") cn.ConnectionString = cnString cn.Open() If txtQuery.SelectedText = "" Then da.SelectCommand.CommandText = txtQuery.Text Else da.SelectCommand.CommandText = txtQuery.SelectedText End If da.Fill(ds, "Temp") DataGrid1.DataSource = ds DataGrid1.DataMember = "Temp" lblRighe.Text = ds.Tables("Temp").Rows.Count & " righe selezionate" Catch ex As OleDb.OleDbException MsgBox("Istruzione SQL non valida", MsgBoxStyle.Critical, "Errore di sintassi SQL") Finally cn.Close() cn.Dispose() End Try End Sub Quote
DayWalker Posted July 9, 2003 Posted July 9, 2003 Try this its quicker. If you filling the datagrid via a dataset. If ds.tables.contains("TableName") then ds.tables.clear() Dunno might be out of my league here Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.