C# - How To Add Delete And Update DataGridView Row Using TextBoxes In C#

C# - How To Add Delete And Update Row From DataGridView Using TextBoxes In C#

                                                                                                                                                     

In This C# Tutorial We Will See:
How To Add A Row To DataGridView From TextBoxes
How To How To Remove A Row From DataGridView 
How To UpdateDataGridView Row With TextBoxes
In C# Programming Language.


Project Source Code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class DataGridView_Add_Update_Delete_Selected_Row : Form
    {
        public DataGridView_Add_Update_Delete_Selected_Row()
        {
            InitializeComponent();
        }

        DataTable table = new DataTable();
        int selectedRow;
        private void DataGridView_Add_Update_Delete_Selected_Row_Load(object sender, EventArgs e)
        {
            // add columns to datatable
            table.Columns.Add("Id", typeof(int));
            table.Columns.Add("First Name", typeof(string));
            table.Columns.Add("Last Name", typeof(string));
            table.Columns.Add("Age", typeof(int));

            // add rows to datatable
            table.Rows.Add(1, "First A", "Last A", 10);
            table.Rows.Add(2, "First B", "Last B", 20);
            table.Rows.Add(3, "First C", "Last C", 30);
            table.Rows.Add(4, "First D", "Last D", 40);
            table.Rows.Add(5, "First E", "Last E", 50);
            table.Rows.Add(6, "First F", "Last F", 60);
            table.Rows.Add(7, "First G", "Last G", 70);
            table.Rows.Add(8, "First H", "Last H", 80);

            dataGridView1.DataSource = table;
        }

        // button add row to datagridview
        private void btnAdd_Click(object sender, EventArgs e)
        {
            // add row to datatable from textboxes
            table.Rows.Add(textBoxID.Text, textBoxFN.Text, textBoxLN.Text, textBoxAGE.Text);
            dataGridView1.DataSource = table;

        }

        // datagridview cell click
        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            // get datagridview selected row
            selectedRow = e.RowIndex;
            DataGridViewRow row = dataGridView1.Rows[selectedRow];

            // display datagridview selected row data into textboxes
            textBoxID.Text = row.Cells[0].Value.ToString();
            textBoxFN.Text = row.Cells[1].Value.ToString();
            textBoxLN.Text = row.Cells[2].Value.ToString();
            textBoxAGE.Text = row.Cells[3].Value.ToString();
        }

        // button update datagridview selected row
        private void btnUpdate_Click(object sender, EventArgs e)
        {
            DataGridViewRow newDataRow = dataGridView1.Rows[selectedRow];
            newDataRow.Cells[0].Value = textBoxID.Text;
            newDataRow.Cells[1].Value = textBoxFN.Text;
            newDataRow.Cells[2].Value = textBoxLN.Text;
            newDataRow.Cells[3].Value = textBoxAGE.Text;

        }

        // button remove
        private void Delete_Click(object sender, EventArgs e)
        {
            // delete datagridview row selected row
            selectedRow = dataGridView1.CurrentCell.RowIndex;
            dataGridView1.Rows.RemoveAt(selectedRow);

        }
    }
}

///////////////
OUTPUT:
c# datagridview add delete update row
c# datagridview add update delete row

ALSO SEE :

Add A Row To DataGridView From TextBox
Remove A Row From DataGridView
Get Selected Row Values From DataGridView Into TextBox
Update A DataGridView Row With TextBox



Share this

Related Posts

Previous
Next Post »

1 comments:

comments
13 décembre 2015 à 08:52 delete

This really helps me on my studies, Thanks bro!

Reply
avatar