0

I have a database in which there is a column named "codes". When this column of codes is displayed in gridview I would like to modify the contents of the column. For example:

In Database if the codes column contains A,B and C values, then in gridview it should show Abeloth(instead of A), Bollux (instead of B), Chewbacca (instead of C).

Thank you

Update:

I have added the following code, what point am I missing?

Protected Sub GrvPassengerReport_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles GrvPassengerReport.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then

             If e.Row.Cells(8).Text = "C" Then
            e.Row.Cells(8).Text = "Chewbacca"
        End If
        End If
    End Sub
saadasharif
  • 80
  • 1
  • 14

3 Answers3

0

If you enter the "Edit Columns" option of the gridview (and assuming your sql is connected correctly), you should have two distinct properties that you will need to do this.

First of all the main one will be under the Data tab and its called DataPropertyName. This is the name of the data that you want this table display (eg Age, Height or Hair Colour)

You also have a field under the Design Tab called (Name) this is just the display name inside the grid.

So you could have a Database property name called ThePersonsName and you would set your gridviews DataPropertyName to that, then in the (Name) field you could just write name and it would display it as so.

DotNet NF
  • 805
  • 1
  • 6
  • 14
0

one way to achieve this is using "itemdatabound" event for the gridview control and then decide what values you'd rather display in the gridview with respect to the actual values in the database table.

Bhargav
  • 1
  • 2
  • The column contains multiple options, thus I am thinking if any "if statement" can be added. for example: If Option A then Show Abeloth End If... – saadasharif Sep 17 '14 at 09:58
  • yes you could do that or maybe use a switch case if there are fewer options to chose from but if there are a lot of distinct entries in the table's column. then i'd recommend you use another table to maintain the corresponding values for these codes via primary and foreign key constraints. – Bhargav Sep 17 '14 at 10:39
  • where can I add the "if statement" in this whole process? – saadasharif Sep 17 '14 at 10:40
  • 1
    it would be something like this protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.Cells[x].Text == "A" ) { e.Row.Cells[x].Text = "Abeloth"; } } // here x is the cell number where your column's text would be binded. Remember that the cell numbers start from 0 – Bhargav Sep 17 '14 at 11:00
  • I'm new to this text editor.. sorry for the garbled reply.:) hope you found what I wanted to share. – Bhargav Sep 17 '14 at 11:08
0

I was able to solve this issue by using GridView.RowDataBound event. Few articles that helped me are

1) MSDN

2) Question on StackOverflow

Here is the code

Protected Sub GrvPassengerReport_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles GrvPassengerReport.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then

            Dim labelone As System.Web.UI.WebControls.Label
            labelone = e.Row.FindControl("lblcStopType")

            If labelone.Text = "C" Then
                labelone.Text = "Chewbaca"
            End If

        End If
    End Sub
Community
  • 1
  • 1
saadasharif
  • 80
  • 1
  • 14