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

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

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()
            }
         ).ToList();
      }

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..

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()
        {
            InitializeComponent();
        }
        private SqlDataAdapter da;
       // private SqlCommand cmd;
        private DataSet ds;

        private void exit_Click(object sender, EventArgs e)
        {
            Application.Exit();

        }

        private void Home_Click(object sender, EventArgs e)
        {
            welcome we = new welcome();
            we.Show();
            this.Close();
        }

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





            try
            {
                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);
                da.Fill(ds);


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

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


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


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




        }



        private void Billing_Load(object sender, EventArgs e)
        {


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

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

                while (rd.Read())
                {

                    comboBox1.Items.Add(rd["Product_Name"]).ToString();

                }
                rd.Close();
                cnn.Close();


        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void bsubmit_Click(object sender, EventArgs e)
        {

            DataTable dt = new DataTable();

            dt.Columns.Add("Name");
            dt.Columns.Add("price");
            dt.Columns.Add("quantity");
          //  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;
            dt.Rows.Add(dr);

           dataGridView1.DataSource = dt;

           insert();

        }



        private void label2_Click(object sender, EventArgs e)
        {

        }




    }
}

Edited 3 Years Ago by Dani: Formatting fixed

Do you need to re "new" your SqlCommandBuilder after the Fill()?
http://support.microsoft.com/kb/307587

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.

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...

This question has already been answered. Start a new discussion instead.