Display the Current Row Into the TextBox Using C#

May 24, 2020
C#
How to Display the Current Row Into the TextBoxes Using C#

Tutorial: Display the Current Row Into the TextBox Using C# with Source Code

About Display the Current Row Into the TextBox Using C#

This is a tutorial on How to Display the Current Row Into the TextBox Using C#. Passing information from the datagridview into the textbox is a typical issue on the off chance that you are an amateur in programming. Along these lines, in this instructional exercise, I will show you how to show the present line into the textboxes utilizing C#. This program is formed by including section and lines in the datagridview automatically and it will naturally show the information in the textbox when the datagridview cell is clicked. This is a major assistance for you to take care of your present issue in programming and you can do it quickly. Follow the bit by bit manual for know how it functions.

Creating Application

Step 1

Open Microsoft Visual Studio 2019 and create a new windows form application for C#.

Step 2

Make the form just like shown below.

Step 3

Double click the form and add the following codes below for adding columns and rows automatically.

     
private void Form1_Load(object sender, EventArgs e)
{
    //setup the columns to be added.
    dataGridView1.ColumnCount = 4;
    //Set the columns name 
    dataGridView1.Columns[0].Name = "Item";
    dataGridView1.Columns[1].Name = "Price";
    dataGridView1.Columns[2].Name = "Quantity";
    dataGridView1.Columns[3].Name = "Sub-Total";
     
    //Set a value to be added in a row
    string[] row = new string[] {  "Cellphone", "15,000", "2", "30,000" };
    //adding rows in the datagridview
    dataGridView1.Rows.Add(row);
    //Set a value to be added in a row
    row = new string[] { "Laptop", "21,000", "1", "21,000" };
    //adding rows in the datagridview
    dataGridView1.Rows.Add(row);
    //Set a value to be added in a row
    row = new string[] { "Speaker", "2,000", "2", "4,000" };
    //adding rows in the datagridview
    dataGridView1.Rows.Add(row);
    //Set a value to be added in a row
    row = new string[] { "Desktop Computer", "10,000", "5", "50,000" };
    //adding rows in the datagridview
    dataGridView1.Rows.Add(row);
}

Step 4

Encode the following code below for passing the data from the datagridview to the textboxes when the current row is selected.

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
    DataGridView dtg = new DataGridView();
    dtg = dataGridView1;
 
    txtProduct.Text = dtg.CurrentRow.Cells[0].Value.ToString();
    txtPrice.Text = dtg.CurrentRow.Cells[1].Value.ToString();
    txtQauntity.Text = dtg.CurrentRow.Cells[2].Value.ToString();
    txtSubtotal.Text = dtg.CurrentRow.Cells[3].Value.ToString();
}
Download Here

Leave a Reply

Your email address will not be published. Required fields are marked *