DataGrid update issues

B

BStrick

I've looked around and haven't found a usuable solution to this issue.
I hope that you can provide me with some assistance. I have a
application that calls for the user to select a database/table to view
data from. At that point a datagrid populates with the information.
This works fine except when the user tries to either repopulate that
grid with the same table or with data from a different table.

If the user tries to repopluate from the same table then if should
simply overwrite what was there at first. If they try to populate from
a diffferent table then it should erase the old data and input the new.

Instead what I get when i try to repopulate with the same table is that
it inputs additional data starting at the last row from the frst
attempt. If I am populating the grid from a new table then it will keep
all the previous columns and add the new data to the last column on the
left from the first attemp.

Here is my code

Dim objIntConn As New OleDbConnection
Dim objIntDataset As New DataSet
Dim objIntDataset As New DataSet

Private Sub Populate()
Try
objIntConn.Open()

objIntAdapter.SelectCommand = New OleDbCommand
objIntAdapter.SelectCommand.Connection = objIntConn
objIntAdapter.SelectCommand.CommandType =
CommandType.TableDirect
objIntAdapter.SelectCommand.CommandText =
cmbIntTbl.Text


Try
'objIntDataset.Reset()
'objIntDataset.Tables("IntTable").Clear()
'objIntDataset.Tables.add("IntTable")
'dgIntData.DataBindings.Clear()
Catch
End Try

objIntAdapter.Fill(objIntDataset, "IntTable")

objIntConn.Close()

dgIntData.DataSource = objIntDataset
dgIntData.DataMember = "IntTable"
dgIntData.AutoGenerateColumns = True
dgIntData.AutoResizeColumns()
Catch
MessageBox.Show("An error occured while processing
data", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Finally
objIntConn.Close()
End Try

You can see that I've tried a couple of different solutions and none of
them have worked.


Please Help!!
 
C

Cor Ligthert [MVP]

BStrick,

Did you try this already


objIntDataset.Tables("IntTable").Clear()
objIntAdapter.Fill(objIntDataset, "IntTable")

I am not sure if this is working, I use this forever with

dgIntData.DataSource = objIntDataset.Tables("IntTable")

By the way, why are you setting so consequent obj before everything, I see
this seldom done and nowhere in the documentation from Microsoft. In Net
everything is an object even your dg.

Another point is the Open and Close of the connection. The dataadapter does
that for you. (If you be consequent and not do an open before it), so just
removing it makes your code cleaner.

I hope this helps,

Cor
 
B

BStrick

I will try the objIntDataSet.Tables("IntTable").Clear method again when
I get home, but if I recall, it was causing my datagrid to return blank
when I tried to use it. I will also remove the .open and .close
methods. Its just a force of habit from VB.6.0 I guess.


Thank You
I'll let you know if this works
 
B

BStrick

I don't know whats going on... it did'nt wrk. Does anyone else have any
ideas? I did a test to see where the error is coming from and believe
it is either with the dataAdapter or the dataset. I've tried to use the
Clear and Reset methods for both of them and didn't get any desired
results. I'm lost.
 
O

Otis Mukinfus

I've looked around and haven't found a usuable solution to this issue.
I hope that you can provide me with some assistance. I have a
application that calls for the user to select a database/table to view
data from. At that point a datagrid populates with the information.
This works fine except when the user tries to either repopulate that
grid with the same table or with data from a different table.

If the user tries to repopluate from the same table then if should
simply overwrite what was there at first. If they try to populate from
a diffferent table then it should erase the old data and input the new.

Instead what I get when i try to repopulate with the same table is that
it inputs additional data starting at the last row from the frst
attempt. If I am populating the grid from a new table then it will keep
all the previous columns and add the new data to the last column on the
left from the first attemp.

Here is my code

Dim objIntConn As New OleDbConnection
Dim objIntDataset As New DataSet
Dim objIntDataset As New DataSet

Private Sub Populate()
Try
objIntConn.Open()

objIntAdapter.SelectCommand = New OleDbCommand
objIntAdapter.SelectCommand.Connection = objIntConn
objIntAdapter.SelectCommand.CommandType =
CommandType.TableDirect
objIntAdapter.SelectCommand.CommandText =
cmbIntTbl.Text


Try
'objIntDataset.Reset()
'objIntDataset.Tables("IntTable").Clear()
'objIntDataset.Tables.add("IntTable")
'dgIntData.DataBindings.Clear()
Catch
End Try

objIntAdapter.Fill(objIntDataset, "IntTable")

objIntConn.Close()

dgIntData.DataSource = objIntDataset
dgIntData.DataMember = "IntTable"
dgIntData.AutoGenerateColumns = True
dgIntData.AutoResizeColumns()
Catch
MessageBox.Show("An error occured while processing
data", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Finally
objIntConn.Close()
End Try

You can see that I've tried a couple of different solutions and none of
them have worked.


Please Help!!

To clear a grid and add new data without appending it in C# you would
simply code: YourGrid.DataSource = null;

Then refill it from the DataDataSet.

Otis Mukinfus
http://www.otismukinfus.com
http://www.tomchilders.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top