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)     { ...

Dynamically programmatically add contols at run time Asp.Net

Introduction: In my previous article I have explained about What is View State? How to Store and retrieve values from View State . In this article I am going to explain how to add controls programmatically on run time. Explanation: This example adds a text box and button to a Web Forms page at run time. It also dynamically binds an event handler to the button's   Click   event. The handler displays the values of the dynamically generated text box. The controls are added into a   Panel   Web server control, which acts as a placeholder. The controls are separated in the panel with line breaks (HTML <BR> elements), which are added to the panel using the   LiteralControl   control.

C# Extension Methods Example

In this article i am going to explain about Extension Methods  with example. Extension methods enable you to "add" methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. Extension methods are a special kind of static method, but they are called as if they were instance methods on the extended type. For client code written in C# and Visual Basic, there is no apparent difference between calling an extension method and the methods that are actually defined in a type. If you do implement extension methods for a given type, remember the following points: An extension method will never be called if it has the same signature as a method defined in the type. Extension methods are brought into scope at the namespace level. For example, if you have multiple static classes that contain extension methods in a single namespace named Extensions, they will all be brought into scope by the using Extensions; directive. ...