locked
How to CRUD in gridview using datatable in c# without database interaction RRS feed

  • Question

  • User1742515686 posted

    Hi! I only need one textbox inside gridview and other operational buttons like add , edit , delete in gridview. I just need that these buttons can save the data in datatable like we do :

    DataTable dt = new Datatable();

    I dont want to save it in database at this stage. Just tell me how can this be done ? CRUD Operations using datatable inside gridview is my aim.

    Thanks

    Wednesday, August 12, 2015 6:11 AM

Answers

  • User571301025 posted

    Just try to store your datatble in Session. Like below:

    Session["yourdatatable']=dt;

    Whenever you requrired, just take it from session and then use. (i.e., bind to the gridview).

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 12, 2015 6:30 AM
  • User461988170 posted

    Hi Emad khan, 

    Based on your requirement, I think you can use ViewState to accomplish it. You can save DataTable into ViewState and bind Gridview without DataBase. I write the sample code about add data in gridview.

    Code shown below:

    In .aspx page:

    <form id="form1" runat="server">
            <table style="color: White; margin-top: 30px; margin-left: 10px">
                <tr>
                    <td>Author Name   
                    </td>
                    <td>Book Name   
                    </td>
                    <td>Book Type   
                    </td>
                    <td>Publisher   
                    </td>
                </tr>
                <tr>
                    <td>
                        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                    </td>
                    <td>
                        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
                    </td>
                    <td>
                        <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
                    </td>
                    <td>
                        <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td>
                        <asp:Button ID="AddProduct" runat="server" Style="color: White" Text="Add Product"
                            OnClick="AddProduct_Click" BackColor="#999966" />
                    </td>
                </tr>
            </table>
    
            <div style="margin-left: 10px; margin-top: 10px">
                <asp:GridView ID="GridView1" AutoGenerateColumns="false" runat="server" CellPadding="4"
                    ForeColor="#333333" GridLines="None">
                    <AlternatingRowStyle BackColor="White" />
                    <Columns>
                        <asp:BoundField HeaderStyle-Width="120px" HeaderText="Author  Name" DataField="AuthorName" />
                        <asp:BoundField HeaderStyle-Width="120px" HeaderText=" Book Name" DataField="BookName" />
                        <asp:BoundField HeaderStyle-Width="120px" HeaderText=" Book Type" DataField="BookType" />
                        <asp:BoundField HeaderStyle-Width="120px" HeaderText="Publisher" DataField="Publisher" />
                    </Columns>
                    <EditRowStyle BackColor="#2461BF" />
                    <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                    <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                    <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
                    <RowStyle BackColor="#EFF3FB" />
                    <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
                    <SortedAscendingCellStyle BackColor="#F5F7FB" />
                    <SortedAscendingHeaderStyle BackColor="#6D95E1" />
                    <SortedDescendingCellStyle BackColor="#E9EBEF" />
                    <SortedDescendingHeaderStyle BackColor="#4870BE" />
                </asp:GridView>
            </div>
        </form>

    In .aspx.cs page:

    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    namespace SaveDataTableInViewsate
    {
        public partial class Default : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    AddDefaultFirstRecord();
                }
            }
    
            private void AddDefaultFirstRecord()
            {
                //creating dataTable    
                DataTable dt = new DataTable();
                DataRow dr;
                dt.TableName = "AuthorBooks";
                dt.Columns.Add(new DataColumn("AuthorName", typeof(string)));
                dt.Columns.Add(new DataColumn("BookName", typeof(string)));
                dt.Columns.Add(new DataColumn("BookType", typeof(string)));
                dt.Columns.Add(new DataColumn("Publisher", typeof(string)));
                dr = dt.NewRow();
                dt.Rows.Add(dr);
                //saving databale into viewstate    
                ViewState["AuthorBooks"] = dt;
                //bind Gridview   
                GridView1.DataSource = dt;
                GridView1.DataBind();
            }
    
            private void AddNewRecordRowToGrid()
            {
                // check view state is not null   
                if (ViewState["AuthorBooks"] != null)
                {
                    //get datatable from view state    
                    DataTable dtCurrentTable = (DataTable)ViewState["AuthorBooks"];
                    DataRow drCurrentRow = null;
    
                    if (dtCurrentTable.Rows.Count > 0)
                    {
    
                        for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
                        {
    
                            //add each row into data table   
                            drCurrentRow = dtCurrentTable.NewRow();
                            drCurrentRow["AuthorName"] = TextBox1.Text;
                            drCurrentRow["BrandName"] = TextBox2.Text;
                            drCurrentRow["Warrenty"] = TextBox3.Text;
                            drCurrentRow["Price"] = TextBox4.Text;
                        }
                        //Remove initial blank row   
                        if (dtCurrentTable.Rows[0][0].ToString() == "")
                        {
                            dtCurrentTable.Rows[0].Delete();
                            dtCurrentTable.AcceptChanges();
                        }
    
                        //add created Rows into dataTable   
                        dtCurrentTable.Rows.Add(drCurrentRow);
                        //Save Data table into view state after creating each row   
                        ViewState["AuthorBooks"] = dtCurrentTable;
                        //Bind Gridview with latest Row   
                        GridView1.DataSource = dtCurrentTable;
                        GridView1.DataBind();
                    }
                }
            }
    
            protected void AddProduct_Click(object sender, EventArgs e)
            {
                AddNewRecordRowToGrid();
            }
        }
    }

    I hope this will help you.

    Best Regards,

    Candy Zhou

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 13, 2015 4:43 AM

All replies

  • User571301025 posted

    Just try to store your datatble in Session. Like below:

    Session["yourdatatable']=dt;

    Whenever you requrired, just take it from session and then use. (i.e., bind to the gridview).

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 12, 2015 6:30 AM
  • User1742515686 posted

    Please provide me all coding. 

    Thanks.

    Wednesday, August 12, 2015 6:34 AM
  • User461988170 posted

    Hi Emad khan, 

    Based on your requirement, I think you can use ViewState to accomplish it. You can save DataTable into ViewState and bind Gridview without DataBase. I write the sample code about add data in gridview.

    Code shown below:

    In .aspx page:

    <form id="form1" runat="server">
            <table style="color: White; margin-top: 30px; margin-left: 10px">
                <tr>
                    <td>Author Name   
                    </td>
                    <td>Book Name   
                    </td>
                    <td>Book Type   
                    </td>
                    <td>Publisher   
                    </td>
                </tr>
                <tr>
                    <td>
                        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                    </td>
                    <td>
                        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
                    </td>
                    <td>
                        <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
                    </td>
                    <td>
                        <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td>
                        <asp:Button ID="AddProduct" runat="server" Style="color: White" Text="Add Product"
                            OnClick="AddProduct_Click" BackColor="#999966" />
                    </td>
                </tr>
            </table>
    
            <div style="margin-left: 10px; margin-top: 10px">
                <asp:GridView ID="GridView1" AutoGenerateColumns="false" runat="server" CellPadding="4"
                    ForeColor="#333333" GridLines="None">
                    <AlternatingRowStyle BackColor="White" />
                    <Columns>
                        <asp:BoundField HeaderStyle-Width="120px" HeaderText="Author  Name" DataField="AuthorName" />
                        <asp:BoundField HeaderStyle-Width="120px" HeaderText=" Book Name" DataField="BookName" />
                        <asp:BoundField HeaderStyle-Width="120px" HeaderText=" Book Type" DataField="BookType" />
                        <asp:BoundField HeaderStyle-Width="120px" HeaderText="Publisher" DataField="Publisher" />
                    </Columns>
                    <EditRowStyle BackColor="#2461BF" />
                    <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                    <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                    <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
                    <RowStyle BackColor="#EFF3FB" />
                    <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
                    <SortedAscendingCellStyle BackColor="#F5F7FB" />
                    <SortedAscendingHeaderStyle BackColor="#6D95E1" />
                    <SortedDescendingCellStyle BackColor="#E9EBEF" />
                    <SortedDescendingHeaderStyle BackColor="#4870BE" />
                </asp:GridView>
            </div>
        </form>

    In .aspx.cs page:

    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    namespace SaveDataTableInViewsate
    {
        public partial class Default : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    AddDefaultFirstRecord();
                }
            }
    
            private void AddDefaultFirstRecord()
            {
                //creating dataTable    
                DataTable dt = new DataTable();
                DataRow dr;
                dt.TableName = "AuthorBooks";
                dt.Columns.Add(new DataColumn("AuthorName", typeof(string)));
                dt.Columns.Add(new DataColumn("BookName", typeof(string)));
                dt.Columns.Add(new DataColumn("BookType", typeof(string)));
                dt.Columns.Add(new DataColumn("Publisher", typeof(string)));
                dr = dt.NewRow();
                dt.Rows.Add(dr);
                //saving databale into viewstate    
                ViewState["AuthorBooks"] = dt;
                //bind Gridview   
                GridView1.DataSource = dt;
                GridView1.DataBind();
            }
    
            private void AddNewRecordRowToGrid()
            {
                // check view state is not null   
                if (ViewState["AuthorBooks"] != null)
                {
                    //get datatable from view state    
                    DataTable dtCurrentTable = (DataTable)ViewState["AuthorBooks"];
                    DataRow drCurrentRow = null;
    
                    if (dtCurrentTable.Rows.Count > 0)
                    {
    
                        for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
                        {
    
                            //add each row into data table   
                            drCurrentRow = dtCurrentTable.NewRow();
                            drCurrentRow["AuthorName"] = TextBox1.Text;
                            drCurrentRow["BrandName"] = TextBox2.Text;
                            drCurrentRow["Warrenty"] = TextBox3.Text;
                            drCurrentRow["Price"] = TextBox4.Text;
                        }
                        //Remove initial blank row   
                        if (dtCurrentTable.Rows[0][0].ToString() == "")
                        {
                            dtCurrentTable.Rows[0].Delete();
                            dtCurrentTable.AcceptChanges();
                        }
    
                        //add created Rows into dataTable   
                        dtCurrentTable.Rows.Add(drCurrentRow);
                        //Save Data table into view state after creating each row   
                        ViewState["AuthorBooks"] = dtCurrentTable;
                        //Bind Gridview with latest Row   
                        GridView1.DataSource = dtCurrentTable;
                        GridView1.DataBind();
                    }
                }
            }
    
            protected void AddProduct_Click(object sender, EventArgs e)
            {
                AddNewRecordRowToGrid();
            }
        }
    }

    I hope this will help you.

    Best Regards,

    Candy Zhou

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 13, 2015 4:43 AM