Skip to main content

Build Products Comparision Table/Grid in ASP.Net C#.Net and VB.Net

In this tutorial i am going to explain about how to Build Products Comparision Table/Grid in ASP.Net,C#.Net and VB.Net. In this tutorial i did it using nested for loops.


In my previous article i have explained about Create Help Pages For Asp.Net Web API, Store Custom Objects In Configuration File , Create Simple Mathmatical Captcha In ASP.Net , Visual Studio Keyboard Shortcuts , How To get data from WEB API , How To Create Your First WEB API Project and many articles in C#.Net,VB.Net,Grid View,Javascript,jQuery,SQL Server and many other topics.


Open the visual studio and go to the file menu. Select new website and then select ASP.Net Empty Form Website template from C# templates or VB Templates as per your requirement. Then right click on the project then add a new webform and name it as Default.aspx. Now in the code behind file i am creating a datatable (Include System.Data namespace if it is not already there to access DataTable class) to store the details of mobile models. The entire code is given below.

C#.Net:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class _Default : System.Web.UI.Page
{
    public DataTable mobileTable = new DataTable("Mobiles");
    protected void Page_Load(object sender, EventArgs e)
    {
        //Adding columns to the table
        mobileTable.Columns.Add("Model", typeof(string));
        mobileTable.Columns.Add("Brand", typeof(string));
        mobileTable.Columns.Add("Operating System", typeof(string));
        mobileTable.Columns.Add("OS Version", typeof(string));
        mobileTable.Columns.Add("Rear Camera", typeof(string));
        mobileTable.Columns.Add("Front Camera", typeof(string));
        mobileTable.Columns.Add("Processor", typeof(string));
        mobileTable.Columns.Add("RAM", typeof(string));
        mobileTable.Columns.Add("Price", typeof(int));

        //Adding rows to the tables
        mobileTable.Rows.Add("Samsung Core 2 Duos", "Samsung", "Android", "Gingerbird","8MP","2MP","1.3 gHZ QuadCore","1 GB",11999);
        mobileTable.Rows.Add("LG G3", "LG", "Android", "KitKat", "13MP", "5MP", "1.7 gHZ QuadCore", "2 GB", 9999);
        mobileTable.Rows.Add("HTC M8", "HTC", "Android", "Lollipop", "8MP", "5MP", "2.4 gHZ QuadCore", "3 GB", 14499);
    }
}

VB.Net:

Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Data

Partial Public Class _Default
    Inherits System.Web.UI.Page
    Public mobileTable As New DataTable("Mobiles")
    Protected Sub Page_Load(sender As Object, e As EventArgs)
        'Adding columns to the table
        mobileTable.Columns.Add("Model", GetType(String))
        mobileTable.Columns.Add("Brand", GetType(String))
        mobileTable.Columns.Add("Operating System", GetType(String))
        mobileTable.Columns.Add("OS Version", GetType(String))
        mobileTable.Columns.Add("Rear Camera", GetType(String))
        mobileTable.Columns.Add("Front Camera", GetType(String))
        mobileTable.Columns.Add("Processor", GetType(String))
        mobileTable.Columns.Add("RAM", GetType(String))
        mobileTable.Columns.Add("Price", GetType(Integer))

        'Adding rows to the tables
        mobileTable.Rows.Add("Samsung Core 2 Duos", "Samsung", "Android", "Gingerbird", "8MP", "2MP", _
            "1.3 gHZ QuadCore", "1 GB", 11999)
        mobileTable.Rows.Add("LG G3", "LG", "Android", "KitKat", "13MP", "5MP", _
            "1.7 gHZ QuadCore", "2 GB", 9999)
        mobileTable.Rows.Add("HTC M8", "HTC", "Android", "Lollipop", "8MP", "5MP", _
            "2.4 gHZ QuadCore", "3 GB", 14499)
    End Sub
End Class

Now in the aspx file i wrote the below nested for loop to form the comparision grid/table of all the mobile models in the datatable. Below is the markup i used.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Build Items/Products Comparision Table/Grid in ASP.Net C#.Net and VB.Net
</title>
</head>
<body>
    <style type="text/css">
        td {
            padding:5px;
        }
    </style>
    <form id="form1" runat="server">
        <div>
            <h2>Comparision Table/Grid in ASP.Net C#.Net and VB.Net</h2>
            <table border="1" style="border-collapse:collapse;padding:2px;">
                <%
                    for (int i = 0; i < mobileTable.Columns.Count; i++)
                    {
                %>
                <tr>
                    <td><%= mobileTable.Columns[i].ColumnName %></td>
                    <%
                        for (int j = 0; j < mobileTable.Rows.Count; j++)
                        {
                    %>
                    <td><%= mobileTable.Rows[j][i] %></td>
                    <%
      }

                    %>
                </tr>
                <% 
                    }
                %>
            </table>
        </div>
    </form>
</body>
</html>

Now if you run the project the output will look like below image.
Output of Build Products Comparision Table/Grid in ASP.Net C#.Net and VB.Net
SourceCode:
Sourcecode of Build Products Comparision Table/Grid in ASP.Net C#.Net and VB.Net
If you like this article then share with your friends and comment your valuable feedback.. Happy coding..

Comments

Popular posts from this blog

Code To Convert rupees(numbers) into words using C#.Net

Introduction: In my previous article I have explained about how to validate emailid using javascript . In this article I am going to explain about code used to convert rupees(numbers) into words using C#.Net . Explanation: For explanation purpose I have a page. It has a textbox to input the numbers. And when you click on the convert to words button then it will convert the input numbers into words and shows it in the below label. Below is the C# code used to do this functionality. public static string NumbersToWords( int inputNumber) {     int inputNo = inputNumber;     if (inputNo == 0)         return "Zero" ;     int [] numbers = new int [4];     int first = 0;     int u, h, t;     System.Text. StringBuilder sb = new System.Text. StringBuilder ();     if (inputNo < 0)     { ...

CHECK IF FUNCTION EXISTS EXAMPLE

http://css-tricks.com/snippets/javascript/check-if-function-exists-before-calling/ When using scripts that are shared between different areas of a site, there may be cases where a function is called that doesn't exist. It makes sense on one page (the dependency is there) but not another. The difference is too slight to warrant forking the file into different versions. Instead, you can just check if the function exists before calling it to avoid the error: if ( typeof yourFunctionName == 'function' ) { yourFunctionName ( ) ; } jQuery code snippet to check whether a function exists within the JavaScript code. This can be easily achieved by using the jQuery.isFunction() function. Useful for checking if a jQuery function exists before calling it! < script type = "text/javascript" >      function  somenoobfunction ( )   {      }         // using jQuery      if   ( $. isFunction ( wi...

Various components of the .NET Platform and the functions performed by them

Now we will go in detail about the various components that build the .NET framework and its functionalities. Common Language Runtime At the core of the .NET platform is the Common Language Runtime (CLR). The CLR simplifies application development, provides a robust and secure execution environment, supports multiple languages and simplifies application deployment and management. The diagram below provides more details on the CLR's features: In this section we will cover some of the more significant features provided to .NET applications by the CLR. These include: • Memory Management • Common Type System Before moving further let us discuss briefly about Common Language Infrastructure(CLI) according to Standardizing Information and Communication Systems(ECMA) specifications. The Microsoft Shared Source CLI Implementation is a file archive containing working source code for the ECMA-334 (C#) and ECMA-335 (Common Language Infrastructure, or CLI) s...