How to Bind Gridview using Linq and Populated Records.
Looking for sample code to demonstrate How to Bind Gridview using Linq Query. Check out this sample code where, when you will
select Corresponding records and populate textboxes with retrieve item collection looping and then populate records using linq Query.
This article, will explain you How to Bind Gridview using Linq Query. This sample code is based on the scenario when you
select corresponding records and populat textboxes with retrieve item collection looping and then populate records using linq Query.Client side code for binding Gridview using Linq and Populated Records.
The Client Side code for this is as follows:
<%@ 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">
<asp:Panel ID="pnlShow" runat ="server">
<div style="border:Solid 3px #D55500;background-color:#D55500; text-align:center;">
<table>
<tr>
<td>Name</td>
<td>
<asp:TextBox ID="TxtName" runat ="server" ></asp:TextBox>
</td>
</tr>
<tr>
<td>
Empno</td>
<td>
<asp:TextBox ID="TxtEmpno" runat ="server" ></asp:TextBox>
</td>
</tr>
<tr>
<td>
Amount</td>
<td>
<asp:TextBox ID="TxtAmount" runat ="server" ></asp:TextBox>
</td>
</tr>
<tr>
<td colspan="2">
<asp:Label ID="lblMsg" runat ="server" ></asp:Label>
</td>
<td>
</td>
</tr>
<tr>
<td>
<asp:Button ID="Cmd_Save" runat="server" onclick="Cmd_Save_Click" Text="Save" />
</td>
<td>
<asp:Button ID="btnClose" runat="server" CssClass="myButton" Text="Close" />
</td>
<td>
</td>
</tr>
</table>
</div>
</asp:Panel>
<asp:GridView ID="grd1" runat ="server" AutoGenerateColumns ="false"
onrowcommand="grd1_RowCommand">
<Columns>
<asp:TemplateField HeaderText ="Name">
<ItemTemplate>
<asp:Label ID="lblName" runat ="server" Text ='<%# Eval("Ename") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText ="Eempno">
<ItemTemplate>
<asp:Label ID="lblEempno" runat ="server" Text ='<%# Eval("Eempno") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText ="Eamt">
<ItemTemplate>
<asp:Label ID="lblEamt" runat ="server" Text ='<%# Eval("Eamt") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Edit">
<ItemTemplate>
<asp:LinkButton CommandName="cmdBind" runat="server" Text='<%
#Eval("Eempno")%>' ID="hypeno" ToolTip='<%#Eval("Eempno")%>'>LinkButton
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Delete">
<ItemTemplate>
<asp:LinkButton CommandName="cmdDelBind" OnClientClick ="return confirm('Are You Sure Deletion?');"
runat="server" Text='<%#Eval("Eempno")%>' ID="LinkButton1" ToolTip='<%#Eval("Eempno")%>'>LinkButton
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</form>
</body>
</html>Server side code for binding Gridview using Linq and Populated Records.
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
{
public int intid;
protected void Page_Load(object sender, EventArgs e)
{
BindGrdis();
}
protected void Cmd_Save_Click(object sender, EventArgs e)
{
using (DataClassesDataContext Dbcontext = new DataClassesDataContext())
{
Table_11 Emp = new Table_11();
Emp.Ename = TxtName.Text;
Emp.Eempno = TxtEmpno.Text;
Emp.Eamt = TxtAmount.Text;
Dbcontext.Table_11s.InsertOnSubmit(Emp);
Dbcontext.SubmitChanges();
lblMsg.Text = "SucessfullY Saved!..";
if (Cmd_Save.Text == "Update")
{
var products = (from p in Dbcontext.Table_11s
where p.id == intid
select p).Single();
Dbcontext.SubmitChanges();
}
}
}
private void BindGrdis()
{
using (DataClassesDataContext Dbcontext = new DataClassesDataContext())
{
var Test = from p in Dbcontext.Table_11s select new { Ename = p.Ename, Eempno = p.Eempno, Eamt = p.Eamt };
grd1.DataSource = Test;
grd1.DataBind();
}
}
protected void grd1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "cmdBind")
{
LinkButton lb = (LinkButton)e.CommandSource;
Session["EditId"] =Convert.ToInt32(lb.Text);
intid = Convert.ToInt32(lb.Text);
using (DataClassesDataContext Dbcontext = new DataClassesDataContext())
{
var Test1 =
from p in Dbcontext.Table_11s
where p.Eempno == lb.Text
select new {id=p.id, Ename = p.Ename, Eempno = p.Eempno, Eamt = p.Eamt };
foreach (var Item in Test1)
{
ViewState["EditID"] = Item.id.ToString();
TxtName.Text = Item.Ename.ToString();
TxtEmpno.Text =Item.Eempno.ToString ();
TxtAmount.Text =Item.Eamt.ToString();
}
}
Cmd_Save.Text = "Update";
}
}
i have database table with user id ,name and address, i have connected database to gridview and create name has templete field and display name on linkbutton in grid view and i want that when i clicked that button i want all information of that user to be displayed on label