Results 1 to 3 of 3

Thread: datagrid

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2002
    Posts
    5

    datagrid

    Hi All,

    question: i need a way of viewing info from a database like the datagrid and i want the user to double click a line ad it then opens a form with the info and the user can then change the info, but with the datagrid you can cick on each cell and i dont want that i wnt that the whole line is selected when clicked on

    many thanks

    alex t

  2. #2
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    518
    You may want to try to teach the ListView control to talk to your datasource. I don't like DataGrid's behavior either so that's what I did. Here's a little sample code to get you started:

    Code:
        Private Sub DoItemListView()
            Dim lvwColumn As ColumnHeader
            Dim itmListItem As ListViewItem
            Dim shtCntr As Short
            Dim objRow As DataRow
    
            lvwItems.Clear()
            lvwItems.BeginUpdate()
            'Loop through the columns and make a column header for each item
            Try
                For shtCntr = 0 To dsGurpsData.Tables("Advantage").Columns.Count - 1
                    lvwColumn = New ColumnHeader()
                    'Create Column Header
                    lvwColumn.Text = dsGurpsData.Tables("Advantage").Columns.Item(shtCntr).ColumnName.ToString
                    lvwItems.Columns.Add(lvwColumn)
                Next
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
    
            lvwItems.Columns(0).Width = 200
    
    
            'Populate the listview items
            Dim objGetItems() As DataRow
            If mnuViewAll.Checked = True Then
                If lvwCategories.SelectedItems.Count > 0 Then
                    'Check to see if apostrophe (') is present in string
                    If lvwCategories.SelectedItems(0).Text.IndexOf("'") > -1 Then
                        Dim strTemp As String
                        strTemp = Replace(lvwCategories.SelectedItems(0).Text, "'", "''")
                        For Each objRow In dsGurpsData.Tables("Advantage").Select("Category = '" + _
                        strTemp + "'")
                            itmListItem = New ListViewItem()
                            itmListItem.Text = objRow.ItemArray(0)
                            For shtCntr = 1 To objRow.ItemArray.GetUpperBound(0)
                                itmListItem.SubItems.Add(objRow.ItemArray.GetValue(shtCntr))
                            Next
                            lvwItems.Items.Add(itmListItem)
                        Next
                    Else
                        For Each objRow In dsGurpsData.Tables("Advantage").Select("Category = '" + _
                        lvwCategories.SelectedItems.Item(0).Text + "'")
                            itmListItem = New ListViewItem()
                            itmListItem.Text = objRow.ItemArray(0)
                            For shtCntr = 1 To objRow.ItemArray.GetUpperBound(0)
                                itmListItem.SubItems.Add(objRow.ItemArray.GetValue(shtCntr))
                            Next
                            lvwItems.Items.Add(itmListItem)
                        Next
                    End If
                End If
            ElseIf mnuViewSelected.Checked = True Then
                For Each objRow In dsGurpsData.Tables("Advantage").Select("LevelsTaken > 0")
                    itmListItem = New ListViewItem()
                    itmListItem.Text = objRow.ItemArray(0)
                    For shtCntr = 1 To objRow.ItemArray.GetUpperBound(0)
                        itmListItem.SubItems.Add(objRow.ItemArray.GetValue(shtCntr))
                    Next
                    lvwItems.Items.Add(itmListItem)
                Next
            End If
            lvwItems.EndUpdate()
        End Sub
    you'll need to rename some strings to get it to work with your datasource but the hard part (for me anyhow) was figuring out how to populate the listview from a dataset table. Then you'll want an eventhandler for listview.doubleclick, like so:

    Code:
        Private Sub lvwItems_DoubleClick(ByVal sender As Object, _
        ByVal e As System.EventArgs) Handles lvwItems.DoubleClick
            If lvwItems.Items.Count > 0 And lvwItems.SelectedItems.Count > 0 Then
                DoIncrementAdvantage(1)
            End If
        End Sub
    
        Private Sub DoIncrementField(ByVal iIncrementBy As Integer)
            Dim objRow As DataRow
    
            'Update the table
            objRow = dsGurpsData.Tables("Advantage").Rows.Find(lvwItems.SelectedItems(0).Text)
            objRow.BeginEdit()
            objRow("LevelsTaken") += iIncrementBy
            objRow.EndEdit()
            objRow.AcceptChanges()
    
            'Update the listview
            lvwItems.SelectedItems(0).SubItems(3).Text = objRow("LevelsTaken")
        End Sub
    Remember to set the .MultiSelect property of the listview to False and you should be OK. I use this exact code in a program I'm working on and I'm pretty happy with the way it works.

    edit: well actually not this EXACT code as I've changed it in some ways that makes it less clear for what I think you're looking for but this worked too at one point (thanks SourceSafe).

  3. #3

    Thread Starter
    New Member
    Join Date
    Sep 2002
    Posts
    5
    thanks for the info, will try tomorrow morning when i am back at my own desk

    cheers

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