Can't access Cell data in DataGridView VB.Net












0















I've got a project in Visual Studio 2017, using VB.NET that populates a DataGridView using a DataTable. The data appears fine in the grid, but when I try to access the cell value in the SelectionChanged event, I always get Nothing. Here's the event:



    Private Sub grdBooks_SelectionChanged(sender As Object, e As EventArgs) Handles grdBooks.SelectionChanged
If Not grdBooks.CurrentRow Is Nothing Then
If grdBooks.CurrentRow.Index > -1 Then
Dim X As String = grdBooks.Rows(0).Cells(0).Value
Dim Y = grdBooks(0, 0).Value
UpdateEditPanel(False)
End If
End If
End Sub


So both X and Y should give me the same value from the grid at position 0,0 but they don't. I've used this before in other projects. Does anybody have any ideas?



Thanks, Colin










share|improve this question























  • Did you by chance add a DataGridviewColumn with (Visible=False) to the DataGridview prior to binding the DataTable? Possibly via the designer. Try adding DataGridView1.Columns.Clear() before setting the DataSource.

    – TnTinMn
    Nov 28 '18 at 18:35








  • 1





    You have a confusing post. You say the cell is always nothing, but then you say x and y should be the same value but aren't. Which issue is it? The code you posted does not support either issue.

    – LarsTech
    Nov 28 '18 at 19:48











  • The code sample you provided works for me. I see the value in cell 0/0 when I select different items in the grid.

    – ThePeter
    Nov 28 '18 at 22:00
















0















I've got a project in Visual Studio 2017, using VB.NET that populates a DataGridView using a DataTable. The data appears fine in the grid, but when I try to access the cell value in the SelectionChanged event, I always get Nothing. Here's the event:



    Private Sub grdBooks_SelectionChanged(sender As Object, e As EventArgs) Handles grdBooks.SelectionChanged
If Not grdBooks.CurrentRow Is Nothing Then
If grdBooks.CurrentRow.Index > -1 Then
Dim X As String = grdBooks.Rows(0).Cells(0).Value
Dim Y = grdBooks(0, 0).Value
UpdateEditPanel(False)
End If
End If
End Sub


So both X and Y should give me the same value from the grid at position 0,0 but they don't. I've used this before in other projects. Does anybody have any ideas?



Thanks, Colin










share|improve this question























  • Did you by chance add a DataGridviewColumn with (Visible=False) to the DataGridview prior to binding the DataTable? Possibly via the designer. Try adding DataGridView1.Columns.Clear() before setting the DataSource.

    – TnTinMn
    Nov 28 '18 at 18:35








  • 1





    You have a confusing post. You say the cell is always nothing, but then you say x and y should be the same value but aren't. Which issue is it? The code you posted does not support either issue.

    – LarsTech
    Nov 28 '18 at 19:48











  • The code sample you provided works for me. I see the value in cell 0/0 when I select different items in the grid.

    – ThePeter
    Nov 28 '18 at 22:00














0












0








0








I've got a project in Visual Studio 2017, using VB.NET that populates a DataGridView using a DataTable. The data appears fine in the grid, but when I try to access the cell value in the SelectionChanged event, I always get Nothing. Here's the event:



    Private Sub grdBooks_SelectionChanged(sender As Object, e As EventArgs) Handles grdBooks.SelectionChanged
If Not grdBooks.CurrentRow Is Nothing Then
If grdBooks.CurrentRow.Index > -1 Then
Dim X As String = grdBooks.Rows(0).Cells(0).Value
Dim Y = grdBooks(0, 0).Value
UpdateEditPanel(False)
End If
End If
End Sub


So both X and Y should give me the same value from the grid at position 0,0 but they don't. I've used this before in other projects. Does anybody have any ideas?



Thanks, Colin










share|improve this question














I've got a project in Visual Studio 2017, using VB.NET that populates a DataGridView using a DataTable. The data appears fine in the grid, but when I try to access the cell value in the SelectionChanged event, I always get Nothing. Here's the event:



    Private Sub grdBooks_SelectionChanged(sender As Object, e As EventArgs) Handles grdBooks.SelectionChanged
If Not grdBooks.CurrentRow Is Nothing Then
If grdBooks.CurrentRow.Index > -1 Then
Dim X As String = grdBooks.Rows(0).Cells(0).Value
Dim Y = grdBooks(0, 0).Value
UpdateEditPanel(False)
End If
End If
End Sub


So both X and Y should give me the same value from the grid at position 0,0 but they don't. I've used this before in other projects. Does anybody have any ideas?



Thanks, Colin







vb.net datagridview cell






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 28 '18 at 18:05









colinmeekscolinmeeks

12




12













  • Did you by chance add a DataGridviewColumn with (Visible=False) to the DataGridview prior to binding the DataTable? Possibly via the designer. Try adding DataGridView1.Columns.Clear() before setting the DataSource.

    – TnTinMn
    Nov 28 '18 at 18:35








  • 1





    You have a confusing post. You say the cell is always nothing, but then you say x and y should be the same value but aren't. Which issue is it? The code you posted does not support either issue.

    – LarsTech
    Nov 28 '18 at 19:48











  • The code sample you provided works for me. I see the value in cell 0/0 when I select different items in the grid.

    – ThePeter
    Nov 28 '18 at 22:00



















  • Did you by chance add a DataGridviewColumn with (Visible=False) to the DataGridview prior to binding the DataTable? Possibly via the designer. Try adding DataGridView1.Columns.Clear() before setting the DataSource.

    – TnTinMn
    Nov 28 '18 at 18:35








  • 1





    You have a confusing post. You say the cell is always nothing, but then you say x and y should be the same value but aren't. Which issue is it? The code you posted does not support either issue.

    – LarsTech
    Nov 28 '18 at 19:48











  • The code sample you provided works for me. I see the value in cell 0/0 when I select different items in the grid.

    – ThePeter
    Nov 28 '18 at 22:00

















Did you by chance add a DataGridviewColumn with (Visible=False) to the DataGridview prior to binding the DataTable? Possibly via the designer. Try adding DataGridView1.Columns.Clear() before setting the DataSource.

– TnTinMn
Nov 28 '18 at 18:35







Did you by chance add a DataGridviewColumn with (Visible=False) to the DataGridview prior to binding the DataTable? Possibly via the designer. Try adding DataGridView1.Columns.Clear() before setting the DataSource.

– TnTinMn
Nov 28 '18 at 18:35






1




1





You have a confusing post. You say the cell is always nothing, but then you say x and y should be the same value but aren't. Which issue is it? The code you posted does not support either issue.

– LarsTech
Nov 28 '18 at 19:48





You have a confusing post. You say the cell is always nothing, but then you say x and y should be the same value but aren't. Which issue is it? The code you posted does not support either issue.

– LarsTech
Nov 28 '18 at 19:48













The code sample you provided works for me. I see the value in cell 0/0 when I select different items in the grid.

– ThePeter
Nov 28 '18 at 22:00





The code sample you provided works for me. I see the value in cell 0/0 when I select different items in the grid.

– ThePeter
Nov 28 '18 at 22:00












2 Answers
2






active

oldest

votes


















0














I am a bit confused by this post aswell but I hope this helps you resolve the problem. Possible solution #1



Dim X As String = DataGridView1.SelectedRows(0).Cells(0).Value.ToString 


or



Dim X As String = DataGridView1.SelectedRows(0).Cells(0).Value.ToString() 


If you are able to provide more code, I'm sure we can resolve this Issue. Cheers.






share|improve this answer































    0














    If you just want to click the cell from the Grid, try this:



    Private Sub grdBooks_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles grdBooks.CellContentClick

    Dim selectedRow As DataGridViewRow

    selectedRow = grdBooks.Rows(e.RowIndex)

    Dim x as string = selectedRow.Cells(0).Value.ToString()


    End Sub





    share|improve this answer
























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53525532%2fcant-access-cell-data-in-datagridview-vb-net%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      I am a bit confused by this post aswell but I hope this helps you resolve the problem. Possible solution #1



      Dim X As String = DataGridView1.SelectedRows(0).Cells(0).Value.ToString 


      or



      Dim X As String = DataGridView1.SelectedRows(0).Cells(0).Value.ToString() 


      If you are able to provide more code, I'm sure we can resolve this Issue. Cheers.






      share|improve this answer




























        0














        I am a bit confused by this post aswell but I hope this helps you resolve the problem. Possible solution #1



        Dim X As String = DataGridView1.SelectedRows(0).Cells(0).Value.ToString 


        or



        Dim X As String = DataGridView1.SelectedRows(0).Cells(0).Value.ToString() 


        If you are able to provide more code, I'm sure we can resolve this Issue. Cheers.






        share|improve this answer


























          0












          0








          0







          I am a bit confused by this post aswell but I hope this helps you resolve the problem. Possible solution #1



          Dim X As String = DataGridView1.SelectedRows(0).Cells(0).Value.ToString 


          or



          Dim X As String = DataGridView1.SelectedRows(0).Cells(0).Value.ToString() 


          If you are able to provide more code, I'm sure we can resolve this Issue. Cheers.






          share|improve this answer













          I am a bit confused by this post aswell but I hope this helps you resolve the problem. Possible solution #1



          Dim X As String = DataGridView1.SelectedRows(0).Cells(0).Value.ToString 


          or



          Dim X As String = DataGridView1.SelectedRows(0).Cells(0).Value.ToString() 


          If you are able to provide more code, I'm sure we can resolve this Issue. Cheers.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 28 '18 at 22:10









          DJNDJN

          366




          366

























              0














              If you just want to click the cell from the Grid, try this:



              Private Sub grdBooks_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles grdBooks.CellContentClick

              Dim selectedRow As DataGridViewRow

              selectedRow = grdBooks.Rows(e.RowIndex)

              Dim x as string = selectedRow.Cells(0).Value.ToString()


              End Sub





              share|improve this answer




























                0














                If you just want to click the cell from the Grid, try this:



                Private Sub grdBooks_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles grdBooks.CellContentClick

                Dim selectedRow As DataGridViewRow

                selectedRow = grdBooks.Rows(e.RowIndex)

                Dim x as string = selectedRow.Cells(0).Value.ToString()


                End Sub





                share|improve this answer


























                  0












                  0








                  0







                  If you just want to click the cell from the Grid, try this:



                  Private Sub grdBooks_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles grdBooks.CellContentClick

                  Dim selectedRow As DataGridViewRow

                  selectedRow = grdBooks.Rows(e.RowIndex)

                  Dim x as string = selectedRow.Cells(0).Value.ToString()


                  End Sub





                  share|improve this answer













                  If you just want to click the cell from the Grid, try this:



                  Private Sub grdBooks_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles grdBooks.CellContentClick

                  Dim selectedRow As DataGridViewRow

                  selectedRow = grdBooks.Rows(e.RowIndex)

                  Dim x as string = selectedRow.Cells(0).Value.ToString()


                  End Sub






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 29 '18 at 3:36









                  PratilectronPratilectron

                  11




                  11






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53525532%2fcant-access-cell-data-in-datagridview-vb-net%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

                      Calculate evaluation metrics using cross_val_predict sklearn

                      Insert data from modal to MySQL (multiple modal on website)