4 Replies - 17175 Views - Last Post: 31 January 2011 - 11:07 AM Rate Topic: -----

#1 Kashif Gulzar  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 31-January 11

DataGridView Cell Value Change Not Happening

Posted 31 January 2011 - 02:23 AM

I have retrieved data from access database into a DataGridView .Let us suppose i am in Row 0.When i change the Contents of Row 0 Cell 1 and press a button the Update query should modify that row but i am unable to modify the value of the cell.The cell maintains it's previous value and the database is not modified .For example if i change the contents of a cell from "David" to "Jhon" how can i change the value of the cell from "David" to "Jhon".Please help me my email is kashif1112000@hotmail.com.I am using VB .Net 2008.
How can i get the text changed in a particular cell of a DataGridView

Is This A Good Question/Topic? 0
  • +

Replies To: DataGridView Cell Value Change Not Happening

#2 raziel_  Icon User is offline

  • Like a lollipop
  • member icon

Reputation: 464
  • View blog
  • Posts: 4,255
  • Joined: 25-March 09

Re: DataGridView Cell Value Change Not Happening

Posted 31 January 2011 - 03:28 AM

ok can we see your code? is your data grid columns enabled for edit? how do you load the data in your DB datareader or with dataset?

This post has been edited by NoBrain: 31 January 2011 - 03:29 AM

Was This Post Helpful? 0
  • +
  • -

#3 Kashif Gulzar  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 31-January 11

Re: DataGridView Cell Value Change Not Happening

Posted 31 January 2011 - 10:13 AM

For populating the DataGridView I am using the following code

Private Sub RefreshRecords()
Dim dtx As New DataTable
Dim dsx As New DataSet
Dim dax As New OleDb.OleDbDataAdapter
If check = 1 Then
check = 0
C1.SelectedIndex = 0
End If
Try
Sql = "Select CheckUpDate,BloodPressure,Pulse,Temperature,Medicine from NursingDetail Where SerialNo=" & C1.Text
dax = New OleDb.OleDbDataAdapter(Sql, conn)
dax.Fill(dsx, "1")
dtx = dsx.Tables("1")

DataGridView1.DataSource = dsx.Tables("1")
If (dtx.Rows.Count > 0) Then
max = dtx.Rows.Count
current = 0
Else
MessageBox.Show("No Record in the Table")
End If

Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
I am changing the text in a cell and using the following code to modify the database but to no avail
Private Sub Save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Save.Click
Dim blood As String
Dim pulse As String
Dim temp As String
Dim med As String
Dim serial As String
Dim dat As String
Dim exp As String
serial = C1.Text
DataGridView1.RefreshEdit()
exp = DataGridView1.Item(1, DataGridView1.CurrentRow.Index).ToString()
MessageBox.Show(exp)
DataGridView1.UpdateCellValue(1, 0)
Try
dat = Convert.ToDateTime(DataGridView1.CurrentRow.Cells(0).Value).Date
blood = Convert.ToString(DataGridView1.Item(1, 0).FormattedValue)
pulse = Convert.ToInt32(DataGridView1.CurrentRow.Cells(2).Value)
temp = Convert.ToInt32(DataGridView1.CurrentRow.Cells(3).Value)
med = Convert.ToString(DataGridView1.CurrentRow.Cells(4).Value)
Sql = "Update NursingDetail Set BloodPressure='" & blood & "'," & "Pulse=" & pulse & "," & "Temperature=" & temp & "," & "Medicine='" & med & "' Where SerialNo=" & serial & " And CheckUpdate Like '" & dat & "'"
cmd.CommandText = Sql
cmd.Connection = conn
cmd.ExecuteNonQuery()
RefreshRecords()
ButtonStatus(True)
DataGridView1.CurrentRow.Cells(0).ReadOnly = False
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
End Sub
How can i change the value of a cell by just changing the text in that cell

Mr Expert Please tell me how can i change the value of a cell by just erasing the cell contents and typing in a new value.Can i use the CellValueChange event for that.Please provide a simple code snippet.I shall be thank full to you.
Was This Post Helpful? 0
  • +
  • -

#4 Kashif Gulzar  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 31-January 11

Re: DataGridView Cell Value Change Not Happening

Posted 31 January 2011 - 10:29 AM

I just want to change the value of a cell when it's contents are erased and a new value is provided.Forget about the database.
Was This Post Helpful? 0
  • +
  • -

#5 raziel_  Icon User is offline

  • Like a lollipop
  • member icon

Reputation: 464
  • View blog
  • Posts: 4,255
  • Joined: 25-March 09

Re: DataGridView Cell Value Change Not Happening

Posted 31 January 2011 - 11:07 AM

how do you get the new value other wise you can change the value simply by using this:
Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Create columns in table and add values'
        DataGridView1.Columns.Add("C1", "Col 1")
        DataGridView1.Columns.Add("C2", "Col 2")
        DataGridView1.Columns.Add("C3", "Col 3")
        Debug.Print(DataGridView1.Columns.Count)
        'Add values'

        For i As Integer = 0 To 10
            DataGridView1.Rows.Add(1)
            DataGridView1.Rows(DataGridView1.Rows.Count - 1).Cells(0).Value = i
            DataGridView1.Rows(DataGridView1.Rows.Count - 1).Cells(1).Value = i + 1
            DataGridView1.Rows(DataGridView1.Rows.Count - 1).Cells(2).Value = "Val" & i.ToString()
        Next
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'Change the value of column 2 and 3 on row 1 and 5'
        'change the vlaue in row 1 in clumn 2 and 3'
        DataGridView1.Rows(0).Cells(1).Value = "new value in col 2"
        DataGridView1.Rows(0).Cells(2).Value = "new value in col 3"
        'change tha vlue in row 5 in column 2 and 3'
        DataGridView1.Rows(4).Cells(1).Value = "new value in col 2 row 5"
        DataGridView1.Rows(4).Cells(2).Value = "new value in col 3 row 5"
    End Sub
End Class



as you see only thing you need to do is set the new values by using the property "Value". I hope this is what your looking for.

good luck :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1