Display the Current Row
Display the Current Row Into the TextBox Using C#
Details: Display the Current Row
How to Display the Current Row Into the TextBox Using C#. Transferring information from the datagridview into the textbox is a common issue that beginners encounter in programming. In this tutorial, we will look at how to show the present line into the textboxes utilizing C#.
This program is formed by including sections and lines in the datagridview automatically and it will naturally show the information in the textbox when the datagridview cell is clicked. This is 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 to know how it functions.
Create the Application: Display the Current Row
1 – Open Microsoft Visual Studio 2019 to start a new windows form application for C#.
2 – Create the form as shown below.
3 – Double tap the form and add the codes below for adding sections and columns consequently.
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);
}
4 – Encode the code below for passing the information from the datagridview to the textboxes when the ongoing line is chosen.
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();
}