none
Bind TextBox Value to DataGrid

    Pertanyaan

  • Hi 

    I have 4 Text Box and 1 Button.. When i enter value in TextBox and click the button.

    My datas should bind in DataGrid View. And after inserting in Datagrid. When i double click the data row

    from DataGrid, the values should be again placed in textboxes so that i can edit data and update the same row. How to do it..

     

    Please Help Me....

    Thanks in advance....

     

    01 Desember 2011 13:20

Jawaban

  • Hi NastyFreak,

    Welcome to the MSDN Forum.

    Here is the sample for you:

    //Form1 for the Designer
    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.dataGridView1 = new System.Windows.Forms.DataGridView();
            this.label1 = new System.Windows.Forms.Label();
            this.txtId = new System.Windows.Forms.TextBox();
            this.txtAge = new System.Windows.Forms.TextBox();
            this.label2 = new System.Windows.Forms.Label();
            this.txtName = new System.Windows.Forms.TextBox();
            this.label3 = new System.Windows.Forms.Label();
            this.txtAddress = new System.Windows.Forms.TextBox();
            this.label4 = new System.Windows.Forms.Label();
            this.btnSubmit = new System.Windows.Forms.Button();
            ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
            this.SuspendLayout();
            // 
            // dataGridView1
            // 
            this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.dataGridView1.Location = new System.Drawing.Point(12, 12);
            this.dataGridView1.MultiSelect = false;
            this.dataGridView1.Name = "dataGridView1";
            this.dataGridView1.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;
            this.dataGridView1.Size = new System.Drawing.Size(578, 267);
            this.dataGridView1.TabIndex = 0;
            this.dataGridView1.DoubleClick += new System.EventHandler(this.dataGridView1_DoubleClick);
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(21, 291);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(19, 13);
            this.label1.TabIndex = 1;
            this.label1.Text = "Id:";
            // 
            // txtId
            // 
            this.txtId.Location = new System.Drawing.Point(46, 288);
            this.txtId.Name = "txtId";
            this.txtId.Size = new System.Drawing.Size(160, 20);
            this.txtId.TabIndex = 2;
            // 
            // txtAge
            // 
            this.txtAge.Location = new System.Drawing.Point(46, 314);
            this.txtAge.Name = "txtAge";
            this.txtAge.Size = new System.Drawing.Size(160, 20);
            this.txtAge.TabIndex = 4;
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(21, 317);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(29, 13);
            this.label2.TabIndex = 3;
            this.label2.Text = "Age:";
            // 
            // txtName
            // 
            this.txtName.Location = new System.Drawing.Point(284, 288);
            this.txtName.Name = "txtName";
            this.txtName.Size = new System.Drawing.Size(160, 20);
            this.txtName.TabIndex = 6;
            // 
            // label3
            // 
            this.label3.AutoSize = true;
            this.label3.Location = new System.Drawing.Point(240, 291);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(38, 13);
            this.label3.TabIndex = 5;
            this.label3.Text = "Name:";
            // 
            // txtAddress
            // 
            this.txtAddress.Location = new System.Drawing.Point(284, 314);
            this.txtAddress.Name = "txtAddress";
            this.txtAddress.Size = new System.Drawing.Size(160, 20);
            this.txtAddress.TabIndex = 8;
            // 
            // label4
            // 
            this.label4.AutoSize = true;
            this.label4.Location = new System.Drawing.Point(240, 317);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(48, 13);
            this.label4.TabIndex = 7;
            this.label4.Text = "Address:";
            // 
            // btnSubmit
            // 
            this.btnSubmit.Location = new System.Drawing.Point(515, 291);
            this.btnSubmit.Name = "btnSubmit";
            this.btnSubmit.Size = new System.Drawing.Size(75, 39);
            this.btnSubmit.TabIndex = 9;
            this.btnSubmit.Text = "Submit";
            this.btnSubmit.UseVisualStyleBackColor = true;
            this.btnSubmit.Click += new System.EventHandler(this.btnSubmit_Click);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(602, 349);
            this.Controls.Add(this.btnSubmit);
            this.Controls.Add(this.txtAddress);
            this.Controls.Add(this.label4);
            this.Controls.Add(this.txtName);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.txtAge);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.txtId);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.dataGridView1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.Load += new System.EventHandler(this.Form1_Load);
            ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();
    
        }
    
        #endregion
    
        private System.Windows.Forms.DataGridView dataGridView1;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.TextBox txtId;
        private System.Windows.Forms.TextBox txtAge;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.TextBox txtName;
        private System.Windows.Forms.Label label3;
        private System.Windows.Forms.TextBox txtAddress;
        private System.Windows.Forms.Label label4;
        private System.Windows.Forms.Button btnSubmit;
    
    }
    

    //the programming code:
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
    
        private void btnSubmit_Click(object sender, EventArgs e)
        {
            if (CheckFiledIsEmpty())
            {
                if (dt.Select("Id=" + Convert.ToInt32(txtId.Text)).Length <= 0)
                {
    
                    DataRow dr = dt.NewRow();
                    dr["Id"] = Convert.ToInt32(txtId.Text);
                    dr["Name"] = txtName.Text;
                    dr["Age"] = Convert.ToInt32(txtAge.Text);
                    dr["Address"] = txtAddress.Text;
                    dt.Rows.Add(dr);
                }
                else
                {
                    if (dgvr != null)
                    {
                        dgvr.Cells["Id"].Value = Convert.ToInt32(txtId.Text);
                        dgvr.Cells["Name"].Value = txtName.Text;
                        dgvr.Cells["Age"].Value = Convert.ToInt32(txtAge.Text);
                        dgvr.Cells["Address"].Value = txtAddress.Text;
                    }
                }
            }
    
        }
    
        bool CheckFiledIsEmpty()
        {
            if (txtId.Text == string.Empty || 
                txtAge.Text == string.Empty || 
                txtAddress.Text == string.Empty || 
                txtName.Text == string.Empty)
                return false;
            else
                return true;
        }
    
        DataTable dt;
    
        private void Form1_Load(object sender, EventArgs e)
        {
            dt = new DataTable();
            dt.Columns.Add("Id", Type.GetType("System.Int32"));
            dt.Columns.Add("Name", Type.GetType("System.String"));
            dt.Columns.Add("Age", Type.GetType("System.Int32"));
            dt.Columns.Add("Address", Type.GetType("System.String"));
            dt.AcceptChanges();
            this.dataGridView1.DataSource = dt;
        }
    
        DataGridViewRow dgvr;
    
        private void dataGridView1_DoubleClick(object sender, EventArgs e)
        {
            dgvr = this.dataGridView1.SelectedRows[0];
            this.txtId.Text = dgvr.Cells["Id"].Value.ToString();
            this.txtName.Text = dgvr.Cells["Name"].Value.ToString();
            this.txtAge.Text = dgvr.Cells["age"].Value.ToString();
            this.txtAddress.Text = dgvr.Cells["Address"].Value.ToString();
    
        }
    }
    

    Here is the screenshot:

    Best Regards


    Neddy Ren[MSFT]
    MSDN Community Support | Feedback to us
    • Disarankan sebagai Jawaban oleh Hardz T L 05 Desember 2011 5:13
    • Ditandai sebagai Jawaban oleh Neddy Ren 07 Desember 2011 6:32
    05 Desember 2011 3:45

Semua Balasan

  • Hi 

    I have 4 Text Box and 1 Button.. When i enter value in TextBox and click the button.

    My datas should bind in DataGrid View. And after inserting in Datagrid. When i double click the data row

    from DataGrid, the values should be again placed in textboxes so that i can edit data and update the same row. How to do it..

     

    Please Help Me....

    Thanks in advance....

    • Digabungkan oleh Neddy Ren 05 Desember 2011 3:47 duplicated
    01 Desember 2011 13:22
  • Hi NastyFreak,

    Welcome to the MSDN Forum.

    Here is the sample for you:

    //Form1 for the Designer
    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.dataGridView1 = new System.Windows.Forms.DataGridView();
            this.label1 = new System.Windows.Forms.Label();
            this.txtId = new System.Windows.Forms.TextBox();
            this.txtAge = new System.Windows.Forms.TextBox();
            this.label2 = new System.Windows.Forms.Label();
            this.txtName = new System.Windows.Forms.TextBox();
            this.label3 = new System.Windows.Forms.Label();
            this.txtAddress = new System.Windows.Forms.TextBox();
            this.label4 = new System.Windows.Forms.Label();
            this.btnSubmit = new System.Windows.Forms.Button();
            ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
            this.SuspendLayout();
            // 
            // dataGridView1
            // 
            this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.dataGridView1.Location = new System.Drawing.Point(12, 12);
            this.dataGridView1.MultiSelect = false;
            this.dataGridView1.Name = "dataGridView1";
            this.dataGridView1.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;
            this.dataGridView1.Size = new System.Drawing.Size(578, 267);
            this.dataGridView1.TabIndex = 0;
            this.dataGridView1.DoubleClick += new System.EventHandler(this.dataGridView1_DoubleClick);
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(21, 291);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(19, 13);
            this.label1.TabIndex = 1;
            this.label1.Text = "Id:";
            // 
            // txtId
            // 
            this.txtId.Location = new System.Drawing.Point(46, 288);
            this.txtId.Name = "txtId";
            this.txtId.Size = new System.Drawing.Size(160, 20);
            this.txtId.TabIndex = 2;
            // 
            // txtAge
            // 
            this.txtAge.Location = new System.Drawing.Point(46, 314);
            this.txtAge.Name = "txtAge";
            this.txtAge.Size = new System.Drawing.Size(160, 20);
            this.txtAge.TabIndex = 4;
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(21, 317);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(29, 13);
            this.label2.TabIndex = 3;
            this.label2.Text = "Age:";
            // 
            // txtName
            // 
            this.txtName.Location = new System.Drawing.Point(284, 288);
            this.txtName.Name = "txtName";
            this.txtName.Size = new System.Drawing.Size(160, 20);
            this.txtName.TabIndex = 6;
            // 
            // label3
            // 
            this.label3.AutoSize = true;
            this.label3.Location = new System.Drawing.Point(240, 291);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(38, 13);
            this.label3.TabIndex = 5;
            this.label3.Text = "Name:";
            // 
            // txtAddress
            // 
            this.txtAddress.Location = new System.Drawing.Point(284, 314);
            this.txtAddress.Name = "txtAddress";
            this.txtAddress.Size = new System.Drawing.Size(160, 20);
            this.txtAddress.TabIndex = 8;
            // 
            // label4
            // 
            this.label4.AutoSize = true;
            this.label4.Location = new System.Drawing.Point(240, 317);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(48, 13);
            this.label4.TabIndex = 7;
            this.label4.Text = "Address:";
            // 
            // btnSubmit
            // 
            this.btnSubmit.Location = new System.Drawing.Point(515, 291);
            this.btnSubmit.Name = "btnSubmit";
            this.btnSubmit.Size = new System.Drawing.Size(75, 39);
            this.btnSubmit.TabIndex = 9;
            this.btnSubmit.Text = "Submit";
            this.btnSubmit.UseVisualStyleBackColor = true;
            this.btnSubmit.Click += new System.EventHandler(this.btnSubmit_Click);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(602, 349);
            this.Controls.Add(this.btnSubmit);
            this.Controls.Add(this.txtAddress);
            this.Controls.Add(this.label4);
            this.Controls.Add(this.txtName);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.txtAge);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.txtId);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.dataGridView1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.Load += new System.EventHandler(this.Form1_Load);
            ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();
    
        }
    
        #endregion
    
        private System.Windows.Forms.DataGridView dataGridView1;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.TextBox txtId;
        private System.Windows.Forms.TextBox txtAge;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.TextBox txtName;
        private System.Windows.Forms.Label label3;
        private System.Windows.Forms.TextBox txtAddress;
        private System.Windows.Forms.Label label4;
        private System.Windows.Forms.Button btnSubmit;
    
    }
    

    //the programming code:
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
    
        private void btnSubmit_Click(object sender, EventArgs e)
        {
            if (CheckFiledIsEmpty())
            {
                if (dt.Select("Id=" + Convert.ToInt32(txtId.Text)).Length <= 0)
                {
    
                    DataRow dr = dt.NewRow();
                    dr["Id"] = Convert.ToInt32(txtId.Text);
                    dr["Name"] = txtName.Text;
                    dr["Age"] = Convert.ToInt32(txtAge.Text);
                    dr["Address"] = txtAddress.Text;
                    dt.Rows.Add(dr);
                }
                else
                {
                    if (dgvr != null)
                    {
                        dgvr.Cells["Id"].Value = Convert.ToInt32(txtId.Text);
                        dgvr.Cells["Name"].Value = txtName.Text;
                        dgvr.Cells["Age"].Value = Convert.ToInt32(txtAge.Text);
                        dgvr.Cells["Address"].Value = txtAddress.Text;
                    }
                }
            }
    
        }
    
        bool CheckFiledIsEmpty()
        {
            if (txtId.Text == string.Empty || 
                txtAge.Text == string.Empty || 
                txtAddress.Text == string.Empty || 
                txtName.Text == string.Empty)
                return false;
            else
                return true;
        }
    
        DataTable dt;
    
        private void Form1_Load(object sender, EventArgs e)
        {
            dt = new DataTable();
            dt.Columns.Add("Id", Type.GetType("System.Int32"));
            dt.Columns.Add("Name", Type.GetType("System.String"));
            dt.Columns.Add("Age", Type.GetType("System.Int32"));
            dt.Columns.Add("Address", Type.GetType("System.String"));
            dt.AcceptChanges();
            this.dataGridView1.DataSource = dt;
        }
    
        DataGridViewRow dgvr;
    
        private void dataGridView1_DoubleClick(object sender, EventArgs e)
        {
            dgvr = this.dataGridView1.SelectedRows[0];
            this.txtId.Text = dgvr.Cells["Id"].Value.ToString();
            this.txtName.Text = dgvr.Cells["Name"].Value.ToString();
            this.txtAge.Text = dgvr.Cells["age"].Value.ToString();
            this.txtAddress.Text = dgvr.Cells["Address"].Value.ToString();
    
        }
    }
    

    Here is the screenshot:

    Best Regards


    Neddy Ren[MSFT]
    MSDN Community Support | Feedback to us
    • Disarankan sebagai Jawaban oleh Hardz T L 05 Desember 2011 5:13
    • Ditandai sebagai Jawaban oleh Neddy Ren 07 Desember 2011 6:32
    05 Desember 2011 3:45