Search Records in Gridview mvc4 with start character word
Introduction:
This tutorial explain how to make search functionality in in gridview using the asp.net mvc4 web application. here we can use the entity data source for the getting the data from the database.when you run the project get the all data from the database and if you search data using the textbox then you get matching data of textbox and display in gridview with mvc4.
Snap shot:
Find Record using Some key word :
Now Create Model and database field name as below.
public class DAL
{
public int Id { get; set; }
public string Name { get; set; }
public string City { get; set; }
public string Education { get; set; }
}
Now Create Controller and add the following code and create action method.
public SqlConnection con = new SqlConnection(con);
public List<DAL> Select_data()
{
List<DAL> D = new List<DAL>();
try
{
con.Open();
SqlCommand cmd = new SqlCommand("select * from tbl_user", con);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
DAL da = new DAL();
da.Id = Convert.ToInt32(dr["id"].ToString());
da.Name = dr["Name"].ToString();
da.City = dr["City"].ToString();
da.Education = dr["Education"].ToString();
D.Add(da);
}
con.Close();
}
catch (Exception)
{
}
return D;
}
[HttpGet]
public ActionResult SearchGrid()
{
return View(Select_data());
}
[HttpPost]
public ActionResult SearchGrid(string SearchKey)
{
List<DAL> D = new List<DAL>();
DAL dd=new DAL();
var data = (from m in Select_data() where m.Name.StartsWith(SearchKey, StringComparison.OrdinalIgnoreCase) select new { m.Name, m.City, m.Education }).ToList();
foreach (var V in data)
{
D.Add(new DAL {
Name = V.Name,
City = V.City,
Education = V.Education
});
}
return View(D);
}
Now Create View To display Gridview as Following
@model IEnumerable<MVC4_Search_In_Grid.Models.DAL>
@{
ViewBag.Title = "Search";
}
@using (@Html.BeginForm())
{
<table>
<tr>
<td>
@Html.TextBox("SearchKey", "")
</td>
<td>
<input type="submit" value="Search" />
</td>
</tr>
</table>
<table>
<tr>
<th>
Name
</th>
<th>
City
</th>
<th>
Education
</th>
</tr>
<tbody>
@foreach (var item in Model)
{
<tr id="tr1">
<td>
@item.Name
</td>
<td>
@item.City
</td>
<td>
@item.Education
</td>
</tr>
}
</tbody>
</table>
}
Now run mvc web application and test gridview filter
Introduction:
This tutorial explain how to make search functionality in in gridview using the asp.net mvc4 web application. here we can use the entity data source for the getting the data from the database.when you run the project get the all data from the database and if you search data using the textbox then you get matching data of textbox and display in gridview with mvc4.
Snap shot:
Find Record using Some key word :
Now Create Model and database field name as below.
{
public int Id { get; set; }
public string Name { get; set; }
public string City { get; set; }
public string Education { get; set; }
}
Now Create Controller and add the following code and create action method.
public List<DAL> Select_data()
{
List<DAL> D = new List<DAL>();
try
{
con.Open();
SqlCommand cmd = new SqlCommand("select * from tbl_user", con);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
DAL da = new DAL();
da.Id = Convert.ToInt32(dr["id"].ToString());
da.Name = dr["Name"].ToString();
da.City = dr["City"].ToString();
da.Education = dr["Education"].ToString();
D.Add(da);
}
con.Close();
}
catch (Exception)
{
}
return D;
}
[HttpGet]
public ActionResult SearchGrid()
{
return View(Select_data());
}
[HttpPost]
public ActionResult SearchGrid(string SearchKey)
{
List<DAL> D = new List<DAL>();
DAL dd=new DAL();
var data = (from m in Select_data() where m.Name.StartsWith(SearchKey, StringComparison.OrdinalIgnoreCase) select new { m.Name, m.City, m.Education }).ToList();
foreach (var V in data)
{
D.Add(new DAL {
Name = V.Name,
City = V.City,
Education = V.Education
});
}
return View(D);
}
Now Create View To display Gridview as Following
@{
ViewBag.Title = "Search";
}
@using (@Html.BeginForm())
{
<table>
<tr>
<td>
@Html.TextBox("SearchKey", "")
</td>
<td>
<input type="submit" value="Search" />
</td>
</tr>
</table>
<table>
<tr>
<th>
Name
</th>
<th>
City
</th>
<th>
Education
</th>
</tr>
<tbody>
@foreach (var item in Model)
{
<tr id="tr1">
<td>
@item.Name
</td>
<td>
@item.City
</td>
<td>
@item.Education
</td>
</tr>
}
</tbody>
</table>
}
Now run mvc web application and test gridview filter
Sign up here with your email
ConversionConversion EmoticonEmoticon