How to Display Selected Row from Datagridview into Textbox using C#

In this tutorial I’m going to show you how to create an application that will display the selected row from the datagridview into textbox. To start with this application we will create first a database in Microsoft access 2003 and we will name it as “studentdb” for Student Database. Then create a table named “tblstudent” and create a field that will look like as shown below. s1 After this, we will create a new windows form project in C#, then save it as “stud_info”. Then we will add controls to the form such as button, textboxes, datagridview and labels and designed that looks like as shown below. s2 Before we proceed, make sure that our database is placed in our bin directory. At this time, we will add functionality to our application. First double click the form and add the following code above the public Form1() function.
  1. //declare new variable named dt as New Datatable
  2. DataTable dt = new DataTable();
  3. //this line of code used to connect to the server and locate the database (studentdb.mdb)
  4. static string connection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " + Application.StartupPath + "/studentdb.mdb";
  5. OleDbConnection conn = new OleDbConnection(connection);
After this, we will add functionality to our “Load All Data” button. By adding these following codes. This code below will get all the record found in the database table called “tblstudent” and display it into the datagridview.
  1. //create a new datatable
  2. dt = new DataTable();
  3. //create our SQL SELECT statement
  4. string sql = "Select * from tblstudent";
  5. //then we execute the SQL statement against the Connection using OleDBDataAdapter
  6. OleDbDataAdapter da = new OleDbDataAdapter(sql, conn);
  7. //we fill the result to dt which declared above as datatable
  8. da.Fill(dt);
  9. //then we populate the datagridview by specifying the datasource equal to dt
  10. dataGridView1.DataSource = dt;
Then when you run the application and try to click the “Load All Data” button. It will display like as shown below. s3 Next, we will add functionality when the user click the selected row and display to the corresponding textbox. First select the datagridview and click the events under the window of the properties and select “CellClick” that looks like as shown below. s4 Then add the following code that will get the data from the selected row and display in the textbox provided.
  1. //it checks if the row index of the cell is greater than or equal to zero
  2. if (e.RowIndex >= 0)
  3. {
  4. //gets a collection that contains all the rows
  5. DataGridViewRow row = this.dataGridView1.Rows[e.RowIndex];
  6. //populate the textbox from specific value of the coordinates of column and row.
  7. txtid.Text = row.Cells[0].Value.ToString();
  8. txtfname.Text = row.Cells[1].Value.ToString();
  9. txtlname.Text = row.Cells[2].Value.ToString();
  10. txtcourse.Text = row.Cells[3].Value.ToString();
  11. txtgender.Text = row.Cells[4].Value.ToString();
  12. txtaddress.Text = row.Cells[5].Value.ToString();
  13.  
  14. }
After this, you test it by pressing the “F5” or clicking the start button. Then when you run this application, make sure that the user first click the “Load all Record” button, then that’s the time that’s the user can click the selected row to display in the textbox. And running application is look like as shown below. s5 And here’s all the code used for this application.
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Data.OleDb;
  10. namespace student_info
  11. {
  12. public partial class Form1 : Form
  13. {
  14. //declare new variable named dt as New Datatable
  15. DataTable dt = new DataTable();
  16. //this line of code used to connect to the server and locate the database (usermgt.mdb)
  17. static string connection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " + Application.StartupPath + "/studentdb.mdb";
  18. OleDbConnection conn = new OleDbConnection(connection);
  19. public Form1()
  20. {
  21. InitializeComponent();
  22. }
  23.  
  24. private void btnload_Click(object sender, EventArgs e)
  25. {
  26. //create a new datatable
  27. dt = new DataTable();
  28. //create our SQL SELECT statement
  29. string sql = "Select * from tblstudent";
  30. //then we execute the SQL statement against the Connection using OleDBDataAdapter
  31. OleDbDataAdapter da = new OleDbDataAdapter(sql, conn);
  32. //we fill the result to dt which declared above as datatable
  33. da.Fill(dt);
  34. //then we populate the datagridview by specifying the datasource equal to dt
  35. dataGridView1.DataSource = dt;
  36. }
  37.  
  38. private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
  39. {
  40. //it checks if the row index of the cell is greater than or equal to zero
  41. if (e.RowIndex >= 0)
  42. {
  43. //gets a collection that contains all the rows
  44. DataGridViewRow row = this.dataGridView1.Rows[e.RowIndex];
  45. //populate the textbox from specific value of the coordinates of column and row.
  46. txtid.Text = row.Cells[0].Value.ToString();
  47. txtfname.Text = row.Cells[1].Value.ToString();
  48. txtlname.Text = row.Cells[2].Value.ToString();
  49. txtcourse.Text = row.Cells[3].Value.ToString();
  50. txtgender.Text = row.Cells[4].Value.ToString();
  51. txtaddress.Text = row.Cells[5].Value.ToString();
  52.  
  53. }
  54.  
  55. }
  56.  
  57. private void Form1_Load(object sender, EventArgs e)
  58. {
  59.  
  60. }
  61.  
  62.  
  63. }
  64. }

Comments

Submitted byCarlos Martinez (not verified)on Fri, 12/05/2014 - 23:09

this code, works propely, but i want to know how can i insert values from a ControlNumber on two textbox, something like: ControlNumbre || Date || Hours 8457 || 11/12/14 || 14:31 8454 || 11/12/14 || 14:40 8457 || 11/12/14 || 14:50 what i needing is, when i select a field on datagrid it insert the values on textbox but separate something like: 8457 || 11/12/14 || 14:31 8457 || 11/12/14 || 14:50 because i need to get the hours difference. on another textbox. note, i need to choose the same ControlNumber and the same Date, have you an idea, how can i realize that? sorry for my english
Submitted byfirozkhan6786 (not verified)on Thu, 11/05/2020 - 12:38

while getting the data from the database i want only fname to display in datagridview and all other columns hidden using single line code

Add new comment