• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 288
  • Last Modified:

Trying to manipulate the displayed value in a GridView column

 I have a column I am creating in a GridView. Now the values retrived from the database for this column are integers.(1,2,3,4) I am trying to run a test for the integer retrieved and then manipulate the DataFormat string of the column based on the test. So if the value retrieved is 1, then write "Customer Complaint".
  However currently my tests (The switch case) do not seem to be running. Question becomes what column property do I actually need to test on as currently testing on the .DataField property does not seem to work. Running a test with a series of IF statements currently yields the same result. Any direction would be greatly appreciated.

BoundField correspondencetype_i = new BoundField();
        correspondencetype_i.HeaderText = "Correspondence Type";
        correspondencetype_i.DataField = "correspondencetype_i"
        switch (correspondencetype_i.DataField)
            case "1":
                correspondencetype_i.DataFormatString = "Customer Complaint";
            case "2":
                correspondencetype_i.DataFormatString = "Technical Support";
            case "3":
                correspondencetype_i.DataFormatString = "Customer Suggestion";
            case "4":
                correspondencetype_i.DataFormatString = "Repair(RMA)";

Open in new window

1 Solution
Do you have to add the BoundField dynamically?
You should add BoundField in your Markup and not in code-behind.
Then in your RowDataBound Event handler do something like below:

protected void GridView4_RowDataBound(object sender, GridViewRowEventArgs e)
        if (e.Row.RowType == DataControlRowType.DataRow)
            DataRowView dv = (DataRowView)e.Row.DataItem;
            string value = "";
            //yourColumnIndex below is the column in your record returned from DB that is being bound to this field
            int dbValue = Convert.ToInt32(dv.Row.ItemArray[yourColumnIndex])
            switch (dbValue )
                case 1:
                    value = "Customer Complaint";
                case 2:
                    value = "Technical Support";
              //and so on
            //yourColumnIndexToReplace is the column in the GV for which you want to replace the value.
            e.Row.Cells[yourColumnIndexToReplace].Text = value;
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now