You must Sign In to post a response.
  • Category: ASP.NET

    Sql database has true/false but in gridview it should be Active/Inactive

    I had database with a column true/false but in grid view I need to display Active in green color and Inactive in gray color. I tried to directly bind the database which is displaying true/false in drop down list in Grid view.

    Aspx code:

    <asp:TemplateField HeaderText="Activity">
    <ItemTemplate>
    <asp:Label ID="Label1" runat="server" Text='<%# Eval("Inactive") %>' Visible="false" />
    <asp:DropDownList ID="DropDownList1" runat="server">
    </asp:DropDownList>
    </ItemTemplate>
    </asp:TemplateField>

    Code behind : Binding database code

    protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
    {
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
    //Find the DropDownList in the Row
    DropDownList DropDownList1 = (e.Row.FindControl("DropDownList1") as DropDownList);
    DropDownList1.DataSource = GetData("Select DISTINCT Inactive from dbo.tblTBT_Depts");
    DropDownList1.DataTextField = "Inactive";
    DropDownList1.DataValueField = "Inactive";
    DropDownList1.DataBind();

    //Select the Inactive of Dept in DropDownList
    string inactive = (e.Row.FindControl("Label1") as Label).Text;
    DropDownList1.Items.FindByValue(inactive).Selected = true;
    }
    }

    Can I now how to change the code?
  • #765960
    Use 'OnRowDataBound' event, which occur on each data bind to row, here you can check the value from database and accordingly color a row or a cell
    see below snippet

    protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
    {
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
    //Find the DropDownList in the Row
    DropDownList DropDownList1 = (e.Row.FindControl("DropDownList1") as DropDownList);
    DropDownList1.DataSource = GetData("Select DISTINCT Inactive from dbo.tblTBT_Depts");
    DropDownList1.DataTextField = "Inactive";
    DropDownList1.DataValueField = "Inactive";
    DropDownList1.DataBind();

    //Here you just fill the dropdown list but you need to select value from the database and accordingly you need to color a row
    //check here if it is inactive then change cell color
    if (Inactive)
    e.Row.Cells[3].BackColor = Color.Gray;
    else
    e..Row.Cells[3].BackColor = Color.Green;
    }
    }

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #765965
    Hi
    try this code

    Client Side


    <asp:GridView ID="Dtg1" runat="server" AutoGenerateColumns="false" OnRowDataBound="Dtg1_RowDataBound">
    <Columns>
    <asp:TemplateField HeaderText="Name">
    <ItemTemplate>
    <asp:Label ID="lblName" runat="server" Text='<%# Eval("EName") %>'></asp:Label>
    </ItemTemplate>
    </asp:TemplateField>

    <asp:TemplateField HeaderText="Status">
    <ItemTemplate>
    <asp:Label ID="lblStatus" runat="server" Text='<%# Eval("Status") %>'></asp:Label>
    </ItemTemplate>
    </asp:TemplateField>

    </Columns>
    </asp:GridView>


    Server Side


    protected void Dtg1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
    Label lblname = (e.Row.FindControl("lblName") as Label);
    if (lblname.Text == "Inactive")
    e.Row.Cells[4].BackColor = Color.Gray;
    else
    e.Row.Cells[4].BackColor = Color.Green;

    }

    Name : Dotnet Developer-2015
    Email Id : kumaraspcode2009@gmail.com

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.

  • #765988
    Hi,

    If you want to bind Active inplace of True and InActive inplace of False with appropriate colors then onRowDataBound event of gridview you can customize the code as you want.

    Ex:

    Protected void Gv_OnRowDataBound(object sender, GridViewRowEventArgs e)
    {
    if(e.Row.RowType==DataControlRowType.DataRow)
    {
    string val=((Label)e.Row.FindControl("lblStatus")).Text.ToUpper().ToString();
    if(val=="TRUE")
    e.Row.Cells[0].BackColor=System.Drawing.Color.Green;
    else if(val=="FALSE")
    e.Row.Cells[0].BackColor=System.Drawing.Color.Gray;
    }
    }

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #765990
    You can handle in two ways. One you can using "Gridview1_OnRowDataBound".

    Protected void Gridview1_OnRowDataBound(object sender, GridViewRowEventArgs e)
    {
    // Handle the logic to get the row and change the color base on the status
    }

    And another way is handling in the javascript.
    1. Write one java script get the color by basing the status.
    2. Call the java script inside your gridview row. ( you can have the row with in the div/tr. in the div/tr you can call/change the style using the javascript )

    By Nathan
    Direction is important than speed

  • #765997
    Hi,
    Try changing your script to this:

    select DISTINCT case when Inactive in (0,UPPER('FALSE')) then 'ACTIVE' else (case when Inactive in (1,UPPER('TRUE')) then 'INACTIVE' else Inactive end) end as Inactive from dbo.tblTBT_Depts

  • #766003
    Sorry, nothing is helping me . I am new to this field , trying my best to try all the suggestions with small changes, but no change.

    e.Row.Cells[0].BackColor=System.Drawing.Color.Green; -----> Using this code is making my cell color to Green , but I want my text color in that to be Green.
    And how can I show true/false in my database to Active/Inactive in my front end in a drop down list.

  • #766004
    Hi

    Try this code



    Label lblname = (e.Row.FindControl("lblName") as Label);

    if (lblname.Text == "Inactive")
    {
    e.Row.Cells[4].BackColor = Color.Gray;
    e.Row.Cells[4].ForeColor = Color.Red;
    }
    else
    {
    e.Row.Cells[4].BackColor = Color.Green;
    e.Row.Cells[4].ForeColor = Color.RoyalBlue;
    }



    Name : Dotnet Developer-2015
    Email Id : kumaraspcode2009@gmail.com

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.


  • Sign In to post your comments