Results 1 to 16 of 16

Thread: questions about datagridview

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2008
    Location
    Singapore
    Posts
    151

    questions about datagridview

    i'm currently using vb 2005 express edition and sql server 2000 and i've manage to get the data out from the server and place them into a datagridview.

    so here's the problems that i'm facing. is it possible that when the user click on a certain cells in the datagridview, that ROW of cells get highlighted? And also if the user were to double-click on a certain cells, all the values from that ROW appear on a messagebox?

    thanks,

    ron

    Previous Thread.
    http://www.vbforums.com/showthread.php?t=538670

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: questions about datagridview

    1. Set the SelectionMode property of the grid to FullRowSelect.

    2. You can loop through the values in the cells like this:
    vb.net Code:
    1. For Each cell As myDataGridView.SelectedCells
    2.     MessageBox.Show(cell.Value.ToString())
    3. Next
    That will show each cell value individually. If you want to show them altogether then you need to get each value and join it into a single larger string, then call MessageBox.Show once only and pass that string. I'll leave that to you as an exercise.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Sep 2008
    Location
    Singapore
    Posts
    151

    Re: questions about datagridview

    Quote Originally Posted by jmcilhinney
    For Each cell As myDataGridView.SelectedCells
    not a correct syntax. tried changing "myDataGridView" to "DataGridView" and also to the name of my datagridview but can't.
    Last edited by sphericalx; Sep 8th, 2008 at 10:32 PM.
    Thanks,

    Ron

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Sep 2008
    Location
    Singapore
    Posts
    151

    Re: questions about datagridview

    got it working alr. below is my attached code ^^


    Dim cells As DataGridViewCell
    For Each cells In DataGridView1.SelectedCells
    MessageBox.Show(cells.Value.ToString)
    Next
    Thanks,

    Ron

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: questions about datagridview

    Ah, my apologies. That's what happens when i don't have an IDE to check my typing. My original code should have been:
    vb.net Code:
    1. For Each cell As DataGridViewCell In myDataGridView.SelectedCells
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Sep 2008
    Location
    Singapore
    Posts
    151

    Re: questions about datagridview

    haha.. its ok.. it gave me room to explore on how to get it done. haha.. ^^
    Thanks,

    Ron

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Sep 2008
    Location
    Singapore
    Posts
    151

    Re: questions about datagridview

    below is the code on how i retrieve the data from the database and place it into a datagridview? however, is it possible for mi to do some changes to the data before filling it into the dataviewgrid?

    Code:
    SQLstr = "SELECT ID 'Product ID', Name 'Description',Size,Make,StockCount 'Stock Count',WholeSale,Retail FROM ProductsAndServices where PS='P'"
                        mycon = New SqlConnection(str_connection)
                        comUserSelect = New SqlCommand(SQLstr, mycon)
                        Dim dataadapter As SqlDataAdapter = New SqlDataAdapter(comUserSelect)
                        Dim ds As DataSet = New DataSet()
                        mycon.Open()
                        dataadapter.Fill(ds, "Product_table")
                        mycon.Close()
                        My.Forms.Main1.DataGridView1.DataSource = ds
                        My.Forms.Main1.DataGridView1.DataMember = "Product_table"
                        Me.Close()
    Thanks,

    Ron

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: questions about datagridview

    Sure. You can get a DataTable from a DataSet by indexing its Tables property by table name or number. You can get a DataRow from a DataTable by indexing its Rows property. You can get or set a field in a row by indexing it by column name or number.

    That said, what exactly is it that you wanted to do? It might be something that the SQL code can take care of for you.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Sep 2008
    Location
    Singapore
    Posts
    151

    Re: questions about datagridview

    err.. do u have coding exmples for references?

    i have a smallmoney datatype value. but as u know, when u retrieve the smallmoney out from the database it will be something like this-> 8.0000

    however i wan it to display in the datagridview as $8.00 therefore i'm tinking of editing it before i place it into the datagridview
    Thanks,

    Ron

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: questions about datagridview

    Then it's a good thing I asked what you wanted to do, because there's no need of any editing. You should just be binding your data to the grid as is and letting the grid worry about formatting. Each column has a DefaultCellStyle property that controls the default style for cells in that column. That has a Format property that accepts a format string, which you can use to format numbers or dates in numerous ways. The format string for currency is "c", assuming that you want to use the currency format of the local system. So, you need to set the DefaultCellStyle.Format property of your grid column to "c", which you can do in the designer or in code.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Sep 2008
    Location
    Singapore
    Posts
    151

    Re: questions about datagridview

    oh really? hmm..but do u have any code or source which i can do a reference from?
    Thanks,

    Ron

  12. #12

  13. #13

    Thread Starter
    Addicted Member
    Join Date
    Sep 2008
    Location
    Singapore
    Posts
    151

    Re: questions about datagridview

    Q1: if i had pre-added all the column header. how is it possible that when i doubleclick on the empty cell in "ID" column, a form dialog appear? whereas if i doubleclick on any of the other column, nothing will happen.
    _______________________________________________________
    |Employee |StartTime |ID |Mins |Type |Description |Price |Total |
    |________|_________|__|____|_____|_________|____|_____|

    Q2: and how can i add values into specify cells?
    For Example:

    Add '200' into ID, '3' into Mins, 'P' into Type, 'Body Massage' into Description, '$20.00' into Price. If first row is empty, it will be added into the specific cell in row 0. But if Row 1 is alr filled, it will be placed into row 1 instead.

    |Employee |StartTime |ID |Mins |Type |Description |Price |Total |
    |________|_________|___|_____|______|__________|_____|______|
    Last edited by sphericalx; Oct 1st, 2008 at 09:06 PM.
    Thanks,

    Ron

  14. #14
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: questions about datagridview

    Please ask new questions in new threads. One thread per topic and one topic per thread.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  15. #15

    Thread Starter
    Addicted Member
    Join Date
    Sep 2008
    Location
    Singapore
    Posts
    151

    Re: questions about datagridview

    Quote Originally Posted by jmcilhinney
    Please ask new questions in new threads. One thread per topic and one topic per thread.
    but the topic is generally about datagridview.
    Thanks,

    Ron

  16. #16
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: questions about datagridview

    Quote Originally Posted by sphericalx
    but the topic is generally about datagridview.
    Well the topic should NOT generally be about the DataGridView. You could ask a thousand different questions about the DataGridView because it's such a complex control. Will they all be asked in this one thread? Formatting currency in a DataGridView is one topic and belongs in its own thread. Your latest questions have nothing to do with formatting currency in a DataGridView so they are new topics and belong in new threads.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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