Click here to Skip to main content
15,996,429 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hi!

i have 4 text boxes and a button text = add

when i click on the button add, i want to display the values in grid view which i have entered in text boxes, and save in database.
for eg like an invoice
Please Help

additional code copied from comment below
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    private void BindGrid(int rowcount)
    {
        DataTable dt = new DataTable();
        DataRow dr;
        dt.Columns.Add(new System.Data.DataColumn("FIRST NAME", typeof(String)));
        dt.Columns.Add(new System.Data.DataColumn("FATHER NAME", typeof(String)));
        dt.Columns.Add(new System.Data.DataColumn("LAST NAME", typeof(String)));
        dt.Columns.Add(new System.Data.DataColumn("AGE", typeof(String)));
        dt.Columns.Add(new System.Data.DataColumn("EMAIL", typeof(String)));
        dt.Columns.Add(new System.Data.DataColumn("TELEPHONE", typeof(String)));

        if (ViewState["CurrentData"] != null)
        {
            for (int i = 0; i < rowcount + 1; i++)
            {
                dt = (DataTable)ViewState["CurrentData"];
                if (dt.Rows.Count > 0)
                {
                    dr = dt.NewRow();
                    dr[0] = dt.Rows[0][0].ToString();

                }
            }
            dr = dt.NewRow();
            dr[0] = txtname.Text;
            dr[1] = txtfname.Text;
            dr[2] = txtlname.Text;
            dr[3] = txtage.Text;
            dr[4] = txtemail.Text;
            dr[5] = txttele.Text;

            dt.Rows.Add(dr);

        }
        else
        {
            dr = dt.NewRow();
            dr[0] = txtname.Text;
            dr[1] = txtfname.Text;
            dr[2] = txtlname.Text;
            dr[3] = txtage.Text;
            dr[4] = txtemail.Text;
            dr[5] = txttele.Text;

            dt.Rows.Add(dr);

        }

        // If ViewState has a data then use the value as the DataSource
        if (ViewState["CurrentData"] != null)
        {
            GridView1.DataSource = (DataTable)ViewState["CurrentData"];
            GridView1.DataBind();
        }
        else
        {
            // Bind GridView with the initial data assocaited in the DataTable
            GridView1.DataSource = dt;
            GridView1.DataBind();

        }
        // Store the DataTable in ViewState to retain the values
        ViewState["CurrentData"] = dt;

    }

    protected void btnadd_Click(object sender, EventArgs e)
    {
        // Check if the ViewState has a data assoiciated within it. If
        if (ViewState["CurrentData"] != null)
        {
            DataTable dt = (DataTable)ViewState["CurrentData"];
            int count = dt.Rows.Count;
            BindGrid(count);
        }
        else
        {
            BindGrid(1);
        }
        txtname.Text = string.Empty;
        txtfname.Text = string.Empty;
        txtlname.Text = string.Empty;
        txtage.Text = string.Empty;
        txtemail.Text = string.Empty;
        txttele.Text = string.Empty;

        txtname.Focus();
        txtfname.Focus();
        txtlname.Focus();
        txtage.Focus();
        txtemail.Focus();
        txttele.Focus();
        
    }
}
Posted
Updated 15-Apr-14 22:03pm
v2
Comments
[no name] 14-Apr-14 7:08am    
Show us your code.
Member 10489888 26-Apr-14 4:04am    
can some body help me
Member 10489888 26-Apr-14 4:00am    
now i want to save those data in DB.
Plese Kindly Help me

On the button click provide the binding between Code and Database and on page load provide the binding between Grid and database so as the page will load on button click new data will be immediately seen.
 
Share this answer
 
Comments
Member 10489888 17-Apr-14 4:22am    
hello!
i am trying to do is i have 4 text boxes and two button's 1) ADD and 2) SAVE

when i enter values in text boxes and click on the button ADD i want to display those values in grid and when I add new values in text boxes and again click on ADD button i want the previous record to be shown in the grid,these goes on until i click on SAVE button.

when i click on SAVE i want all those data which is in grid to be saved in DB!

please help me
See create method to save data in database. This is just for concept
void SavaData()
{
//Save your texboxes values to database
//Call the bind gridview method to display in list or grid
BindDatainGridview();
}

void BindDatainGridview()
{
//Get list of values 
//Bind to gridview
}
 
Share this answer
 
You can use ViewState for this. Update the ViewState and bind that to GridView.

Example - Dynamically adding and deleting rows from ASP.NET GridView[^]
 
Share this answer
 
You can call this way a brute force method but if I understood what you are trying to do then it will be perfect for you.


C#
object[] s = new object[4];
s[0] = textBox1.Text;
s[1] = textBox2.Text;
s[2] = textBox3.Text;
s[3] = textBox4.Text;
dataGridView1.Rows.Add(s);

 
Share this answer
 
Comments
Member 10489888 17-Apr-14 4:26am    
hello!
i am trying to do is i have 4 text boxes and two button's 1) ADD and 2) SAVE

when i enter values in text boxes and click on the button ADD i want to display those values in grid and when I add new values in text boxes and again click on ADD button i want the previous record to be shown in the grid,these goes on until i click on SAVE button.

when i click on SAVE i want all those data which is in grid to be saved in DB!

please help me

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