You must Sign In to post a response.
  • Category: ASP.Net MVC

    Bind Data into table / grid

    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.
  • #768189

    We have many simple methods to implement this,
    Following will be one of the basic method to implement grid/table in the view page.

    Below is the coding which you have to do with the Controller. Before Controller you should already declared the Model along with columns you are expected to shown in the grid.

    public ViewResult Index()
    var Details = db.Details.AsEnumerable<Detail>();

    return View(Details); //You just passed details as a model

    Below is the coding for the View page, where we are using this Details object and creating Grid.

    @model IEnumerable<Project.Details>
    @foreach (var item in Model)
    //Design for your DataGrid


  • #768190
    You want to create a view, and that view data you want to bind to grid. Is it?
    Please refer the below..

    //To bind data to the grid using
    string strCon = "Server=;Database=sample;Uid=sa;Pwd=sn";
    SqlConnection con = new SqlConnection(strCon);
    string strSQL = "SELECT * from yourview1";
    SqlCommand cmd = new SqlCommand(strSQL, con);
    GridView1.DataSource = cmd.ExecuteReader();
    catch (Exception ex)
    Label1.Text = "SQL Query failed: " + ex.Message;

    Hope this will help you.

    Never lose hope..You never know what tomorrow will bring

  • #768202
    Hai Pranjal,
    If you are working with ASP.Net MVC, then you can have the partial view, or View to bind the whole Model.
    To do this, you need to a controller and get the Model data as the List and then generate the View for the Model and fill the view.
    It will be simplest way where you need not to write lot of code as the code can be generated automatically using Scaffolding option. You just need to create the Model which you want to display in the View.
    Create Model:

    public class Customer
    // customer id
    public int Id{get;set;
    // customer name
    public string Name {get; set;}

    The mode part is done here.
    Now you can right click on the controller and choose this model as the List template in Scaffolding option.
    Provide the controller name and click OK.
    it will generate the controller with all the action items and will generate the views as well
    Hope it will be helpful to you.

    Pawan Awasthi(DNS MVM)
    +91 8123489140 (whatsApp), +60 14365 1476(Malaysia)

  • Sign In to post your comments