linq gridview

Here I am going to show how you can bind the GridView with Linq.
First let us have a look to our aspx page.
/*.aspx page*/

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

 

<!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 id="Head1" runat="server">

    <title>Untitled Page</title>

</head>

<body>

    <form id="form1" runat="server">

    <asp:ScriptManager ID="ScriptManager1" runat="server">

    </asp:ScriptManager>

    <div>

        <table align="center">

            <tr>

                <td>

                    <asp:Label ID="Label1" runat="server" Text="Region:"></asp:Label>

                </td>

                <td>

                    <asp:UpdatePanel ID="UpdatePanel1" runat="server">

                            <ContentTemplate>

                                <asp:DropDownList ID="ddlregion" runat="server" AutoPostBack="True"

                                    onselectedindexchanged="ddlregion_SelectedIndexChanged">

                                </asp:DropDownList>

                            </ContentTemplate>

                    </asp:UpdatePanel>                   

                </td>

            </tr>

            <tr>

                <td>

                </td>

                <td>

                     <asp:UpdatePanel ID="UpdatePanel2" runat="server">

                            <ContentTemplate>

                                <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"

                                    BackColor="White" BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px"

                                    CellPadding="4" ForeColor="Black" GridLines="Vertical">                                   

                                    <FooterStyle BackColor="#CCCC99" />

                                    <RowStyle BackColor="#F7F7DE" />

                                    <Columns>

                                        <asp:BoundField HeaderText="Name" DataField="name" />

                                        <asp:BoundField HeaderText="Country" DataField="country" />

                                        <asp:BoundField HeaderText="Region"  DataField="region"/>

                                        <asp:BoundField HeaderText="City"   DataField="city"/>

                                    </Columns>                                   

                                    <PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />

                                    <SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />

                                    <HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />

                                    <AlternatingRowStyle BackColor="White" />

                                </asp:GridView>

                            </ContentTemplate>

                    </asp:UpdatePanel>

                    <asp:UpdateProgress ID="UpdateProgress1" runat="server">

                        <ProgressTemplate>

                            Loading...

                        </ProgressTemplate>

                    </asp:UpdateProgress>

                </td>

            </tr>

        </table>

    </div>

    </form>

</body>
</html> 
The code behind to fetch the records through linq and bind it to the GridView.
/*.cs file for Bind Gridview*/

using System;

using System.Collections;

using System.Configuration;

using System.Data;

using System.Linq;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.HtmlControls;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Xml.Linq;

using System.Data.SqlClient;

 

public partial class Default2 : System.Web.UI.Page

{

    connect connect = new connect(); // Connection class contain connection string.

    protected void Page_Load(object sender, EventArgs e)

    {

        if (!IsPostBack)

        {

            Bindregiondata();

        }

    }

 

    private void Bindregiondata()

    {

        using (SqlConnection con = new SqlConnection(connect.Getconnection()))

        {

            con.Open();

            testLINQinfoDataContext db = new testLINQinfoDataContext();

 

            var regioninfo = from p in db.REGIONs

                             orderby p.regionname

                             select new

                             {

                                 regionid = p.regionid,

                                 regionname = p.regionname

                             };

 

            ddlregion.DataSource = regioninfo;

            ddlregion.DataTextField = "regionname";

            ddlregion.DataValueField = "regionid";

            ddlregion.DataBind();

 

            ddlregion.Items.Insert(0, "--Select Regioname--");

 

        }

    }

    protected void ddlregion_SelectedIndexChanged(object sender, EventArgs e)

    {

        using (SqlConnection con = new SqlConnection(connect.Getconnection()))

        {

            testLINQinfoDataContext db = new testLINQinfoDataContext();

 

            var employeeinfo = from p in db.EMPLOYEEINFOs

                               join c in db.COUNTRies

                               on p.countryid equals c.countryid

                               join r in db.REGIONs

                               on p.regionid equals r.regionid

                               join d in db.CITies

                               on p.cityid equals d.cityid

                               where p.regionid == Convert.ToInt32(ddlregion.SelectedItem.Value)

                               select new

                               {

                                   name = p.empname,

                                   country = c.countryname,

                                   region = r.regionname,

                                   city = d.cityname

                               };

 

            GridView1.DataSource = employeeinfo;

            GridView1.DataBind();

        }

    }

}
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值