You are on page 1of 8

` // C # Code//

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;
using System.Text;
using System.Collections.Generic;

namespace Grid_view_Testing
{
public partial class _Default : System.Web.UI.Page
{
SqlConnection con;

protected void Page_Load(object sender, EventArgs e)


{

if (!IsPostBack)
{

con = new SqlConnection("user id


=sa;password=KUMAR;database=northwind;data source=localhost");
SqlDataAdapter da = new SqlDataAdapter("Select * from
Items", con);
DataSet ds = new DataSet();
da.Fill(ds, "Items");
GridView1.DataSource = ds.Tables["Items"];
GridView1.DataBind();

con.Close();
}

}
protected void Button1_Click(object sender, EventArgs e)
{

string selectedids = "";

foreach (GridViewRow r in GridView1.Rows)


{
if ((r.FindControl("chkgrd") as CheckBox).Checked)
{
selectedids = selectedids +
GridView1.DataKeys[r.RowIndex].Value.ToString() + ",";
}

}
selectedids = selectedids.TrimEnd(',');
con = new SqlConnection("database=northwind;data
source=localhost;user id=sa;password=KUMAR");
con.Open();
SqlDataAdapter da = new SqlDataAdapter("Select * from Items
where ItemID in(" + selectedids + ")", con);
DataSet ds = new DataSet();
da.Fill(ds, "Items");
GridView2.DataSource = ds.Tables["Items"];
GridView2.DataBind();
}
protected void Button2_Click(object sender, EventArgs e)
{
foreach (GridViewRow r in GridView1.Rows)
{
(r.FindControl("chkgrd") as CheckBox).Checked =false;
}
GridView2.DataSource = null;
GridView2.DataBind();
}

// Design Code//

<%@ Page Language="C#" AutoEventWireup="true"


CodeBehind="Default.aspx.cs" Inherits="Grid_view_Testing._Default" %>

<%@ Register assembly="Microsoft.ReportViewer.WebForms, Version=9.0.0.0,


Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
namespace="Microsoft.Reporting.WebForms" tagprefix="rsweb" %>

<!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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>

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


="true" DataKeyNames ="ItemId"

CellPadding="4" ForeColor="#333333" GridLines="None" >


<RowStyle BackColor="#E3EAEB" />

<Columns>
<asp:TemplateField HeaderText ="Select">
<ItemTemplate >
<asp:CheckBox ID="chkgrd" runat ="server" />

</ItemTemplate>
</asp:TemplateField>

</Columns>

<FooterStyle BackColor="#1C5E55" Font-Bold="True"


ForeColor="White" />
<PagerStyle BackColor="#666666" ForeColor="White"
HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True"
ForeColor="#333333" />
<HeaderStyle BackColor="#1C5E55" Font-Bold="True"
ForeColor="White" />
<EditRowStyle BackColor="#7C6F57" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>

</div><div ></div>
<div >
<table >
<tr>
<td ><asp:Button ID="Button1" runat="server"
Text="Show Selection" onclick="Button1_Click" /></td>
<td ><asp:Button ID="Button2" runat="server"
Text="Clear" onclick="Button2_Click" /></td>
</tr>
</table>
</div>
<div >
<asp:GridView ID="GridView2" runat="server"
BackColor="LightGoldenrodYellow"
BorderColor="Tan" BorderWidth="1px" CellPadding="2"
ForeColor="Black"
GridLines="None">
<FooterStyle BackColor="Tan" />
<PagerStyle BackColor="PaleGoldenrod"
ForeColor="DarkSlateBlue"
HorizontalAlign="Center" />
<SelectedRowStyle BackColor="DarkSlateBlue"
ForeColor="GhostWhite" />
<HeaderStyle BackColor="Tan" Font-Bold="True" />
<AlternatingRowStyle BackColor="PaleGoldenrod" />
</asp:GridView>
</div>

</form>
</body>
</html>
//Form Design//
InPut:
OutPut:
DataBase Design:
Enter Data Like This:

You might also like