Click here to Skip to main content
15,895,142 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello community

I would like to know how I can make a preview in a datagrid taking data from a textbox then be inserted to the database?

thanks very much
Posted
Updated 21-Jan-14 5:59am
v2
Comments
njammy 21-Jan-14 11:49am    
Please use the improve question function to add more detail to your question like what data you want to enter, how you wish to preview it, and what exactly you want to preview.
Salomon Pineda Silva 21-Jan-14 15:50pm    
filled all textbox or combobox or any object that contains data. having all objects with the data, press a button to display a datagrid and then trigger the query, in this case the insert, to be saved in the database. Normally the data is displayed in the datagridview and inserted into the database

fill textbox -> select option "show on datagrid"-> fill textbox -> select option show on datagrid-> data are shown-in datagridview -> select option "save data from datagridview to the database." so the process would be like -> select option "save data from datagridview to the database." so the process would be like
Krunal Rohit 21-Jan-14 12:00pm    
elaborate your question.

 
Share this answer
 
Use this sample:
Create a new form called Form1 (or as you wish)

Use this Form1.designer.cs code: (it should look like the screen shot here: http://tinypic.com/r/m8marn/5[^]
C#
partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.button1 = new System.Windows.Forms.Button();
            this.dataGridView1 = new System.Windows.Forms.DataGridView();
            this.field1 = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.button2 = new System.Windows.Forms.Button();
            ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
            this.SuspendLayout();
            // 
            // textBox1
            // 
            this.textBox1.Location = new System.Drawing.Point(47, 12);
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(100, 20);
            this.textBox1.TabIndex = 0;
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(154, 12);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(75, 23);
            this.button1.TabIndex = 1;
            this.button1.Text = "button1";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // dataGridView1
            // 
            this.dataGridView1.AllowUserToResizeColumns = false;
            this.dataGridView1.AllowUserToResizeRows = false;
            this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
            this.field1});
            this.dataGridView1.Location = new System.Drawing.Point(47, 39);
            this.dataGridView1.MultiSelect = false;
            this.dataGridView1.Name = "dataGridView1";
            this.dataGridView1.Size = new System.Drawing.Size(235, 280);
            this.dataGridView1.TabIndex = 2;
            // 
            // field1
            // 
            this.field1.HeaderText = "Column1";
            this.field1.Name = "field1";
            // 
            // button2
            // 
            this.button2.Location = new System.Drawing.Point(289, 39);
            this.button2.Name = "button2";
            this.button2.Size = new System.Drawing.Size(75, 23);
            this.button2.TabIndex = 3;
            this.button2.Text = "Delete";
            this.button2.UseVisualStyleBackColor = true;
            this.button2.Click += new System.EventHandler(this.button2_Click);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(583, 331);
            this.Controls.Add(this.button2);
            this.Controls.Add(this.dataGridView1);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.textBox1);
            this.Name = "Form1";
            this.Text = "Form1";
            ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.TextBox textBox1;
        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.DataGridView dataGridView1;
        private System.Windows.Forms.DataGridViewTextBoxColumn field1;
        private System.Windows.Forms.Button button2;
    }


Form1.cs
C#
public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        /// &lt;summary&gt;
        /// Add row
        /// &lt;/summary&gt;
        /// &lt;param name=&quot;sender&quot;&gt;&lt;/param&gt;
        /// &lt;param name=&quot;e&quot;&gt;&lt;/param&gt;
        private void button1_Click(object sender, EventArgs e)
        {
            string rowValue = textBox1.Text;
            dataGridView1.Rows.Add(new object[] { rowValue });
        }

        /// &lt;summary&gt;
        /// Delete row
        /// &lt;/summary&gt;
        /// &lt;param name=&quot;sender&quot;&gt;&lt;/param&gt;
        /// &lt;param name=&quot;e&quot;&gt;&lt;/param&gt;
        private void button2_Click(object sender, EventArgs e)
        {
            DataGridViewRow row = dataGridView1.SelectedRows[0];
            dataGridView1.Rows.Remove(row);
        }
    }


Then just add your button to run the database insert query. Look at how the delete button works to get the rows from gridview, in this same way you can get data from gridview by rows and do insert.
 
Share this answer
 
and how would you take the data from the cell to insert to the database?
greetings
 
Share this answer
 

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