C# - How To Update A DataGridView Row Using InputBox In C#

update datagridview selected row

C# - How To Update A DataGridView Selected Row With InputBoxes In C#

__________________________________________________________________________

In This C# Code We Will See How To  Update A DataGridView Row Using InputBoxes 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;
using Microsoft.VisualBasic;

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

        DataTable table = new DataTable();
        int selectedRowIndex;
        private void Csharp_Update_DatagridView_Row_Using_InputBox_Load(object sender, EventArgs e)
        {
            // populate DatagridView using 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));

            table.Rows.Add(1, "AAA", "BBB", 32);
            table.Rows.Add(2, "CCC", "DDD", 23);
            table.Rows.Add(3, "EEE", "FFF", 16);
            table.Rows.Add(4, "GGG", "HHH", 45);
            table.Rows.Add(5, "III", "JJJ", 53);
            table.Rows.Add(6, "KKK", "LLL", 62);

            dataGridView1.DataSource = table;
        }

        // update elected datagridview row 
        private void BtnUpdateRow_Click(object sender, EventArgs e)
        {
            DataGridViewRow row = new DataGridViewRow();

            row = dataGridView1.Rows[selectedRowIndex];

            string id = Interaction.InputBox("Enter The New Id", "Update Data", row.Cells[0].Value.ToString(), -1, -1);
            string fn = Interaction.InputBox("Enter The New First Name", "Update Data", row.Cells[1].Value.ToString(), -1, -1);
            string ln = Interaction.InputBox("Enter The New Last Name", "Update Data", row.Cells[2].Value.ToString(), -1, -1);
            string age = Interaction.InputBox("Enter The New Age", "Update Data", row.Cells[3].Value.ToString(), -1, -1);

            row.Cells[0].Value = int.Parse(id);
            row.Cells[1].Value = fn;
            row.Cells[2].Value = ln;
            row.Cells[3].Value = int.Parse(age);
        
        }

        // get the selected datagridview row index
        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            selectedRowIndex = e.RowIndex;
        }
    }
}

 
/////////////////////////// OUTPUT: 
update datagridview selected row
update datagridview row




Share this

Related Posts

Previous
Next Post »