C# - How To Get Selected DataGridView Row Values Into InpuBoxes In C#

get selected datagridview row data

C# - How To Display Data From Selected DataGridView Row into InputBox Using C#

                                                                                                                                                     

In This C# Code We Will See How To Get A DataGridView Row Values Into  InpuBoxes
In C# Programming Language.




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_Show_DatagridView_Row_Data_In_InputBox : Form
    {
        public Csharp_Show_DatagridView_Row_Data_In_InputBox()
        {
            InitializeComponent();
        }

        DataTable table = new DataTable();

        private void Csharp_Show_DatagridView_Row_Data_In_InputBox_Load(object sender, EventArgs e)
        {
            // populate DatagridView with some data 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;

        }

        // dataGridView Cell Click
        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            // get selected row index
            int selectedRowIndex = e.RowIndex;

            DataGridViewRow row = new DataGridViewRow();

            row = dataGridView1.Rows[selectedRowIndex];

            // get data from the selected row
            int id = int.Parse(row.Cells[0].Value.ToString());
            //string id2 = row.Cells[0].Value.ToString();
            string fn = row.Cells[1].Value.ToString();
            string ln = row.Cells[2].Value.ToString();
            int age = int.Parse(row.Cells[3].Value.ToString());

            // show the selected row data on inputboxes
            Interaction.InputBox("The Id", "Row Data", id.ToString(), -1, -1);
            Interaction.InputBox("The First Name", "Row Data", fn, -1, -1);
            Interaction.InputBox("The Last Name", "Row Data", ln, -1, -1);
            Interaction.InputBox("The Age", "Row Data", age.ToString(), -1, -1);
        }
    }
}
/////////////////////////// OUTPUT:

show datagridview selected row
datagridview selected row



Share this

Related Posts

Previous
Next Post »