0

I have a problem displaying the data on text boxes when clicked on DataGridView because I show only fullname and student id column on DataGridView.

con.Open();
SqlDataAdapter sda = new SqlDataAdapter("SELECT Student_ID as S_ID, Fname +' '+ Lname +' '+ Mname as NAME from student", con);
DataTable dtbl = new DataTable();
sda.Fill(dtbl);
dataGridView1.DataSource = dtbl;
dataGridView1.Columns[0].Width = 40;
dataGridView1.Columns[1].Width = 200;
dataGridView1.RowHeadersVisible = false;
con.Close();

this

LarsTech
  • 80,625
  • 14
  • 153
  • 225
  • 2
    That code doesn't really show any effort at your problem. It does show you need to learn to use sql parameters real quick to avoid sql injection and formatting errors. – LarsTech Feb 24 '19 at 14:54
  • thank you for your reply like i said im just a beginer im just a kid ya know trying to get help from here . – Ronald Caymo Feb 24 '19 at 15:04
  • 2
    Use databinding; bind text box to the same data source which your data data grid view use a data source. Also you need to learn how to use [data source window](https://stackoverflow.com/q/38165043/3110834). – Reza Aghaei Feb 24 '19 at 15:40
  • 2
    Also if you just load Id and Name, you cannot expect to show all other fields on the form without another query. As an option you can load all data using the first query, but show just Id and Name column i the data grid view. – Reza Aghaei Feb 24 '19 at 16:00

1 Answers1

2

So, If I understand you correctly. When you're clicking on a datagridview cell you wanna display the information in the textboxes?

Start with this:

  • Click on your datagridview. -> Select Events -> Double click on "CellContentClick"

From here you can select the data from datagridview, and insert it into the textbox like this:

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
    YourTextBoxName.Text = dataGridView1.CurrentRow.Cells["Lname"].Value.ToString();
} 
Putte
  • 328
  • 2
  • 17
  • But i dont have Lname Cell because i only show Name and S_id on datagridview – Ronald Caymo Feb 24 '19 at 15:18
  • see the picture above i only show name and id on datagridview what i want is when i click on that name the text boxes will display the data that i added to them . – Ronald Caymo Feb 24 '19 at 15:20
  • Using `CellContentClick` is definitely wrong. What if the user click on the row/cell headers? What if the user move between records by keyboard? – Reza Aghaei Feb 24 '19 at 15:57
  • @RonaldCaymo What textboxes are you actually talking about? What I can read from your answer is: When I click on the datagridview name, you want it to be displayed in the textboxes. And that's what this code is doing. And "Lname" is because you selected it in your sql query, and I assume you mean "Lastname" by that. But you can simply just change the "Lname" to whatever your database string is. If you don't mean like this, you have to re-ask you question, cuz I cannot find any other answer to this. :) – Putte Feb 24 '19 at 18:48
  • @RezaAghaei Well, what do you suggest instead of CellContentClick? Is there any other you can select just a specific row? :) – Putte Feb 24 '19 at 18:49
  • Data-binding as I described in the comments under the question. – Reza Aghaei Feb 25 '19 at 03:37
  • Sounds like you're trying to pull back more data from your database and display in all text boxes. To do this you need to use data-binding or populate a custom List with all values. – Magic Mick Feb 25 '19 at 05:18