Results 1 to 10 of 10

Thread: Cell value of First colum & last row in datagridview

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2021
    Posts
    11

    Cell value of First colum & last row in datagridview

    Hello All,

    I trained myself pretty well in visual basic, started vb.net recently ... there are a few new things in it for me. So far, Iíve managed to solve everything, but the next thing doesnít work out but it seems pretty simple.
    I want to read the value (number) of the last cell in the first column of a datagridview.

    My code:

    Code:
    Public Class Material_Group
    Public cellvalue As String
    
    Public Sub read_last_numbering()
    
                    cellvalue = DataGridView1.Rows(DataGridView_MatGroup.Rows.Count)...?
        End Sub
    
    End Class
    Thank you very much if anyone solves this mystery.

    rgds,
    P.

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    107,727

    Re: Cell value of First colum & last row in datagridview

    Firstly, it's good that you provided your code but you also need to describe the actual problem. Saying that something doesn't work is generally of little value. You need to specify exactly what you expect and what actually happens. If there's a compilation error or run-time exception, give us the error message and point out where it occurs. Sometimes we'll be able to work it out and sometimes we won't. Sometimes we might think we've worked it out when your issue is something else entirely. If you just give us all the information in the first place, there's no guessing required.

  3. #3
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    107,727

    Re: Cell value of First colum & last row in datagridview

    I see two potential issues in the code you posted. Firstly, why are you using the row count from one grid to index the Rows collection of a different grid? Maybe there's a legitimate reason for that but, if so, that is something you should have included in your explanation. Secondly, assuming that you should be using the same grid in both cases, the Rows collection is, like pretty much every other collection, zero-based. That means that the index of the last item is one less than the count of the items in the list. I suspect that this:
    vb.net Code:
    1. DataGridView1.Rows(DataGridView_MatGroup.Rows.Count)
    should be this:
    vb.net Code:
    1. DataGridView1.Rows(DataGridView1.Rows.Count - 1)
    That expression will return a DataGridViewRow. If you want to know how to get a cell from a row, read the relevant documentation. You should have the Docs home page favourited in your browser but you can also go directly to the page for a type or member by clicking on that type or member in the code window and pressing F1. Context-sensitive Help has been a thing in Windows for a long time. Once you have the cell, you can read the relevant documentation to see how to get the value stored in that cell.

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    24,376

    Re: Cell value of First colum & last row in datagridview

    Quote Originally Posted by jmcilhinney View Post
    vb.net Code:
    1. DataGridView1.Rows(DataGridView1.Rows.Count - 1)
    That will work fine, unless youíre allowing users to add new rows, and the new row is at the last row of the dgv

    vb.net Code:
    1. DataGridView1.Rows(DataGridView1.Rows.Count - 2).Cells(0).Value

  5. #5
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,629

    Re: Cell value of First colum & last row in datagridview

    An alternative is to load the DataGridView by setting the DataSource be it a DataTable or a list. Let's say columns are EmployeeID, LastName, FirstName, Country in a DataTable and populated from a database table (could be from a text file, excel etc).

    • Read data into a DataTable
    • Assign the DataTable to a BindingSource
    • Assign the BindingSource to the DataSource of the DataGridView
    • Present data



    In a button click event check if there is a current row, if so, cast Current property of the BindingSource to a DataRow and get data. In this case the DataGridView column names are the same as the columns in the DataTable, alternately you can create columns for the DataGridView in code, set data property for each column to a column from the DataTable then set use the code below.

    Code:
    Public Class Form1
        Private EmployeeBindingSource as New BindingSource
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            EmployeeBindingSource.DataSource = AccessOperations.ReadEmployees()
            DataGridView1.DataSource = EmployeeBindingSource
    
        End Sub
    
        Private Sub CurrentRowButton_Click(sender As Object, e As EventArgs) Handles CurrentRowButton.Click
            if EmployeeBindingSource.Current IsNot nothing
                Dim row = Ctype(EmployeeBindingSource.Current, DataRowView).Row
    
                Dim id = row.Field(of Integer)("EmployeeID")
                dim FirstName = row.Field(Of string)("FirstName")
                dim LastName = row.Field(Of string)("LastName")
                dim Country = row.Field(Of string)("Country")
    
                MessageBox.Show($"{id},{FirstName},{LastName},{Country}")
                
            End If
        End Sub
    End Class
    Using the above methods means never having to work with cells which is the better option 99 percent of the time.

    Now to get the last row column values

    Code:
    Private Sub LastRowButton_Click(sender As Object, e As EventArgs) Handles LastRowButton.Click
        Dim row = Ctype(EmployeeBindingSource(EmployeeBindingSource.Count -1),DataRowView).Row
        Dim id = row.Field(of Integer)("EmployeeID")
        dim FirstName = row.Field(Of string)("FirstName")
        dim LastName = row.Field(Of string)("LastName")
        dim Country = row.Field(Of string)("Country")
    
        MessageBox.Show($"{id},{FirstName},{LastName},{Country}")
    
    End Sub
    Or

    Code:
    Private Sub LastRowButton_Click(sender As Object, e As EventArgs) Handles LastRowButton.Click
        Dim row = Ctype(EmployeeBindingSource(DataGridView1.Rows.Count -1),DataRowView).Row
        Dim id = row.Field(of Integer)("EmployeeID")
        dim FirstName = row.Field(Of string)("FirstName")
        dim LastName = row.Field(Of string)("LastName")
        dim Country = row.Field(Of string)("Country")
    
        MessageBox.Show($"{id},{FirstName},{LastName},{Country}")
    
    End Sub

  6. #6

    Thread Starter
    New Member
    Join Date
    Oct 2021
    Posts
    11

    Re: Cell value of First colum & last row in datagridview

    Thanks a lot, a I solved it in the same way.

  7. #7

    Thread Starter
    New Member
    Join Date
    Oct 2021
    Posts
    11

    Re: Cell value of First colum & last row in datagridview

    My expectation was clear and very simple.
    I had a deficiency in the syntax, it made no sense to talk about an error message. The the two different grid were a mistake when I entered the code.

  8. #8
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    107,727

    Re: Cell value of First colum & last row in datagridview

    Quote Originally Posted by peter.lenkei View Post
    it made no sense to talk about an error message
    That might explain why I said "IF" there's an error. If there's no compilation error or run-time exception then it must be the behaviour that indicates that something is wrong, so describe that behaviour. Just provide all the relevant information, whatever that might be. If you expect one thing to happen and something else happens, describe both those things. Pretty simple stuff. Instead of trying to make out that there was nothing wrong with your question, just accept the constructive criticism and move on. If you do better in future then you maximise the possibility of getting the help you want, which you should consider a good thing. I have a computer science degree, I have been a professional developer for over two decades and I've been posting on this site for over sixteen years. That doesn't mean that I know everything, by any means, but it does mean that I have a fairly good idea of how to write a good question on a forum. Do you really think you know better? It's not about ego. It's about everyone getting the best out of the system. We don't have to waste our time or yours trying to work out the details that you already know, so you get the best help as quickly as possible. It's a win-win.

  9. #9

    Thread Starter
    New Member
    Join Date
    Oct 2021
    Posts
    11

    Re: Cell value of First colum & last row in datagridview

    I understand what you mean but we talking about 2 different things.
    Once again: there was no relevant error message or unexpected behavior because my code wasn't complete beceuse I don't know the exact syntax.
    Iím sure I donít know better than you and no, itís not about EGO at all. At least not about mine. Maybe my question was wrong, but there were those who understood the problem exactly and provided a solution. I can accept the constructive (or not constructive) criticism but that was not it.

    Have a good day.
    Last edited by peter.lenkei; Oct 17th, 2021 at 04:39 AM.

  10. #10

    Thread Starter
    New Member
    Join Date
    Oct 2021
    Posts
    11

    Re: Cell value of First colum & last row in datagridview

    Quote Originally Posted by .paul. View Post
    That will work fine, unless youíre allowing users to add new rows, and the new row is at the last row of the dgv

    vb.net Code:
    1. DataGridView1.Rows(DataGridView1.Rows.Count - 2).Cells(0).Value
    There is an MSSSQL database behind the datagrid, users can add a new line via another form which writes the data directly to the SQL databse. In the new form, I handled updating datagrid from the SQL database after adding a new row. Direct input to datagrid is not allowed.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width