Click here to Skip to main content
15,890,670 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am doing the application in asp.net using grid view and save that data into the data base.


In GridView i want the following design as follows;


Sno Name Age Address

textbox textbox textbox textbox

textbox textbox textbox textbox

textbox textbox textbox textbox

AddNewRow Insert
(Button) (button)

I want when i olick the AddNewRow (Button) another row to be added in the gridview and when i click the Insert (Button) the inserted data to be saved in the database.


for that i want to do the code and send the source code also.please help me.
Posted

You can follow these steps:

1.Create GridView in ASPX Page
ASP.NET
<asp:gridview id="GridView1" runat="server" autogeneratecolumns="false" xmlns:asp="#unknown">
            AllowSorting="True">
        <columns>
                <asp:templatefield headertext="SNO">
                    <edititemtemplate>
                        <asp:textbox id="txtSNO" runat="server" text="<%# Bind("SNO") %>"></asp:textbox>
                    </edititemtemplate>
                    <itemtemplate>
                      <asp:label id="lblSNO" runat="server" text="<%# Bind("SNO") %>"></asp:label>
                    </itemtemplate>
                </asp:templatefield>
                
                <asp:templatefield headertext="Name">
                    <edititemtemplate>
                        <asp:textbox id="txtName" runat="server" text="<%# Bind("Name") %>"></asp:textbox>
                    </edititemtemplate>
                     <itemtemplate>
                      <asp:label id="lblName" runat="server" text="<%# Bind("Name") %>"></asp:label>
                    </itemtemplate>
                </asp:templatefield>
                
                <asp:templatefield headertext="Age">
                    <edititemtemplate>
                        <asp:textbox id="txtAge" runat="server" text="<%# Bind("Age") %>"></asp:textbox>
                    </edititemtemplate>
                     <itemtemplate>
                      <asp:label id="lblAge" runat="server" text="<%# Bind("Age") %>"></asp:label>
                    </itemtemplate>
                </asp:templatefield>

                <asp:templatefield headertext="Address">
                    <edititemtemplate>
                        <asp:textbox id="txtAddress" runat="server" text="<%# Bind("Address") %>"></asp:textbox>
                    </edititemtemplate>
                     <itemtemplate>
                      <asp:label id="lblAddress" runat="server" text="<%# Bind("Address") %>"></asp:label>
                    </itemtemplate>
                </asp:templatefield>
            </columns>
            <selectedrowstyle forecolor="#CCFF99" font-bold="True">
                BackColor="#009999"></selectedrowstyle>
            <rowstyle forecolor="#003399" backcolor="White"></rowstyle>
        </asp:gridview>

2.Create Buttons in ASPX Page
ASP.NET
<asp:button id="btnAddNewRow" runat="server" text="AddNewRow" onclick="BtnAddNewRowClick" xmlns:asp="#unknown" />
<asp:button id="btnInsert" runat="server" text="Insert" onclick="BtnInsertClick" xmlns:asp="#unknown" />

3.Define your DataType
it should be Serializable
C#
[Serializable]
internal class Sample
{
    public static  Sample CreateTestSample(int i)
    {
        return new Sample
                   {
                       SNO = "SNO " + i,
                       Address = "Address " + i,
                       Age = i,
                       Name = "Name " + i,
                       City = "City " + i
                   };
    }
    
    public string SNO { get; set; }
    public string Name { get; set; }
    public string City { get; set; }
    public int Age { get; set; }
    public string Address { get; set; }
}

4.Define Persistent Object
C#
private List<sample> DataSamples
{
    get { return  ViewState["Samples"] as List<sample>; }
    set { ViewState["Samples"] = value; }
}

5.Initialize GridView control
C#
protected void Page_Load(object sender, EventArgs e)
       {
           // Fill GridView for First Time
           if (!IsPostBack)
           {
               FillGrid();
           }
       }

6.Fill GridView control
C#
private void FillGrid()
        {
            // For test sapmle code
            DataSamples = CreateDataSamples();
            GridView1.DataSource =DataSamples ;
            GridView1.DataBind();

            //TODO:There are more ways to retreiving data from db
            // e.g SqlDataAdapter
            // SqlConnection cs = new SqlConnection("Data Source=.\\SQLEXPRESS; Initial Catalog=dbName; Integrated Security=TRUE");
            // SqlDataAdapter da = new SqlDataAdapter();
            // cs.Open();
            // da.SelectCommand = new SqlCommand("SELECT * FROM table", cs);
            // da.Fill(dg);
        }
        private List<sample> CreateDataSamples()
        {
            var list = new List<sample>();
            for (int i = 0; i < 10; i++)
                list.Add(Sample.CreateTestSample(i));
            
            return list;
        }

7.Define your AddNewRow Action
C#
protected void BtnAddNewRowClick(object sender, EventArgs e)
        {
            var list = CreateDataSamples();
            list.Add(new Sample());
            
            GridView1.EditIndex = list.Count - 1;
            GridView1.DataSource = DataSamples;
            GridView1.DataBind();
        }

8.Define your Insert Action
C#
protected void BtnInsertClick(object sender, EventArgs e)
        {
            var row = GridView1.Rows[GridView1.EditIndex];
            var sno = ((TextBox) (row.FindControl("txtSNO"))).Text;
            var name = ((TextBox) (row.FindControl("txtName"))).Text;
            var age = Convert.ToInt32(((TextBox) (row.FindControl("txtAge"))).Text);
            var address = ((TextBox) (row.FindControl("txtAddress"))).Text;

            var newSample = new Sample
                                {
                                    SNO = sno,
                                    Name = name,
                                    Age = age,
                                    Address = address
            
                                };
            //Save Data in Persistent Data
            //TODO you should write your own code for update DB
            if (GridView1.EditIndex > 0)
                DataSamples[GridView1.EditIndex] = newSample;

            GridView1.EditIndex = -1;
            GridView1.DataSource = DataSamples;
            GridView1.DataBind();

        }


please let me know if you need anymore details.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900