Adding textbox values to all the datagridview rows c# code

DataGridView control in Windows forms:
             DataGridView control is the windows forms tool as like  GridView in the ASP web designing.To add the values from the textbox to datagridview first you have to add the datagridview columns statically.For the go to properties at the bottom of the propertie window you will see Edit columns property. By clicking that property you will get a window. there you can add , remove and set the special properties of those columns. The DataGridView control makes it easy to define the appearance of cells and the display formatting of cell . The cell is the fundamental  unit of interaction for the DataGridView. We can add the different types of controls in each cell as combobox, link, image etc. All cells derive
from the DataGridViewCell base class. Each cell  within the DataGridView control can have its own style, such as text format, background color, foreground color, and font. Typically, however, multiple cells will share particular style characteristics. The data type for the cell's Value property by default is of type Object.




Adding from textbox to all the datagridview rows
The code shown bellow will work for your solution.

Event : Write this code in Add button_Click event.

            int row = 0;
            dataGridView1.Rows.Add();
            row = dataGridView1.Rows.Count - 2;
            dataGridView1["Column1", row].Value = textBox1.Text;
            dataGridView1["Column2", row].Value = textBox2.Text;
            dataGridView1["Column3", row].Value = textBox3.Text;
            dataGridView1["Column4", row].Value = textBox4.Text;
            dataGridView1["Column5", row].Value = textBox5.Text;
            dataGridView1.Refresh();

I hope this code will help you.

7 comments: