By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,483 Members | 3,312 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,483 IT Pros & Developers. It's quick & easy.

Update Datagridview with new data VB.net

P: 31
Hey all, trying to update a cell in a dgv when I have a new value for it. I'm not sure how to go about it. I've looked online but, like the books I have, there isn't much information on dgvs or controls.

I've found a suggestion to use resetItem but I must be using it wrong. Any suggestions would be great either on the code in particular or resources online.

My main goal is to figure out how to update individual cells with new data that comes into the datatable. My code is below:


Expand|Select|Wrap|Line Numbers
  1.   Imports System.Data
  2. Imports System.Data.Sql
  3. Imports System.Data.SqlClient
  4.  
  5. Public Class Form1
  6.     Dim testNumber As Integer = 1
  7.  
  8.     Dim myBindingSource As BindingSource
  9.     Dim ds As New DataSet("myDataset")
  10.     Dim dt As New DataTable("MyTable")
  11.  
  12.     Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  13.  
  14.         'fillDataGridviewInitial()
  15.  
  16.         dt.Columns.Add("TestColumn", GetType(Integer))
  17.         dt.Rows.Add()
  18.         ds.Tables.Add(dt)
  19.         myBindingSource = New BindingSource(ds, "MyTable")
  20.         DataGridView1.DataSource = (myBindingSource)
  21.         dt.Rows(0).Item(0) = testNumber
  22.         'myBindingSource.ResetItem(dt.Rows(0).Item(0))
  23.         Timer1.Start()
  24.     End Sub
  25.  
  26.     Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
  27.         incrementNumber()
  28.         dt.Rows(0).Item(0) = testNumber
  29.         myBindingSource.ResetItem(dt.Rows(0).Item(0))
  30.         If testNumber = 10 Then
  31.             MsgBox("pause")
  32.         End If
  33.     End Sub
  34.  
  35.     Private Sub incrementNumber()
  36.         testNumber = testNumber + 1
  37.     End Sub
  38.  
  39. End Class
  40.  
<edit by mod insertAlias: removed unnecessary line breaks>
Nov 13 '08 #1
Share this Question
Share on Google+
7 Replies


P: 31
I got it to work with this:

Expand|Select|Wrap|Line Numbers
  1.     Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
  2.         incrementNumber()
  3.         dt.Rows(0).Item(0) = testNumber
  4.  
  5.         'myBindingSource.ResetItem(dt.Rows(0).Item(0))
  6.         DataGridView1.Refresh()
  7.  
  8.         If testNumber = 10 Then
  9.             MsgBox("pause")
  10.  
  11.  
  12.         End If
  13.     End Sub
  14.  
Is the refresh 'slow' or use a lot of resources? I'm not sure I want to refresh everything everytime I need to change one cell.

TIA
Nov 13 '08 #2

vanc
Expert 100+
P: 211
Why don't you use cell value changed event? Then you just have to refresh that cell only.
Nov 13 '08 #3

P: 31
I would be very interested in something like that. Are there any books or websites you could recommend?
Nov 14 '08 #4

P: 31
I've found update method that works great. I tested this out with 20 columns and a ton of rows. The refresh was very slow in comparison. Refresh did not keep up with counter where as the update did.

Expand|Select|Wrap|Line Numbers
  1.  Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
  2.         incrementNumber()
  3.         dt.Rows(0).Item(0) = testNumber
  4.         dt.Rows(0).Item(1) = testNumber * 2
  5.  
  6.         'method 1 to update cell values in a datatable
  7.         'use datagridview refresh method
  8.         'DataGridView1.Refresh()
  9.  
  10.         'method 2 to update cell values in a datatable
  11.         'use resetItem method
  12.         'this takes an argument of Integer for one column only
  13.         'myBindingSource.ResetItem(0)
  14.  
  15.  
  16.         'method3 to update cell values in a datatable
  17.         'use update method
  18.         DataGridView1.Update()
  19.  
  20.  
  21.         If testNumber = 10 Then
  22.             'MsgBox("pause")
  23.             Me.DataGridView1.Rows(0).Cells(0).Style.BackColor = Color.Red
  24.         End If
  25.     End Sub
Nov 14 '08 #5

P: 31
I'm now confronted with a new problem. I can't seem to be able to get the value of a cell and assign that value to a datatable. In the following code, i correctly get the column and row indexes but 'tempvalue' comes up blank. Any suggestions?

Expand|Select|Wrap|Line Numbers
  1.     Private Sub datagridview1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellLeave
  2.         MsgBox(e.ColumnIndex)
  3.         MsgBox(e.RowIndex)
  4.  
  5.  
  6.         DataGridView1.BeginEdit(True)
  7.  
  8.         Dim tempValue As New Object
  9.         tempValue = DataGridView1(e.ColumnIndex, e.RowIndex).Value.ToString
  10.  
  11.  
  12.         MsgBox(tempValue)
  13.         Try
  14.  
  15.         Catch ex As Exception
  16.  
  17.         End Try
  18.  
  19.         If e.ColumnIndex <> 0 Then
  20.             Try
  21.                 ds.Tables("dt").Rows(e.RowIndex).Item(e.ColumnIndex) = tempValue
  22.             Catch ex As Exception
  23.                 MsgBox("error")
  24.             End Try
  25.  
  26.  
  27.         End If
  28.  
  29.         DataGridView1.EndEdit(True)
  30.  
  31.  
  32.     End Sub
TIA
Nov 17 '08 #6

vanc
Expert 100+
P: 211
What is the output of tempvalue? I don't do VB but your code to get value from the cell in gridview seems right. If you don't get value from that cell then there are two chances:
1. You refer to a wrong cell location!!!
2. You haven't set value to that cell.

You can color the selected cell to make sure you're pointing at the right cell and observe the output. Throw me some output.
Nov 18 '08 #7

P: 31
Ok, got a solution. I originally got the cell leave business online. Using cellvaluechanged is a lot easier (at least to me).


Expand|Select|Wrap|Line Numbers
  1. Private Sub datagridview1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellValueChanged          
  2.  
  3. DataGridView1(e.ColumnIndex, e.RowIndex).Style.BackColor = Color.Yellow          
  4.  
  5. Dim tempValue As String          
  6.  
  7. tempValue = DataGridView1.CurrentCell.Value.ToString          
  8.  
  9. updateDatatabledt(DataGridView1.CurrentCellAddress.Y, DataGridView1.CurrentCellAddress.X, tempValue)       
  10.  
  11. End Sub
Nov 20 '08 #8

Post your reply

Sign in to post your reply or Sign up for a free account.