Wednesday, September 8, 2010

How to filter records based on req field using data table and datagrid

by Narendra, .Net Developer
using System;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net.Mail;

namespace examples_Post
{
public partial class _Default : System.Web.UI.Page
{


protected void Page_Load(object sender, EventArgs e)
{

// Binding the table to gridview to display
gv.DataSource = CreateTable();
gv.DataBind();

}
public static DataTable CreateTable()
{

// Creating the datatable with five columns.
DataTable studentdetails = new DataTable();

studentdetails.Columns.Add ("Id", typeof(Int32 ));
studentdetails.Columns.Add ("Name", typeof(String ));
studentdetails.Columns.Add ("Age", typeof(Int32 ));
studentdetails.Columns.Add ("City", typeof(String));
studentdetails.Columns.Add ("State", typeof(String));


// Filling the studentdetails datatable with 5 rows

studentdetails.Rows.Add(1, "Peter", 29, "Richmond", "VA");
studentdetails.Rows.Add(2, "Cowen", 28, "Newark", "NJ");
studentdetails.Rows.Add(3, "Blair", 24, "Albany", "NY");
studentdetails.Rows.Add(4, "Brien", 22, "Redmond", "WA");
studentdetails.Rows.Add(5, "Johnson", 27, "Dallas", "TX");

return studentdetails ;

}

protected void btnfind_Click(object sender, EventArgs e)
{

pnlDataGrids.Visible = true;

DataSet ds = new DataSet("ds");
//Adding the datatable to dataset 'ds'
DataTable studentdetails = ds.Tables.Add();

studentdetails.Columns.Add("Id", typeof(Int32));
studentdetails.Columns.Add("Name", typeof(String));
studentdetails.Columns.Add("Age", typeof(Int32));
studentdetails.Columns.Add("City", typeof(String));
studentdetails.Columns.Add("State", typeof(String));

studentdetails.Rows.Add(1, "Peter", 29, "Richmond", "VA");
studentdetails.Rows.Add(2, "Cowen", 28, "Newark", "NJ");
studentdetails.Rows.Add(3, "Blair", 24, "Albany", "NY");
studentdetails.Rows.Add(4, "Brien", 22, "Redmond", "WA");
studentdetails.Rows.Add(5, "Johnson", 27, "Dallas", "TX");

// searching the records based on the 'Age' enterend in the textbox


DataRow[] rows = ds.Tables[0].Select("Name='" + txtname.Text + "'");
DataTable table = ds.Tables[0].Clone();
foreach (DataRow row in rows)
{
table.ImportRow(row);
}
DG2.DataSource = table;
DG2.DataBind();


}
}
}

.aspx


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h3> How to use datatable and datagrid to Filter the records based on the required field </h3>
<table width="890px">
<tr>
<td style="width:300px">
<asp:Label ID="lblex" runat="server" Width="300px" Text=" Enter the Name of the person to find his record"></asp:Label>
</td>
<td style="width:120px">
<asp:TextBox ID="txtname" Width="120px" runat="server"></asp:TextBox>
</td>
<td >
<asp:Button ID="Button1" runat="server" Width="200px" Text="Click here to Find Records" OnClick="btnfind_Click" />
</td>
</tr>
</table>
<br /><br />
<asp:Panel ID="pnlDataGrids" runat="server" Visible="False">
<b>Display of the record using the Select() method based on the required filed entered in textbox:</b>
<br />

<asp:DataGrid ID="DG2" runat="server">
<AlternatingItemStyle BackColor="Aqua"></AlternatingItemStyle>
<ItemStyle Font-Size="Medium" Font-Names="Verdana"></ItemStyle>
<HeaderStyle Font-Bold="True" Font-Size="Large" ForeColor="Brown" BackColor="Beige">
</HeaderStyle>
</asp:DataGrid>
</asp:Panel>

<br /><br />
<table>
<tr>
<td align="center">
<asp:GridView ID="gv" BorderStyle="Groove" ShowFooter="true" ShowHeader="true" GridLines="Both"
DataKeyNames="Id" BorderColor="brown" ForeColor="Red" Font-Size="Medium" Font-Bold="true"
AlternatingRowStyle-BackColor="AliceBlue" AlternatingRowStyle-Font-Bold="true"
AlternatingRowStyle-Font-Size="Medium" AlternatingRowStyle-ForeColor="BlueViolet"
CellPadding="2" CellSpacing="0" Width="890px" runat="server">
</asp:GridView>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>

No comments:

Post a Comment