Raghu
Raghu

Reputation: 1

data replacing in gridview

I am working in ASP.Net, I am saving the status field in Database as true or false. Now, I want to display the true or false as Active or Inactive in the front end in GridView. How to display the data in Gridview.

Thanks in advance.

Upvotes: 0

Views: 706

Answers (5)

Yogesh
Yogesh

Reputation: 685

If you are using template field then you can create server side function and call on Eval like below

<asp:TemplateField>
    <ItemTemplate>
        <asp:Label ID="lblstatus" runat="server" Text='<%# GetStatusText(Eval("status")) %>'></asp:Label>
    </ItemTemplate>
</asp:TemplateField>




public string GetStatusText(string strIsActive)
        {
          bool val = Boolean.Parse(strIsActive);
          if(val)
          { 
             return "Active";
          }
          else
          { 
             return "Inactive";
          }
        }

Upvotes: 0

Since_2008
Since_2008

Reputation: 2341

If you know the cell location of which you want to filter data, you could also do this in the gridviews RowDataBound event.

protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (e.Row.Cells[2].Text == "1")
                e.Row.Cells[2].Text = "True";
            else
                e.Row.Cells[2].Text = "False";
        }
    }

This is what I used to find and replace text in a gridview.

Upvotes: 0

JonH
JonH

Reputation: 33183

The alternative is to use your datagrid's RowDataBound event to convert what is it to the strings active / inactive:

  Protected Sub gvRequests_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvRequests.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim lbl As Label = CType(e.Row.FindControl("lblStatus"), Label)
            If lbl.Text="1" then
                   lbl.Text="Active"
            else
                   lbl.Text="Inactive"
            end if
        end if
  end sub

Upvotes: 3

Pragnesh Patel
Pragnesh Patel

Reputation: 1444

use checkbox column to show the status field. ( set that column to disable )

Upvotes: 0

SLaks
SLaks

Reputation: 888185

If you're asking how to change true and false to Active and Inactive, you could use a CASE statement in your SQL query, like this:

SELECT CASE Status WHEN 1 THEN 'Active' WHEN 0 THEN 'Inactive' END FROM Something

For a more specific answer, please post more details.

Upvotes: 1

Related Questions