i want to add multiple rows in datagridview from textbox values then save the rows into database?
how its possible...???

Which part is giving you a problem?

my record is overvide in grid view .....only 1 record inserted at a time.......

So the DataGridView is not adding rows?
What code are you using to update the DataGridView?

yes gridview is not working .....how to add multiple record in gridview from textfield..?

That will really depend on how you have your text data arranged.
If you just have a single text box with values in it, and you want to convert that to a DataGridView (with a button push), you could do something like this:

private void button1_Click(object sender, EventArgs e)
         dataGridView1.DataSource =
            from arr_str in textBox1.Text.ToString().Split("\r\n".ToCharArray(), StringSplitOptions.RemoveEmptyEntries)
            select new
               Converted = arr_str.Trim()

this time grid view is working and record are inserted into database but only 1 record are inserted in database if i inserted 2nd record then 1st record are override in grid view and also inserted 2nd record in database.......

i have two text fields and one comboBox and one button for insert data in gridview.

my project is billing system in which customer can buy many things .

product name comboxbox

price text field

quantity text field

insert button

grid view

i want to add multiple products in gridview from combobox and textfield but only 1 record are show in grid view if i insert 2nd record then first record is override in gridview..

Are you looping through the values in the text box?

Are you looping through the values in the text box?


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using System.Data.SqlClient;

namespace Semester_Proj
    public partial class Billing : Form
        public Billing()
        private SqlDataAdapter da;
       // private SqlCommand cmd;
        private DataSet ds;

        private void exit_Click(object sender, EventArgs e)


        private void Home_Click(object sender, EventArgs e)
            welcome we = new welcome();

        private void insert()
            string name = comboBox1.Text.Trim();
            string price = pbtn.Text.Trim();
            string quantity = qbtn.Text.Trim();

                String connectionString = ConfigurationManager.ConnectionStrings["con"].ConnectionString;
                SqlConnection con = new SqlConnection(connectionString);

                string query = "select * from Bill";
                ds = new DataSet();
                da = new SqlDataAdapter(query, con);

                SqlCommandBuilder builder = new SqlCommandBuilder(da);

                DataRow row = ds.Tables[0].NewRow();
                row["name"] = comboBox1.Text.ToString();
                row["price"] = pbtn.Text.ToString();

                row["quantity"] = qbtn.Text.ToString();

                int idx = dataGridView1.Rows.GetLastRow(DataGridViewElementStates.Visible); dataGridView1.Rows[idx].Selected = true;
            //    dataGridView1.DataSource = ds.Tables[0];
                MessageBox.Show("Record is Inserted!!!!");
            catch (Exception )

                comboBox1.SelectedIndex = -1;
                // comboBox1.Items.Clear();


        private void Billing_Load(object sender, EventArgs e)

              string str = ConfigurationManager.ConnectionStrings["con"].ConnectionString;
                SqlConnection cnn = new SqlConnection(str);

                string query = "select Product_Name from Products";
                SqlCommand cmd = new SqlCommand(query, cnn);
                SqlDataReader rd = cmd.ExecuteReader();

                while (rd.Read())




        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)


        private void bsubmit_Click(object sender, EventArgs e)

            DataTable dt = new DataTable();

          //  dt.Columns.Add("total");

            string name =comboBox1 .Text.Trim();
            string price = pbtn.Text.Trim();
            string quantity = qbtn.Text.Trim();

            DataRow dr = dt.NewRow();
            dr["Name"] = name;
            dr["price"] = price;
            dr["quantity"] = quantity;
           //dr["total"] = TextBox4.Text;

           dataGridView1.DataSource = dt;



        private void label2_Click(object sender, EventArgs e)



Do you need to re "new" your SqlCommandBuilder after the Fill()?

I don't use DataSet and it's associated classes.
Is it possible for you to simplify this by eliminating the use of the data rows, tables and adapters and just issue the SQL directly for the insert?

[after you fix the problem...]
The data is pulled out of the controls twice -- once for the DataGridView and then again for the DataRow in the insert() method. Is it possible to make one function that does the select, one that does the insert, one that does the update but where NONE involve the display?
That way you can just pass the data to and from the functions without having to deal with the visual at the same time.

Once the data is processed, you can update the display.

thanks alot my problem is solved.....

hey guys ,can u suggest me a code for this sutaion.
in my project ,i want to add a data in gridview at run time and that have to store in my data base and my projects is a window base application.
i know about insert a data from database into gridview but not know anything about this .....
plz help me and suggest me a code for this...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.