Formatting GridView

Today i have just making a intersting thing. i made the funny view of gridview.
let's start for making it !

first you have to select a grid view control from your toolbox. yup one more thing just download a northwind database. it'll be a very helpfull to you for doing any practice......





 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BackColor="YellowGreen"
            BorderColor="#336699" BorderStyle="Solid" BorderWidth="1px" CellPadding="2"
            Font-Names="Verdana" Font-Size="10pt" Width="60%" DataKeyNames="ProductID"
            GridLines="Horizontal">
 
   <Columns>
      <asp:BoundField DataField="ProductID" HeaderText="ProductID" />
      <asp:BoundField DataField="ProductName" HeaderText="ProductName" />
                             
      <asp:TemplateField HeaderText="UnitPrice">
         <ItemTemplate>
            <%# FormatUnitPrice(Eval("UnitPrice"))%>
         </ItemTemplate>
         <HeaderStyle HorizontalAlign="Center" />
         <ItemStyle HorizontalAlign="Center" />
      </asp:TemplateField>
             
      <asp:TemplateField HeaderText="Delivery">
         <ItemTemplate>
            <%# FormatDelivery(Eval("UnitPrice"))%>
         </ItemTemplate>
         <HeaderStyle HorizontalAlign="Center" />
         <ItemStyle HorizontalAlign="Center" />
      </asp:TemplateField>
             
      <asp:TemplateField HeaderText="UnitsInStock">
         <ItemTemplate>
            <%# FormatUnitsInStock(Eval("UnitsInStock")) %>
         </ItemTemplate>
         <HeaderStyle HorizontalAlign="Center" />
         <ItemStyle HorizontalAlign="Center" />
      </asp:TemplateField>
           
      <asp:TemplateField HeaderText="Discontinued">
         <ItemTemplate>
            <%# FormatOnSaleLabel(Eval("Discontinued"))%>                    
         </ItemTemplate>
         <HeaderStyle HorizontalAlign="Center" />
         <ItemStyle HorizontalAlign="Center" />
      </asp:TemplateField>
           
      <asp:TemplateField>
         <ItemTemplate>
            <asp:Image runat="server" ID="imgOnSale" ImageAlign="AbsMiddle"
               ImageUrl='<%# FormatOnSale(Eval("Discontinued"))%>' />
         </ItemTemplate>
         <HeaderStyle HorizontalAlign="Center" />
         <ItemStyle HorizontalAlign="Center" />
      </asp:TemplateField>
             
   </Columns>
   <HeaderStyle BackColor="#336699" Font-Bold="True" ForeColor="White" HorizontalAlign="Left" />
</asp:GridView>







now you have to make a different mehods for making a formatting.


//its simple page load there will be run only binddata method.

 protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            BindData();
        }
    }


// In this method you just make a connection for viewing a data in gridview..
    private void BindData()
    {
        string constr = "Server=kishu-pc;Database=northwind;uid=sa;pwd=pass@word1;";
        string query = "SELECT ProductID, ProductName, UnitPrice, UnitsInStock, Discontinued FROM Products";

        SqlDataAdapter da = new SqlDataAdapter(query, constr);
        DataTable table = new DataTable();

        da.Fill(table);

        GridView1.DataSource = table;
        GridView1.DataBind();
    }


// This method usded for a colomn (UnitPrice) if that'll be a null then display a (Not Set) in bold letter along with a red color. there are very simple coding .i don't thing so i should explain every line ........okay

    public string FormatUnitPrice(object objPrice)
    {

        if (objPrice.Equals(DBNull.Value))
        {
            return "<span style='color: red; font-weight: bold;'>Not Set</span>";
        }
        else
        {
            decimal price = Convert.ToDecimal(objPrice);
            if (price <= 0)
            {
                return "<span style='color: red; font-weight: bold;'>Not Set</span>";
            }
            else
            {
                return price.ToString();
            }
        }
    }


//Actully this method is showing the Not Free in Red color....according to my table....so you guys just make it very easlly....its also very simple to understand whatever is doing in this method.
    public string FormatDelivery(object objPrice)
    {
        if (objPrice.Equals(DBNull.Value))
        {
            return "";
        }
        else
        {
            decimal price = Convert.ToDecimal(objPrice);
            if (price <= 0)
            {
                return "";
            }
            else if (price >= 1000)
            {
                return "<span style='color: green; font-weight: bold;'>FREE</span>";
            }
            else
            {
                return "<span style='color: red; font-weight: bold;'>NOT FREE</span>";
            }
        }
    }




//This method simply show if you have a null value in this coloumn then show the UnitInStock attribute in red color Out of stock..

    public string FormatUnitsInStock(object objStock)
    {
        if (objStock.Equals(DBNull.Value))
        {
            return "<span style='color: red; font-weight: bold;'>Out of Stock</span>";
        }
        else
        {
            int stock = Convert.ToInt32(objStock);
            if (stock <= 0)
            {
                return "<span style='color: red; font-weight: bold;'>Out of Stock</span>";
            }
            else
            {
                return stock.ToString();
            }
        }
    }




// In this method show you if you have a boolean value then
    public string FormatOnSaleLabel(object objOnSale)
    {
        bool onSale = Convert.ToBoolean(objOnSale);
        if (onSale)
        {
            return "Yes";
        }
        else
        {
            return "No";
        }
    }



//its very intersting method that will be show a smilly face when you have a value oterwise it'll be saddy face...
    public string FormatOnSale(object objOnSale)
    {
        string happyIcon = "~/images/happy.gif";
        string sadIcon = "~/images/sad.gif";

        bool onSale = Convert.ToBoolean(objOnSale);
        if (onSale)
        {
            return happyIcon;
        }
        else
        {
            return sadIcon;
        }
    }



Here is the screenshot :




so guys just enjoy with this program............nd don't forget ur feedback for this program

Comments

Popular posts from this blog

i am writing With sad hearts some problems with my windows phone samsung omnia w

Delete Duplicate Records in DataBase sql server