locked
Gridview CRUD RRS feed

  • Question

  • User-570626059 posted

    Hi, being new to web development, whats the common approach to enable CRUD operations for dataview grid?

    I did try go down he entity model and model bind approach but I couldnt get it working in the end.  I am just looking for a quick way to do this as I will need several pages with difference grids which need crud operation.

    thanks

    Monday, July 9, 2018 11:57 AM

Answers

  • User283571144 posted

    Hi skyblue28,

    According to your description, I suggest you could use  strongly-typed gridview crud combined with Entity Framework to achieve your requirement.

    You could write the select update delete method in the code-behind with EF and bind it with girdview build-in event.

    I also suggest you could create a form view, you could use its Insert method to achieve inserting data into database.

    More details, you could refer to below codes:

    ASPX:

    <form id="form1" runat="server">
            <div>
                <asp:GridView ID="GridView1" runat="server" SelectMethod="GridView1_GetData" DeleteMethod="GridView1_DeleteItem" UpdateMethod="GridView1_UpdateItem" 
                  DataKeyNames="CustomerID"  PageSize="10"  ItemType="GridViewCrudDemo.CrudDemos.Customer"  AutoGenerateEditButton="true"
                    AutoGenerateDeleteButton="true"   AllowPaging="true" 
                 >
                </asp:GridView>
                          
            </div>
            
            <div>
     
                <asp:FormView ID="FormView1" runat="server" DefaultMode="Insert" InsertMethod="FormView1_InsertItem"
                      
                    ItemType="GridViewCrudDemo.CrudDemos.Customer" >
                 
                   <InsertItemTemplate>
                      
                      <table>                                                                                                                                         
                       <tr><td>   <asp:Label ID="Label2" runat="server" Text="CustomerID"></asp:Label><asp:TextBox ID="TextBox1" runat="server" Text='<%# BindItem.CustomerID %>'></asp:TextBox>    </td> 
     
                        <td><asp:Label ID="Label1" runat="server" Text="CompanyName"></asp:Label><asp:TextBox ID="TextBox" runat="server" Text='<%# BindItem.CompanyName %>'></asp:TextBox></td>
                                                                       
                        <td><asp:Label ID="Label3" runat="server" Text="City"></asp:Label><asp:TextBox ID="TextBox2" runat="server" Text='<%# BindItem.City %>'></asp:TextBox> </td>
                                      <td><asp:Label ID="Label4" runat="server" Text="ContactTitle"></asp:Label><asp:TextBox ID="TextBox3" runat="server" Text='<%# BindItem.ContactTitle %>'></asp:TextBox> </td>
     
                           <td>  <asp:Button runat="server" Text="Insert" CommandName="Insert" /></td>
                       </tr>                                                                                                                                          <!-- use BindItem to pass value to the insert method -->
                       
                      </table>
                          
                           
               
                        </InsertItemTemplate>
                        </asp:FormView>
            </div>
             <asp:ValidationSummary ID="ValidationSummary1" runat="server" />
    </form>
     
    

    Code-behind:

    //   instantiate your DbContext
    MyDbContext MyDbContext = new MyDbContext();
    
            public IQueryable<Customer> GridView1_GetData()
            {
                return MyDbContext.Customers;
            }
    
            // The id parameter name should match the DataKeyNames value set in the gridview
            public void GridView1_DeleteItem(string CustomerID)
            {
                //  get the customer from EF through id  passed by gridview  if you specified the DeleteMethod
                Customer customer=  MyDbContext.Customers.Find(CustomerID);
                // remove the found customer 
                MyDbContext.Customers.Remove(customer);
                // save data
                MyDbContext.SaveChanges();
            }
     
    
    
            // The id parameter name should match the DataKeyNames value set in the gridview
            public void GridView1_UpdateItem(string CustomerID)
            {
                // first ensure the data is valid 
                if (!ModelState.IsValid)
                {
                    return;
                }
                //  get the customer from EF through id  passed by gridview  
                Customer customer = MyDbContext.Customers.Find(CustomerID);
                // use TryUpdateModel method to populate the customer  with the data passed from gridview
                TryUpdateModel(customer);
                //save data
                MyDbContext.SaveChanges();
     
     
            }
    
    
           //use object of type customer to get value from formview 
            // the value will be bound to customer automaticlly
            // or you can use the method TryUpdateMedel to populate your model
            public void FormView1_InsertItem(Customer customer)
            {
                //Customer customer=new Customer()
                //TryUpdateModel(customer)
                if (ModelState.IsValid)
                {
                    MyDbContext.Customers.Add(customer);
     
                    MyDbContext.SaveChanges();
                    Response.Redirect("~/CrudDemos/SimpleGridViewCrud.aspx");
                }
     
    

    Result:

    Best Regards,

    Brando

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 10, 2018 7:33 AM

All replies

  • User-525215917 posted

    Try if C# Corner can help you: https://www.c-sharpcorner.com/UploadFile/rahul4_saxena/crud-operations-using-entity-framework-in-Asp-Net/

    Their article covers how to use GridView and Entity Framework for basic CRUD operations.

    Monday, July 9, 2018 1:27 PM
  • User-1171043462 posted

    Refer my article for complete example and working sample code.

    Select Insert Edit Update and Delete in GridView using Entity Framework in ASP.Net

    Any doubts you can ask.

    Monday, July 9, 2018 5:38 PM
  • User283571144 posted

    Hi skyblue28,

    According to your description, I suggest you could use  strongly-typed gridview crud combined with Entity Framework to achieve your requirement.

    You could write the select update delete method in the code-behind with EF and bind it with girdview build-in event.

    I also suggest you could create a form view, you could use its Insert method to achieve inserting data into database.

    More details, you could refer to below codes:

    ASPX:

    <form id="form1" runat="server">
            <div>
                <asp:GridView ID="GridView1" runat="server" SelectMethod="GridView1_GetData" DeleteMethod="GridView1_DeleteItem" UpdateMethod="GridView1_UpdateItem" 
                  DataKeyNames="CustomerID"  PageSize="10"  ItemType="GridViewCrudDemo.CrudDemos.Customer"  AutoGenerateEditButton="true"
                    AutoGenerateDeleteButton="true"   AllowPaging="true" 
                 >
                </asp:GridView>
                          
            </div>
            
            <div>
     
                <asp:FormView ID="FormView1" runat="server" DefaultMode="Insert" InsertMethod="FormView1_InsertItem"
                      
                    ItemType="GridViewCrudDemo.CrudDemos.Customer" >
                 
                   <InsertItemTemplate>
                      
                      <table>                                                                                                                                         
                       <tr><td>   <asp:Label ID="Label2" runat="server" Text="CustomerID"></asp:Label><asp:TextBox ID="TextBox1" runat="server" Text='<%# BindItem.CustomerID %>'></asp:TextBox>    </td> 
     
                        <td><asp:Label ID="Label1" runat="server" Text="CompanyName"></asp:Label><asp:TextBox ID="TextBox" runat="server" Text='<%# BindItem.CompanyName %>'></asp:TextBox></td>
                                                                       
                        <td><asp:Label ID="Label3" runat="server" Text="City"></asp:Label><asp:TextBox ID="TextBox2" runat="server" Text='<%# BindItem.City %>'></asp:TextBox> </td>
                                      <td><asp:Label ID="Label4" runat="server" Text="ContactTitle"></asp:Label><asp:TextBox ID="TextBox3" runat="server" Text='<%# BindItem.ContactTitle %>'></asp:TextBox> </td>
     
                           <td>  <asp:Button runat="server" Text="Insert" CommandName="Insert" /></td>
                       </tr>                                                                                                                                          <!-- use BindItem to pass value to the insert method -->
                       
                      </table>
                          
                           
               
                        </InsertItemTemplate>
                        </asp:FormView>
            </div>
             <asp:ValidationSummary ID="ValidationSummary1" runat="server" />
    </form>
     
    

    Code-behind:

    //   instantiate your DbContext
    MyDbContext MyDbContext = new MyDbContext();
    
            public IQueryable<Customer> GridView1_GetData()
            {
                return MyDbContext.Customers;
            }
    
            // The id parameter name should match the DataKeyNames value set in the gridview
            public void GridView1_DeleteItem(string CustomerID)
            {
                //  get the customer from EF through id  passed by gridview  if you specified the DeleteMethod
                Customer customer=  MyDbContext.Customers.Find(CustomerID);
                // remove the found customer 
                MyDbContext.Customers.Remove(customer);
                // save data
                MyDbContext.SaveChanges();
            }
     
    
    
            // The id parameter name should match the DataKeyNames value set in the gridview
            public void GridView1_UpdateItem(string CustomerID)
            {
                // first ensure the data is valid 
                if (!ModelState.IsValid)
                {
                    return;
                }
                //  get the customer from EF through id  passed by gridview  
                Customer customer = MyDbContext.Customers.Find(CustomerID);
                // use TryUpdateModel method to populate the customer  with the data passed from gridview
                TryUpdateModel(customer);
                //save data
                MyDbContext.SaveChanges();
     
     
            }
    
    
           //use object of type customer to get value from formview 
            // the value will be bound to customer automaticlly
            // or you can use the method TryUpdateMedel to populate your model
            public void FormView1_InsertItem(Customer customer)
            {
                //Customer customer=new Customer()
                //TryUpdateModel(customer)
                if (ModelState.IsValid)
                {
                    MyDbContext.Customers.Add(customer);
     
                    MyDbContext.SaveChanges();
                    Response.Redirect("~/CrudDemos/SimpleGridViewCrud.aspx");
                }
     
    

    Result:

    Best Regards,

    Brando

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 10, 2018 7:33 AM
  • User-570626059 posted

    Brilliant thanks, 

    how do I set some fields as not editable, also how can I have a drop down in one of the fields which is populated by enum within my model , so the only option available are - "Live,Not Live"?

    thanks

    Tuesday, July 10, 2018 8:34 AM
  • User-1171043462 posted

    You can set ReadOnly="true" for BoundField. So they wont be editable

    Tuesday, July 10, 2018 5:22 PM