Hello,
This is my solution for your problem. It is built as windows form application
in IDE SharpDevelop C# 4.0 and needs netframework 4.0.
It consists of one form with one DataGridView with four columns as you requested,
four text boxes and one button to be clicked when you want to transfer data from text boxes to DataGridView.
At the begining of program there is call for setting initial values of DataGridView an text boxes.Look At the code below.
It has only one event handler and it is EnterValuesToDatagridView, that executes when you click button ( initialy it was Button1Click , but I like this name ).
This solution do not solve case , what if you need to change entered data to grid view. Perhaps you need another event , like user select row heder and transfer data back to text boxes for change...
Program.cs*
using System;
using System.Windows.Forms;
namespace Insert_value_from_TextBox__to__DataGridView
{
internal sealed class Program
{
[STAThread]
private static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
}
}
}
MainForm.cs*
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
namespace Insert_value_from_TextBox__to__DataGridView
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
SetInitialValues();
}
void EnterValuesToDatagridView(object sender, EventArgs e)
{
int Row = 0;
if (textBox1.Text != "" & textBox2.Text != "" &
textBox3.Text != "" & textBox4.Text != "")
{
dataGridView1.Rows.Add();
Row = dataGridView1.Rows.Count - 2;
dataGridView1[0,Row].Value = textBox1.Text;
dataGridView1[1,Row].Value = textBox2.Text;
dataGridView1[2,Row].Value = textBox3.Text;
dataGridView1[3,Row].Value = textBox4.Text;
dataGridView1.Refresh();
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
textBox4.Text = "";
}
else
{
MessageBox.Show("You did not entered values to all text boxes",
"Error", MessageBoxButtons.OK,MessageBoxIcon.Error);
}
}
void SetInitialValues()
{
dataGridView1.ReadOnly = true;
dataGridView1.AllowUserToDeleteRows = true;
dataGridView1.RowCount = 1;
dataGridView1.Refresh();
textBox1.Text = "Hemant";
textBox2.Text = "IT";
textBox3.Text = "25";
textBox4.Text = "10 rupee";
}
}
}
MainFor.Designer.cs*
namespace Insert_value_from_TextBox__to__DataGridView
{
partial class MainForm
{
private System.ComponentModel.IContainer components = null;
protected override void Dispose(bool disposing)
{
if (disposing) {
if (components != null) {
components.Dispose();
}
}
base.Dispose(disposing);
}
private void InitializeComponent()
{
System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle1 = new System.Windows.Forms.DataGridViewCellStyle();
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.textBox3 = new System.Windows.Forms.TextBox();
this.textBox4 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.dataGridView1 = new System.Windows.Forms.DataGridView();
this.Column1 = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.Column2 = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.Column3 = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.Column4 = new System.Windows.Forms.DataGridViewTextBoxColumn();
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
this.SuspendLayout();
this.textBox1.Location = new System.Drawing.Point(49, 357);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(202, 26);
this.textBox1.TabIndex = 0;
this.textBox2.Location = new System.Drawing.Point(257, 357);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(134, 26);
this.textBox2.TabIndex = 1;
this.textBox3.Location = new System.Drawing.Point(397, 357);
this.textBox3.Name = "textBox3";
this.textBox3.Size = new System.Drawing.Size(51, 26);
this.textBox3.TabIndex = 2;
this.textBox4.Location = new System.Drawing.Point(454, 357);
this.textBox4.Name = "textBox4";
this.textBox4.Size = new System.Drawing.Size(138, 26);
this.textBox4.TabIndex = 3;
this.button1.Location = new System.Drawing.Point(49, 400);
this.button1.Margin = new System.Windows.Forms.Padding(2);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(543, 31);
this.button1.TabIndex = 4;
this.button1.Text = "Enter values to grid";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.EnterValuesToDatagridView);
this.dataGridView1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.dataGridView1.CellBorderStyle = System.Windows.Forms.DataGridViewCellBorderStyle.Sunken;
dataGridViewCellStyle1.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter;
dataGridViewCellStyle1.BackColor = System.Drawing.SystemColors.Control;
dataGridViewCellStyle1.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(204)));
dataGridViewCellStyle1.ForeColor = System.Drawing.SystemColors.WindowText;
dataGridViewCellStyle1.SelectionBackColor = System.Drawing.SystemColors.Highlight;
dataGridViewCellStyle1.SelectionForeColor = System.Drawing.SystemColors.HighlightText;
dataGridViewCellStyle1.WrapMode = System.Windows.Forms.DataGridViewTriState.True;
this.dataGridView1.ColumnHeadersDefaultCellStyle = dataGridViewCellStyle1;
this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
this.Column1,
this.Column2,
this.Column3,
this.Column4});
this.dataGridView1.Location = new System.Drawing.Point(8, 8);
this.dataGridView1.Margin = new System.Windows.Forms.Padding(2);
this.dataGridView1.Name = "dataGridView1";
this.dataGridView1.Size = new System.Drawing.Size(613, 328);
this.dataGridView1.TabIndex = 5;
this.Column1.HeaderText = "Name";
this.Column1.Name = "Column1";
this.Column1.Width = 200;
this.Column2.HeaderText = "Department";
this.Column2.Name = "Column2";
this.Column2.Width = 150;
this.Column3.HeaderText = "Age";
this.Column3.Name = "Column3";
this.Column3.Width = 50;
this.Column4.HeaderText = "Salary";
this.Column4.Name = "Column4";
this.Column4.Width = 150;
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
this.ClientSize = new System.Drawing.Size(632, 442);
this.Controls.Add(this.dataGridView1);
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox4);
this.Controls.Add(this.textBox3);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox1);
this.DoubleBuffered = true;
this.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(204)));
this.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5);
this.Name = "MainForm";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = " Insert value from TextBox to DataGridView";
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();
}
private System.Windows.Forms.DataGridViewTextBoxColumn Column4;
private System.Windows.Forms.DataGridViewTextBoxColumn Column3;
private System.Windows.Forms.DataGridViewTextBoxColumn Column2;
private System.Windows.Forms.DataGridViewTextBoxColumn Column1;
private System.Windows.Forms.DataGridView dataGridView1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox textBox4;
private System.Windows.Forms.TextBox textBox3;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.TextBox textBox1;
}
}
All the best,
Perić Željko
get value from selected rows in datagridview[
^]