1

I have tried to get an answer to this but so far no help has been able to do what I want it to.

I have this piece of code, which is meant to look at the selected row and output it's columns into the corresponding text boxes.

 private void DataGridView01_SelectionChanged(object sender, EventArgs e)
 {
    if (DataGridView01.SelectedRows.Count > 0)
    {
       personIDTextBox.Text = DataGridView01.SelectedRows[0].Cells[0].Value.ToString();
       comboBox1.Text = DataGridView01.SelectedRows[0].Cells[1].Value.ToString();
       Txt_FirstName.Text = DataGridView01.SelectedRows[0].Cells[2].Value.ToString();
       mIDDLENAMETextBox.Text = DataGridView01.SelectedRows[0].Cells[3].Value.ToString();
       sURNAMETextBox.Text = DataGridView01.SelectedRows[0].Cells[4].Value.ToString();
       cITYTextBox.Text = DataGridView01.SelectedRows[0].Cells[5].Value.ToString();
       eMAILTextBox.Text = DataGridView01.SelectedRows[0].Cells[6].Value.ToString();
     }
  }

When I launch the program, I get no errors but it doesn't output the data into the textbox. Anyone know what I am doing wrong?

imamage597
  • 97
  • 1
  • 2
  • 11
  • You do select a row, right? - Also: There seems to be one curly to many in your code; ae you sure it is as written?? – TaW Oct 22 '15 at 07:51
  • Did you put a breakpoint to see what's the value of DataGridView01.SelectedRows.Count? – User2012384 Oct 22 '15 at 07:51
  • @TaW Yes, I do select a row....... I have put a breakpoint and tried testing that but it just says "The name DataGridView01 does not exist in the current context. – imamage597 Oct 22 '15 at 07:53
  • That sounds fishy! How could that be without the compiler complaining..?? The 1st breakpoint should go on the if – TaW Oct 22 '15 at 07:55
  • @TaW I am unsure, I am fairly new to C# and Visual Studio. The extra curly was just a copy and paste error. I have no clue what i've done wrong. Everything I have read says that this code should work. The breakpoint is on the if. – imamage597 Oct 22 '15 at 07:57
  • Indeed, it does look ok; so you do hit the if with the debugger and can step on or does it complain about the DGV?? – TaW Oct 22 '15 at 07:58
  • I am unsure what you mean by the DGV? But when I press the start button, the program launches like normal, my DataGridView01 gets populated with the information from the SQL connection but when I click a row nothing changes. @TaW – imamage597 Oct 22 '15 at 07:59
  • DGV is just a short hand for DataGRidView. What is its `SelectionMode`? Maybe it doesn't get selected beause of that? There are a few and if any cell click shall select do use `FullRowSelect`! – TaW Oct 22 '15 at 08:01
  • @TaW I am using FullRowSelect. – imamage597 Oct 22 '15 at 08:05
  • So the row gets blue? Then the event is not __hooked up__? – TaW Oct 22 '15 at 08:06
  • Yes, the full row does turn blue. Could you please elaborate on "not hooked up" as I said I am fairly new. – imamage597 Oct 22 '15 at 08:07
  • If you have to ask that chances are that you have just copied the code without _hooking it up_. See my answer, which is a little too long for comments – TaW Oct 22 '15 at 08:08

3 Answers3

1

HOOKING UP EVENTS:

It is the most basic thing you need to learn to code in VS. In short it means that the event name, here DataGridView01_SelectionChanged is connected to the event. To do so one can either use code or one inserts it into the correct slot of the events pane of the property tab. Select the DataGridView, open the events pane (the one with the flash) and locate the SelectionChanged event! Here insert the name of the event and you are done.

enter image description here

(I only have the German versions of VS installed..)

The result is reflected in the form_designer.cs file and it is the same thing (in reverse) as double clicking that spot and then filling in the generated code stub..

Controls have many events; one is the default event and this can be generated by double clicking the control itself in the designer. But eventually you will need all 3 ways to generate and hook up the events, (as well as sometimes removing them.)

TaW
  • 53,122
  • 8
  • 69
  • 111
  • Hmm, I am unsure where the events pane is? I have tried looking but I don't see one with a yellow flash. – imamage597 Oct 22 '15 at 08:10
  • It is one of the panes along with the properties pane, usually at the right botton, and the flash is not yellow in all VS versions, see the screenshot! – TaW Oct 22 '15 at 08:15
  • Thank you, this is now working. I am just curious as to why tutorials don't cover this? I have read over 20 different ones and have never come across this. – imamage597 Oct 22 '15 at 08:15
  • Now I am curious how I can populate the combo box with the right selection as it's "DrowDownList". – imamage597 Oct 22 '15 at 08:19
  • 1
    That is a new question; as usual first google and read up on a few SO answers there; try a little code and __when and if__ you don't succeed write a new question! – TaW Oct 22 '15 at 08:21
0

I use a slightly different approach when trying to get data from a datagridview.

Try doing personIDTextBox.Text = DataGridView01.SelectedCells[0].Value.ToString();

but instead of the event being on selection change, switch to CellClick and change the property of the the datagridview row selection property to full row select. after that you can change the SelectedCell[0] number to match whichever cell you want

Takarii
  • 1,612
  • 2
  • 18
  • 29
  • Thanks for trying to help but with that, I am getting DataGridView does not contain a definition for SelectedCell. – imamage597 Oct 22 '15 at 08:04
  • Sorry, i just spotted a typo in what I wrote - `SelectedCells[0]` is the method (plural, not singular). Answer updated – Takarii Oct 22 '15 at 08:11
0

If you want to display the datagridview selected rows into corresponding textboxes, fine the below steps ,

Step 1: 1. Change the DataGridView Selection mode to FullRowSelect in Datagridview property. 2. Create the cell click event in Data grid view using property. enter image description here 3. Write the below code and test it, It may helpful

private void DataGridView01_CellClick(object sender,DataGridViewCellEventArgs e)
{
if (DataGridView01.Rows.Count > -1)
{
PersonIdTextBox.Text=DataGridView01.Rows[e.RowIndex].Cells[0].Value.ToString();
comboBox1.Text = DataGridView01.Rows[e.RowIndex].Cells[1].Value.ToString();
Txt_FirstName.Text = DataGridView01.Rows[e.RowIndex].Cells[2].Value.ToString();
 mIDDLENAMETextBox.Text = DataGridView01.Rows[e.RowIndex].Cells[3].Value.ToString();
sURNAMETextBox.Text = DataGridView01.Rows[e.RowIndex].Cells[4].Value.ToString();
cITYTextBox.Text = DataGridView01.Rows[e.RowIndex].Cells[5].Value.ToString();
eMAILTextBox.Text = DataGridView01.Rows[e.RowIndex].Cells[6].Value.ToString();

}
}
Ramesh P
  • 81
  • 4