Working with LINQ to select Data from an Array plus Array Sorting
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
int[] Employees = { 5,6,4,3,2,6,8,1,4,7,3};
protected void Page_Load(object sender, EventArgs e)
{
// string[] Employees = { "abc", "bcd", "cde", "def", "efg", "fgh", "ghi" };
ShowData();
}
protected void btnSort_Click(object sender, EventArgs e)
{
int temp = 0;
int i,j;
for (i = 0; i < Employees.Length; i++)
{
for (j = 0; j < Employees.Length - 1; j++)
{
if( Employees[j]<Employees [j+1] )
{
temp=Employees[j];
Employees[j] = Employees[j+1];
Employees[j + 1] = temp;
}
}
}
ShowData();
}
public void ShowData()
{
GridView1.DataSource = from emp in Employees
where Employees.Length > 6
select emp;
GridView1.DataBind();
}
}
Default.aspx
<%@ 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></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" BackColor="White"
BorderColor="#3366CC" BorderStyle="None" BorderWidth="1px" CellPadding="4"
EnableModelValidation="True"
onselectedindexchanged="GridView1_SelectedIndexChanged" Width="263px">
<FooterStyle BackColor="#99CCCC" ForeColor="#003399" />
<HeaderStyle BackColor="#003399" Font-Bold="True" ForeColor="#CCCCFF" />
<PagerStyle BackColor="#99CCCC" ForeColor="#003399" HorizontalAlign="Left" />
<RowStyle BackColor="White" ForeColor="#003399" />
<SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
</asp:GridView>
<asp:Button ID="btnSort" runat="server" onclick="btnSort_Click"
Text="Sort List" />
</div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
int[] Employees = { 5,6,4,3,2,6,8,1,4,7,3};
protected void Page_Load(object sender, EventArgs e)
{
// string[] Employees = { "abc", "bcd", "cde", "def", "efg", "fgh", "ghi" };
ShowData();
}
protected void btnSort_Click(object sender, EventArgs e)
{
int temp = 0;
int i,j;
for (i = 0; i < Employees.Length; i++)
{
for (j = 0; j < Employees.Length - 1; j++)
{
if( Employees[j]<Employees [j+1] )
{
temp=Employees[j];
Employees[j] = Employees[j+1];
Employees[j + 1] = temp;
}
}
}
ShowData();
}
public void ShowData()
{
GridView1.DataSource = from emp in Employees
where Employees.Length > 6
select emp;
GridView1.DataBind();
}
}
Default.aspx
<%@ 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></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" BackColor="White"
BorderColor="#3366CC" BorderStyle="None" BorderWidth="1px" CellPadding="4"
EnableModelValidation="True"
onselectedindexchanged="GridView1_SelectedIndexChanged" Width="263px">
<FooterStyle BackColor="#99CCCC" ForeColor="#003399" />
<HeaderStyle BackColor="#003399" Font-Bold="True" ForeColor="#CCCCFF" />
<PagerStyle BackColor="#99CCCC" ForeColor="#003399" HorizontalAlign="Left" />
<RowStyle BackColor="White" ForeColor="#003399" />
<SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
</asp:GridView>
<asp:Button ID="btnSort" runat="server" onclick="btnSort_Click"
Text="Sort List" />
</div>
</form>
</body>
</html>
No comments:
Post a Comment