הערה
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות להיכנס או לשנות מדריכי כתובות.
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות לשנות מדריכי כתובות.
Question
Thursday, May 27, 2010 4:40 AM
Anyone knows how to replace a string in a datagridview with another string?
e.g I have a column which has texts "Text1","Text2" that repeate many times in the column. How can I search-replace "Text1" with "anothertext1" and "Text2" with "anothertext2"?
I searched in datagridview members but I didn't find how to do it. Thanks.
All replies (7)
Thursday, May 27, 2010 8:34 AM ✅Answered | 1 vote
I found out the solution :
For RowsCount = 0 To DataGridView1.Rows.Count - 2
If DataGridView1.Columns("Status").DataGridView.Rows.Item(RowsCount).Cells("Status").Value.ToString = "A" Then
DataGridView1.Columns("Status").DataGridView.Rows.Item(RowsCount).Cells("Status").Value = "ΑΝΑΓΝΩΡΙΣΗ"
ElseIf DataGridView1.Columns("Status").DataGridView.Rows.Item(RowsCount).Cells("Status").Value.ToString = "R" Then
DataGridView1.Columns("Status").DataGridView.Rows.Item(RowsCount).Cells("Status").Value = "ΑΠΟΣΒΕΣΗ"
ElseIf DataGridView1.Columns("Status").DataGridView.Rows.Item(RowsCount).Cells("Status").Value.ToString = "F" Then
DataGridView1.Columns("Status").DataGridView.Rows.Item(RowsCount).Cells("Status").Value = "ΕΝΕΡΓΟΠΟΙΗΣΗ"
End If
Next
The problem was that I had the last row null. So with the above code I don´t read the last row. Thank you all.
Thursday, May 27, 2010 5:17 AM
you can iterate through all the DGV Rows and check that value in that Column. Just replace the Column index with whatever yours is, or you can use the Column name - Cells("columnName")
For Each dgvr As DataGridViewRow In DataGridView1.Rows
If dgvr.Cells(0).Value.ToString = "Text1" Then
dgvr.Cells(0).Value = "another Text1"
ElseIf dgvr.Cells(0).Value.ToString = "Text2" Then
dgvr.Cells(0).Value = "another Text2"
End If
Next
Thursday, May 27, 2010 5:35 AM
As alternative to the answer from jwavila
For Each dgvr As DataGridViewRow In DataGridView1.Rows
dgrv.Cells(0).Value = dgvr.Cells(0).Value.ToString.Replace("Text","anothertext")
Next
Success
Cor
Thursday, May 27, 2010 6:30 AM
Thank you both for your answers! It was so simple! I guess my mind stacked.
Thursday, May 27, 2010 7:09 AM
I think I have a new problem now,
When the compiler runs
For Each dgvr As DataGridViewRow In DataGridView1.Rows
I get an error "NullReferenceException was caught". Can you help.
Below is my code
Private Sub Form3_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Try
'Open connection with database.
Connection = New Odbc.OdbcConnection(ConnectionString)
Connection.Open()
'Start the Timer that controls the refresh rate of data displayed at Form4.
Timer2.Interval = 2000
Timer2.Enabled = True
Timer2.Start()
'Disable Button6
Button6.Enabled = False
Catch ex As Exception 'Error handling code.
'Send the Form3 "To back" in order to see the Msgbox.
Me.TopMost = False
'Message that is displayed if the connection with database isn't possible.
MsgBox("Η ΣΥΝΔΕΣΗ ΜΕ ΤΗ ΒΑΣΗ ΔΕΔΟΜΕΝΩΝ ΔΕΝ ΕΙΝΑΙ ΔΥΝΑΤΗ.ΤΟ ALARMS MANAGER ΘΑ ΚΛΕΙΣΕΙ. F11")
'Terminate AlarmsManager due to fatal error.
Application.Exit()
End Try
End Sub
Private Sub Timer2_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer2.Tick
Try
'Stop Timer that controls the refresh rate of current time-date being displayed at Form4.
Timer2.Enabled = False
Timer2.Stop()
Dim Command As New Odbc.OdbcCommand("select TheTime,Description,Status from ALARMLOG", Connection)
Me.DataAdapter = New Odbc.OdbcDataAdapter(Command)
Dim Table As New DataTable()
Me.DataAdapter.Fill(Table)
Me.BindingSource1.DataSource = Table
Me.DataGridView1.DataSource = Me.BindingSource1
For Each dgvr As DataGridViewRow In DataGridView1.Rows
If dgvr.Cells("Status").Value.ToString = "A" Then
dgvr.Cells("Status").Value = "ΑΝΑΓΝΩΡΙΣΗ"
ElseIf dgvr.Cells("Status").Value.ToString = "R" Then
dgvr.Cells("Status").Value = "ΑΠΟΣΒΕΣΗ"
ElseIf dgvr.Cells("Status").Value.ToString = "F" Then
dgvr.Cells("Status").Value = "ΕΝΕΡΓΟΠΟΙΗΣΗ"
End If
Next
end sub
Thank you
Thursday, May 27, 2010 9:30 AM
But this is not the solution to your own question.
:-)
Success
Cor
Friday, May 28, 2010 12:26 PM
I think this solution is OK. Because as I said the last row ( I don´t need the last row) is null so there is no need to read it and have an error.