Bind Data into table / grid
Hello.I want to create 1 view where I need to bind data.
What is the good way to bind the data.
Also, table and grid, what is the useful for this.
public ViewResult Index()
{
var Details = db.Details.AsEnumerable<Detail>();
return View(Details); //You just passed details as a model
}
@model IEnumerable<Project.Details>
@foreach (var item in Model)
{
//Design for your DataGrid
}
//To bind data to the grid using ASP.net
string strCon = "Server=192.2.3.4;Database=sample;Uid=sa;Pwd=sn";
SqlConnection con = new SqlConnection(strCon);
string strSQL = "SELECT * from yourview1";
SqlCommand cmd = new SqlCommand(strSQL, con);
try
{
con.Open();
GridView1.DataSource = cmd.ExecuteReader();
GridView1.DataBind();
con.Close();
}
catch (Exception ex)
{
Label1.Text = "SQL Query failed: " + ex.Message;
}
//Customer.cs
public class Customer
{
// customer id
public int Id{get;set;
// customer name
public string Name {get; set;}
}.