asp.net gridview to find the record from the database
Introduction : This artical explain how to search record from the database using sql server database in asp.net c#
Step 1: Create Database
CREATE TABLE [dbo].[Reg] (
[Id] INT IDENTITY (1, 1) NOT NULL,
[Name] NVARCHAR (50) NOT NULL,
[RollNo] NVARCHAR (50) NOT NULL,
[Address] NVARCHAR (50) NOT NULL,
[Class] NVARCHAR (50) NOT NULL,
PRIMARY KEY CLUSTERED ([Id] ASC)
);
[Id] INT IDENTITY (1, 1) NOT NULL,
[Name] NVARCHAR (50) NOT NULL,
[RollNo] NVARCHAR (50) NOT NULL,
[Address] NVARCHAR (50) NOT NULL,
[Class] NVARCHAR (50) NOT NULL,
PRIMARY KEY CLUSTERED ([Id] ASC)
);
Step 2: Design the Webform.aspx as following
Step 3 : code in code behind file
namespace Search_In_Gridview
{
public partial class WebForm1 : System.Web.UI.Page
{
SqlDataAdapter da;
DataSet ds = new DataSet();
DataTable dt = new DataTable();
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindGrid();
}
}
private void BindGrid()
{
SqlConnection con = new SqlConnection();
ds = new DataSet();
con.ConnectionString = (@"Data Source=(LocalDB)\v11.0;AttachDbFilename=D:\VED
\Search_In_Gridview\Search_In_Gridview\App_Data\MYDB.mdf;Integrated Security=True");
SqlCommand cmd = new SqlCommand("SELECT * FROM Reg WHERE Name Like '%"
+ txtSearch.Text + "%'", con);
da = new SqlDataAdapter(cmd);
da.Fill(ds);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
if (ds.Tables[0].Rows.Count > 0)
{
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
}
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
BindGrid();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[0].Text = Regex.Replace(e.Row.Cells[0].Text, txtSearch.Text.Trim(),
delegate(Match match)
{
return string.Format("<span style = 'background-color:green;color:white;'>
{0}</span>", match.Value);
}, RegexOptions.IgnoreCase);
}
}
protected void Search_Click(object sender, EventArgs e)
{
this.BindGrid();
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
}
{
public partial class WebForm1 : System.Web.UI.Page
{
SqlDataAdapter da;
DataSet ds = new DataSet();
DataTable dt = new DataTable();
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindGrid();
}
}
private void BindGrid()
{
SqlConnection con = new SqlConnection();
ds = new DataSet();
con.ConnectionString = (@"Data Source=(LocalDB)\v11.0;AttachDbFilename=D:\VED
\Search_In_Gridview\Search_In_Gridview\App_Data\MYDB.mdf;Integrated Security=True");
SqlCommand cmd = new SqlCommand("SELECT * FROM Reg WHERE Name Like '%"
+ txtSearch.Text + "%'", con);
da = new SqlDataAdapter(cmd);
da.Fill(ds);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
if (ds.Tables[0].Rows.Count > 0)
{
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
}
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
BindGrid();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[0].Text = Regex.Replace(e.Row.Cells[0].Text, txtSearch.Text.Trim(),
delegate(Match match)
{
return string.Format("<span style = 'background-color:green;color:white;'>
{0}</span>", match.Value);
}, RegexOptions.IgnoreCase);
}
}
protected void Search_Click(object sender, EventArgs e)
{
this.BindGrid();
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
}
Run project as belove
Sign up here with your email
ConversionConversion EmoticonEmoticon