Replace Text in ASP.net GridView
There are times when you need to replace text displayed in ASP.net GridView with something different than what is in your database.
Use the RowDataBound property of the GridView. In the properties window click on the Events button and than select RowDataBound.
Here is a simple if statement that checks the 2nd column for the text "ASP" and replaces it with the text "Net".
If e.Row.RowType = DataControlRowType.DataRow Then
If e.Row.Cells(1).Text = "ASP" Then
e.Row.Cells(1).Text = "NET"
End If
End If
You can easily replace the text of one column in the GridView based on another column. In this example we are replacing the text of the 3rd column based on the 1st column.
If e.Row.RowType = DataControlRowType.DataRow Then
If e.Row.Cells(0).Text = "ASP" Then
e.Row.Cells(2).Text = "NET"
End If
End If
Sometimes you need to append text to a GridView column. In this example if the the column contains "NET" we will add "ASP" to it
If e.Row.RowType = DataControlRowType.DataRow Then
If e.Row.Cells(0).Text = "NET" Then
e.Row.Cells(0).Text = "ASP" & e.Row.Cells(0).Text
End If
End If
0 Comments:
Post a Comment
<< Home