Modifying data and displaying it in a datagrid

Hi,

I am having a problem updating and displaying data, and i've been stuck on this for a long, long time, so thank you in advance to anyone who can help me out with this.

On a form, i have a data grid which, when the user double clicks on a record, a data table updates with the value of the selection in the datagrid. For modifying this table, i am using the following commands:

dt = Me.FantasyDataSet.Draft

dt.Rows(position - 1).Item(Team) = player & ", " & pts

dt.AcceptChanges()

As i step through my program, i can see that my datatable, dt, is being updated with new values.

i have another datagrid which i want to display the datatable i just updated, but it never updates. Here is my routine for displaying the data in the other datagrid, which i call just after dt.AcceptChanges():

Public Sub updateTeamGrids()

Dim oCon As OleDb.OleDbConnection

Dim oCommand As OleDb.OleDbCommand

Dim strSQL As String

Dim dt As DataTable

Dim oAdpt As OleDbDataAdapter

oCon = New OleDbConnection(My.Settings.FantasyConnectionString)

oCon.Open()

strSQL = "SELECT Draft.[Team " & Team & "] FROM Draft;"

oCommand = New OleDbCommand(strSQL, oCon)

oAdpt = New OleDbDataAdapter(oCommand)

dt = New DataTable()

oAdpt.SelectCommand = oCommand

oAdpt.Fill(dt)

oCon.Close()

Me.DraftDataGridView.DataSource = dt

But this does not update my datagrid, and when i step through this routine, the changes that i made to my table are no longer apparent. So my problems here are: I want to save my changes to my data table, and display my changes in the datagrid. What am i doing wrong

I have been looking through similar posts and they have not been helped, so please don't direct to another post, or any of the MS documentation....

Thanks in advance!

Pete M



Answer this question

Modifying data and displaying it in a datagrid

  • jch02140

    Hi,< xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

     

    Your problem is that the datatable (tb) that you update in the procedure updateTeamGrids you are creating a new instance of it and when you assign it to the datagridview has the new query result instead of the ones you just update.

     

    dt = Me.FantasyDataSet.Draft

    dt.Rows(position - 1).Item(Team) = player & ", " & pts

    dt.AcceptChanges()

    Me.DraftDataGridView.DataSource = dt

     

    That will display the updated data in the datagridview but I don’t know if there are other columns in the datatable that you want to omit. If you want to omit other columns in the datatable you can go to the datagridview columns properties and assign there the columns that you want to use but you have to set the AutoGenerateColumns properties to false by code of the datagridview or you can do it in code:

     

    dt = Me.FantasyDataSet.Draft

    dt.Rows(position - 1).Item(Team) = player & ", " & pts

    dt.AcceptChanges()

     

    Dim MyTeamColumn As New DataGridViewTextBoxColumn

    MyTeamColumn.HeaderText = Team

    MyTeamColumn.Width = 100

    MyTeamColumn.DataPropertyName = Team

     

    Me.DraftDataGridView.Columns.Add(MyTeamColumn)

    Me.DraftDataGridView.AutoGenerateColumns = False

    Me.DraftDataGridView.DataSource = dt


  • Glennn

    Thanks. Make sense, i'll give it a try and get back to you.

    Pete M


  • Modifying data and displaying it in a datagrid