In this example we will learn how to use Casting method that could also be enumerated. We also learn how to use a Text Box in a GridView control using empty template.

Keywords:

  • Enumerable Cast Method
    • IEnumerable<string> query = grades.Cast<string>().Select(g => g);
  • <EmptyDataTemplate><asp:TextBox ID="TextBox1" runat="server" Height="49px" Width="108px"></asp:TextBox></EmptyDataTemplate>

 

Step:1Crtea anew web site add a style sheet and then edit Default page by using the code given below

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>mm.LINQIntro11: Casting Enumerable Method</title>
<link href="main.css" rel="stylesheet" type="text/css" />
</head>
<body>
<form id="form1" runat="server">
<div id="div1">
<table style="width:100%;">
<tr><td colspan="2" >&nbsp; Casting Enumerable Method</td></tr>
<tr><td id="td1" >&nbsp
<asp:GridView ID="GridView1" runat="server" Width="59px" >
<EmptyDataTemplate><asp:TextBox ID="TextBox1" runat="server" Height="49px" Width="108px"></asp:TextBox>
</EmptyDataTemplate>
</asp:GridView>

</td><td id="td2"><asp:Label ID="L1" runat="server" Text=""></asp:Label></td>
</tr><tr><td colspan="2" >&nbsp;</td></tr>
</table>
</div>
</form>
</body>
</html>

Code: Default.aspx.cs

using System;
using System.Data;
using System.Linq;
using System.Linq.Expressions;
using System.Collections.Generic;
using System.Collections;

public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
List<int> grades = new List<int> { 78, 92, 100, 37, 81 };
IEnumerable<string> query = grades.Cast<string>().Select(g => g);
IEnumerable<double> q2 = grades.Cast<double>().Select(g => g);
L1.Text += "List<int> { 78, 92, 100, 37, 81 } <br/>";
L1.Text += " grades.LongCount().ToString() = "+ grades.LongCount().ToString() + "<br/>";
int n1 = 0;
foreach (string grade in query)
{
L1.Text += grade + "&nbsp" + "data Type : " + grade.GetType()+ "<br/>" ;
}

L1.Text += "<br/> Adding a quadratic number each round <br/>";
foreach (double grade in q2)
{
L1.Text += grade + n1 + " data Type : " + grade.GetType() + "<br/>";
n1++;
}

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

Code: menu.css

body
{
background-color:Gray;
}
#div1
{
background-color: #FFFFCC; color: #000080; position: absolute; width:700px; height: 450px;
padding-left: 20px; border-color:Maroon; border-style:solid; border-width: 4px; font-size:16px;
}
table
{
width : 700px; vertical-align:top;
}
#td1
{
width : 100px;vertical-align:top; text-align:left;
}
#td2
{
width : 500px;vertical-align:top; text-align:left;
}
#GridView1
{
width : 200px; border-style: solid; border-width:2px; border-color:Navy;
}

Step:2 Run time View

Step:3 Below shows the flow of coding and casting with Enumerable methods.

Step:4

Step:5

Step:6

Step:7

Step:8

Step:9

Step:10