0

N,V,C,D are the variables in a bound column of my GridView.

When I display this table in my GridView, I want New,Verified,Cancelled,Deleted to be displayed instead.

My GridView calls a procedure in my database that runs the select Query. Do I need to change the query or add a GridView function? I do not want to change my database values per se.

How do I go about this?

This is my bound field as of now:

<asp:BoundField 
    DataField="Status"
    HeaderText="Status"
    SortExpression="Status" />
2
  • SQL - Alias?? Commented Apr 4, 2014 at 8:50
  • @huMptyduMpty I do not want to change my column headers. I want to replace the data it retrieves. Commented Apr 4, 2014 at 8:55

4 Answers 4

1

That should be possible in C# - at least if you only wan to display the values and not edit them. In the CellFormatting event you can simply change the value to be displayed.

private void gridview_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
  if (e.Value.equals("N")) e.Value = "New";
  else if(e.Value.equals("V")) e.Value = "Verified";
  else if(e.Value.equals("C")) e.Value = "Cancelled";
  else if(e.Value.equals("D")) e.Value = "Deleted";
}

I haven't got C# at hand right now, so there may be typos. Just try it.

Sign up to request clarification or add additional context in comments.

5 Comments

Where do I call this event?
You don't call events. Events happen. You write a method to react on an event associated with a control. Is there no CellFormatting event for your gridview? What gridview do you use? A DataGridView?
I just use a gridview.<asp:GridView ID="Gv_IndentsForIp" runat="server"
Ah, okay. It seems, for ASP GridView you'd use the RowDataBound event. I have no experience with that. I hope Sajeetharan's answer will help you.
I use a bound field and those don't seem to have ID tags. That's why I'm having some problem with this.
1

In your query,

Select 
      YourFields
      case 
      when YourConditionField= 'N' then 'New' 
      when YourConditionField= 'V' then 'Verified'
      when YourConditionField= 'C' then 'Cancelled'
      when YourConditionField= 'D' then 'Deleted'
      end,
from table

First in GridView,

You can use the RowDataBound event, You need to add a template column with a label to your grid view

<asp:TemplateField>
    <ItemTemplate>
        <asp:Label ID="labelResult" runat="server" />
    </ItemTemplate>
</asp:TemplateField>


protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if(e.Row.RowType == DataControlRowType.DataRow)
    {
       string value = e.Row.Cells[0].Text;
       Next find the label in the template field.
        Label myLabel = (Label) e.Row.FindControl("myLabel");
        if (value == "N")
        {
            myLabel.Text = "New";
        }
        else if (value == "V")
        {
            myLabel.Text = "Verified";
        }
        else if (value == "C")
        {
            myLabel.Text = "Cancelled";
        }
        else if (value == "D")
        {
            myLabel.Text = "Deleted";
        }
    }
}

1 Comment

I use bound fields, not template fields. How do I proceed?
0
   <asp:BoundField HeaderText="NEW" DataField="N" ></asp:BoundField>

1 Comment

yes, You can add bound fields as many as you need and keep header text as a column name as you like. and DataField as your column name coming output from DB.
0

First, I edited the status column of the GridViewControl from the default BoundField and changed it to ItemTemplate

(I removed the autogenerated EditItemTemplate tag)

 <asp:Label ID="lblStatus"  
                 runat="server" 
                 Text='<%# GetLabelText(Eval("status")) %>'>
 </asp:Label>

Then in my CS file, I added the following code:

public string GetLabelText(object dataItem)
    {
    string text = "";
    string val = dataItem as string;
    switch (val)
        {
        case "N":   text = "New"; 
             break;
        case "V":   text = "Verified";
            break;
        case "F":   text = "Fulfilled";
            break;
        case "C":   text = "Cancelled";
            break;
        }
    return text;
    }

This worked like a charm. Thanks for the help, guys!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.