Important! Clear A Datagrid

aletheia

Member
Joined
Jun 27, 2003
Messages
15
Hi everyone, I have an easy question (but Im 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 :)
 
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")
 
No, no, it doesnt work anyway!!!
Here is my sub: whats 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
 
Alethia,
If a Data Table is created by a DataAdapters .Fill or .FillSchema method, the Columns collection will be generated automatically.
Your DataTable "Temp" is persisting the column names try:

Code:
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
 
you need to specify the table , rather than just "Ds" i think . eg: 1 of my forms i can update the datagrid to show the table i wish like this :
Code:
DBAdapt.Fill(DBset, "UserTable")

DataGrid1.DataSource = DBset.Tables("UserTable")/// update the datagrid to hold the new dataset.
hope this helps.
 
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
 
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
 
Back
Top