How to Add a Textbox Value to Gridview in C#

Introduction

 
In this tutorial, you will learn how to bind data from textbox to gridview in C#, in detail. Displaying data with a tabular format is a task you are likely to perform frequently. The DataGridView  is designed to be a complete solution for displaying data with tabular format in Windows Forms. The DataGridView is very configurable and extensible, and it provides many facets such as properties, methods, and events to customize appearance and behavior.
 
The DataGridView makes it easy use to define the basic appearance of cells and the display formatting of cell values. The cell of DataGridView is the fundamental unit of interaction for the DataGridView. All cells derive from the DataGridViewCell based class. Each cell within the DataGridView can have its own style, such as foreground color, background color, text format, and font. Typically, multiples cells will share particular style characteristics. By default, the data types in the cells value property by the type Object.
 
The following chapters will explain the basics of DataGridView  and walk through an example that shows how to build a simple C# application.
 
Let’s follow the steps to learn more about adding a textbox to Datagridview,
  1. Create a new application project. In Visual Studio, on the menu click File> New  > Project. For more details, see the following menu on the display.

  1. Then the window New Project will appear, as shown below.

  1. Write down the name of the project that will be created as a field Name. Specify the directory storage project by accessing the field Location. Next, give the name of the solution in the Solution Name. Then click OK. For example, you can name the project "add_textboxt_to_datagridvew"

  1. Create textbox properties with a label in the windows form, like shown below.
  2. Create Datagridview with properties like shown below.

Next, go back to windows form and view code to write the following program listing:
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Windows.Forms;  
  9.   
  10. namespace Add_textbox_to_Datagridvew  
  11. {  
  12.     public partial class Form1 : Form  
  13.     {  
  14.         public Form1()  
  15.         {  
  16.             InitializeComponent();  
  17.         }  
  18.   
  19.         private void button1_Click(object sender, EventArgs e)  
  20.         {  
  21.             History_addGrid(textBox1.Text, textBox2.Text, textBox3.Text, textBox4.Text);  
  22.         }  
  23.   
  24.         private void History_addGrid(string name, string age, string telephone, string address)  
  25.         {  
  26.             try  
  27.             {  
  28.                 int no = 0;  
  29.                 DataGridViewRow newRow = new DataGridViewRow();  
  30.                 newRow.CreateCells(dataGridView1);  
  31.                 newRow.Cells[0].Value = no + 1;  
  32.                 newRow.Cells[1].Value = name;  
  33.                 newRow.Cells[2].Value = age;  
  34.                 newRow.Cells[3].Value = telephone;  
  35.                 newRow.Cells[4].Value = address;  
  36.                 dataGridView1.Rows.Add(newRow);  
  37.             }  
  38.             catch { }  
  39.         }  
  40.   
  41.         private void button2_Click(object sender, EventArgs e)  
  42.         {  
  43.             dataGridView1.Rows.Clear();  
  44.             textBox1.Text = "";  
  45.             textBox2.Text = "";  
  46.             textBox3.Text = "";  
  47.             textBox4.Text = "";  
  48.         }  
  49.     }  


After you write down the program listings, press the F5 key to run the program.

 
We have explained how to make a program add data from a textbox to gridview C#. For those of you who want to download the source code of the program, you also can. Hopefully this discussion is helpful to you.
 
You can see the Add Textbox Value to Gridview C# Github project here.
 
Thank you for reading this article about Adding a Textbox Value to Gridview C#, I hope this article was useful for you. Visit My GitHub about .NET C# Here


Similar Articles