Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

Posted

Clearing a datagrid

 

Give this a try:

 

DataGrid1.DataSource = Nothing

Being smarter than you look is always better than looking smarter than you are.
Posted

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

Posted
I guess that would be the quick route, huh?Guess it depends on how much you like to type! ;)
Being smarter than you look is always better than looking smarter than you are.
Posted

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

  • *Experts*
Posted

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

  • Leaders
Posted

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.

Posted

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

Posted

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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...