0

I have read numerous articles and tutorials on this button each attempt doesn't work. I click within the gridview and the textboxes just don't populate. Here are two examples of what I've tried.

        private void GridView01_MouseClick(object sender, MouseEventArgs e)
    {
        personIDTextBox.Text = GridView01.SelectedRows[0].Cells[0].Text.ToString();

        comboBox1.Text = GridView01.SelectedRows[0].Cells[1].Text.ToString();
        Txt_FirstName.Text = GridView01.SelectedRows[0].Cells[2].Text.ToString();
        mIDDLENAMETextBox.Text = GridView01.SelectedRows[0].Cells[3].Text.ToString();
    }

Error CS1061 'DataGridViewCell' does not contain a definition for 'Text' and no extension method 'Text' accepting a first argument of type 'DataGridViewCell' could be found (are you missing a using directive or an assembly reference?

imamage597
  • 97
  • 1
  • 2
  • 11
  • If you enter `row.Cells["PersonID"].Value.ToString();` into your immediate window while debugging, what does it return? If your textboxes are just displaying an empty string then you mustn't be accessing the cells correctly. – sab669 Oct 21 '15 at 11:33
  • Per [this question](http://stackoverflow.com/questions/7534335/accessing-gridview-cells-value) you should be accessing the cell's `Text` property, not `Value`. Try that, as Shreyas Bhandimatt answered and it should work. – sab669 Oct 21 '15 at 11:35
  • Is this an ASP.NET `GridView` or is this a Win Forms `DataGridView` – sab669 Oct 21 '15 at 13:07
  • @sab669 Sorry, It's win Forms. Forgot to mention that – imamage597 Oct 21 '15 at 13:08

2 Answers2

1

FYI Going forward, if you continue to use StackOverflow, you should be sure to include the tags for WinForms, WPF, or ASP.NET (or at least specify it in your post) and make sure you state the full type of the control you're using. You use a DataGridView but you named it GridView01, which causes confusion for us because there is the WinForms DataGridView control and the ASP.NET GridView control which have different properties and methods associated with them. So one person's suggested answer might not work because they mistook your control for a different one. The other StackOverflow question I linked in my comment on your original post is for an ASP.NET GridView control, which is why we thought you should be accessing the Text property. Only the error message which states it's actually a DataGridView was I able to determine which control you're actually using ;)

Anyways, I created a small little WinForms application with just a DataGridView and Textbox control on it. In my Form_Load event I added some rows with text data to my DataGridView like so:

private void Form1_Load(object sender, EventArgs e)
{
    for (int i = 0; i < 10; i++)
    {
        DataGridViewRow dgvr = (DataGridViewRow)dataGridView1.Rows[0].Clone();

        for (int j = 0; j < 3; j++)
        {
            switch j)
            {
                case 0:
                    dgvr.Cells[j].Value = "A - " + i;
                    break;
                case 1:
                    dgvr.Cells[j].Value = "B - " + i;
                    break;
                case 2:
                    dgvr.Cells[j].Value = "C - " + i;
                    break;
            }
            
        }

        dataGridView1.Rows.Add(dgvr);
    }
}

You of course don't need to do that, I'm just showing you what I did so there are no mysteries.

Then I added a SelectionChanged event to my DataGridView control. I don't seem to have a Mouse_Click event available, so instead I set myDataGridView.SelectionMode = FullRowSelect in the properties sheet. This makes it so that you can't select a single cell, but rather clicking a cell selects the entire row. This guarantees* there'll be something in the SelectedRows property.

private void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
    if (dataGridView1.SelectedRows.Count > 0)
    {
        textBox1.Text = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
    }
}

And this is properly dumping the value of the first cell within the selected row into my textbox:

enter image description here

Then all you need to do is set which textbox uses which cell index.

Edit: Sample 'Immediate Window' output;

enter image description here

Edit 2: Make sure the application is running, set the breakpoint, select a new row in your DataGridView and then enter that line in the immediate window like shown here: https://i.stack.imgur.com/gR8HM.png

Community
  • 1
  • 1
sab669
  • 3,984
  • 8
  • 38
  • 75
  • Thanks for trying to help, but I am not getting the output into the text box, I click the row and none of the data goes into the text boxes. – imamage597 Oct 21 '15 at 13:47
  • If you put a breakpoint in the `SelectionChanged` event, go to `Debug` -> `Windows` -> `Immediate` and then type in `dataGridView1.SelectedRows[0].Cells[0].Value.ToString();` and hit enter. Does it give you anything? Editted my answer to include a snippet from the `Immediate` window where you can see my results. Note that they don't line up with my other screenshot, but that's because I didn't change my selected row since taking the second screenshot. – sab669 Oct 21 '15 at 13:49
  • I get "The name 'GridView01' does not exist in the current context" – imamage597 Oct 21 '15 at 13:52
  • Did you put the breakpoint *inside* the event handler and actually have something selected? I just put the breakpoint on the `if (dataGridView1.SelectedRows.Count > 0)` line before taking the screenshot in my editted section of the answer. – sab669 Oct 21 '15 at 13:54
  • I am unsure what you mean by have something selected and I put a breakpoint at the first "personIDTextBox.Text = GridView01.SelectedRows[0].Cells[0].Value.ToString();" – imamage597 Oct 21 '15 at 13:56
  • I just keep getting that my datagridview doesn't exist in the current context? – imamage597 Oct 21 '15 at 14:02
  • @imamage597 Re-editted my post a second time with a sample screenshot. Run the application, set the breakpoint in the code, select a row in your application (this will trigger the breaktpoint) and **then** run that snippet in the `Immediate` window. The error you're getting means that, wherever you are in your application, the control doesn't exist yet as far as the debugger is concerned. So you're probably not accessing it at the right time. – sab669 Oct 21 '15 at 14:06
  • It now says "The expression cannot be evaluated while in run mode. " If I try what you just said. – imamage597 Oct 21 '15 at 14:09
0
   private void GridView01_MouseClick(object sender, MouseEventArgs e)
    {
 personIDTextBox.Text = GridView01.SelectedRows[0].Cells[0].Text;

        comboBox1.Text = GridView01.SelectedRows[0].Cells[1].Text;
        Txt_FirstName.Text = GridView01.SelectedRows[0].Cells[2].Text;
        mIDDLENAMETextBox.Text = GridView01.SelectedRows[0].Cells[3].Text;
 }

Just replace with this code it will work.

Shreyas Achar
  • 1,407
  • 5
  • 36
  • 63
  • This doesn't work. I get 'MouseEventArgs' does not contain a definition for 'RowIndex' and no extension method 'RowIndex' accepting a first argument of type 'MouseEventArgs' could be found. – imamage597 Oct 21 '15 at 11:39
  • 1
    @imamage597 Change it to reference `GridView01.SelectedRows[0]` rather than `e.RowIndex` like you access in the second snippet of the code in your original post, then use the `Text` property like I said in my comment. – sab669 Oct 21 '15 at 11:41
  • Also, @ShreyasBhandimatt you don't need to call `ToString()` because the `Text` property is already a string. – sab669 Oct 21 '15 at 11:42
  • @sab669 Now I get this error: Error CS1061 'DataGridViewCell' does not contain a definition for 'Text' and no extension method 'Text' accepting a first argument of type 'DataGridViewCell' could be found. – imamage597 Oct 21 '15 at 11:43
  • @sab669 Still looking for assistance with this. – imamage597 Oct 21 '15 at 12:39
  • @imamage597 Can you update your original post with **just** the code you're using now (including the containing event declaration) and the error message? Not sure exactly what you have – sab669 Oct 21 '15 at 12:43
  • @sab669 I am unsure what you mean by "containing event declaration", I am still fairly new to this but I have updated my main post – imamage597 Oct 21 '15 at 12:54
  • @imamage597 I just meant like "Is it in the mouse click event, and if so contain the `private void someControl_MouseClick(object sender, eventArgs e)` line, which you did. – sab669 Oct 21 '15 at 13:01
  • @sab669 Alright thanks. If you could help I'd greatly appreciate it. – imamage597 Oct 21 '15 at 13:03