Reputation: 1113
I have problem on changing cell value binded in gridview based on condition. The data was binded first to gridview from a csv file. What I want to do is whenever a specific cell on a specific column meets the condition I put, it will change the value of that specific cell.
For example, if cell value is equal to "PHP" then it will changed into "Philippines". I tried below codes on Gridview RowDataBound event but nothing happens:
Private Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
For i As Integer = 0 To GridView1.Rows.Count - 1
Dim myVal As String = GridView1.Rows(i).Cells(0).Text
If myVal = "PHP" Then
GridView1.Rows(i).Cells(0).Text = "Philippines"
End If
Next
End Sub
Please help me.
Thanks in advance.
Upvotes: 1
Views: 12372
Reputation: 14614
If you use RowDataBound event for GridView1, then GridView1_RowDataBound
will be executed for each row, not just once. Here's what you should do instead:
Private Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
Dim myVal As String = e.Row.Cells(0).Text
If myVal = "PHP" Then
e.Row.Cells(0).Text = "Philippines"
End If
End If
End Sub
Upvotes: 2
Reputation: 3549
If it's bound to a CSV you have two options:
If you want to change both the CSV and the table choose option 1. If you don't care about the CSV and or want to create a separate output then choose option 2.
*edited to add some code
You just read it into the table like normal. Since it's a CSV I would personally just change it to a .txt file to make it easier. Then:
dim reader as string() = input.txt
for x as integer = 0 to reader.length -1
dim split as string() = reader(x).split(","c)
for y as integer from 0 to split.length - 1
datagridview.cells(y+1,x+1) = split(y)
next
next
This is untested, but it should go together something along these lines (I don't know the format of the CSV). If you have uneven rows it would probably be better if you did something like this
for each str as String in Split
add to datagrid...
next
Upvotes: 0