C# - How To Update A DataGridView Row With TextBox In C#

C# - How To Update A DataGridView Row With TextBoxes In C#


In This C# Code We Will See How To  Update A DataGridView Row Using TextBoxes In CSharp 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 Update_DataGridView_Using_TextBoxes : Form
        public Update_DataGridView_Using_TextBoxes()

        DataTable table = new DataTable();
        int indexRow;

        private void Update_DataGridView_Using_TextBoxes_Load(object sender, EventArgs e)
   // set datatable columns values
   table.Columns.Add("Id", typeof(int));// data type int
   table.Columns.Add("First Name", typeof(string));// datatype string
   table.Columns.Add("Last Name", typeof(string));// datatype string
   table.Columns.Add("Age", typeof(int));// data type int

            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;


        // Get Selected Row Values From DataGridView Into TextBox
        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
            indexRow = e.RowIndex; // get the selected Row Index
            DataGridViewRow row = dataGridView1.Rows[indexRow];

            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();

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

/////////////////////////// OUTPUT: 
C# Update DataGridView Selected Row Using TextBoxes
DatagridView Row Uodated


Add A Row To DataGridView From TextBox
Remove A Row From DataGridView
Get Selected Row Values From DataGridView Into TextBox
Add Delete And Update DataGridView Row Using TextBoxes

Share this

Related Posts

Next Post »