如何在插入数据库之前将数据从文本框显示到数据网格? [英] how display data from textbox to datagrid before insert to data base?

查看:100
本文介绍了如何在插入数据库之前将数据从文本框显示到数据网格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好社区



我想知道如何在数据网格中预览从文本框中获取数据然后插入数据库?



非常感谢

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

推荐答案

创建新 datatable对象 [ ^ ]并将此数据表绑定到datagrid。



其他资源:

如何使用c#.net 将DataTable绑定到asp.net中的Gridview [ ^ ]

动态创建DataTable并绑定到ASP.Net中的GridView [ ^ ]
Create new datatable object[^] and bind this datatable to datagrid.

Other resources:
How to Bind the DataTable to Gridview in asp.net using c#.net[^]
Dynamically create DataTable and bind to GridView in ASP.Net[^]


使用此示例:

创建一个名为Form1的新表单(或者您希望的话)



使用此 Form1.designer.cs 代码:(它应该看起来像这里的屏幕截图: http://tinypic.com/r/m8marn/5 [ ^ ]

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[^]
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



Form1.cs

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);
        }
    }





然后只需添加按钮即可运行数据库插入查询。看看删除按钮如何从gridview获取行,同样地,你可以按行从gridview获取数据并插入。



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.


以及如何从中获取数据要插入数据库的单元格?

greetings
and how would you take the data from the cell to insert to the database?
greetings


这篇关于如何在插入数据库之前将数据从文本框显示到数据网格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆