Results 1 to 8 of 8

Thread: DGV Styles

  1. #1

    Thread Starter
    Hyperactive Member mbutler755's Avatar
    Join Date
    May 2008
    Location
    Peoria, AZ
    Posts
    417

    Lightbulb DGV Styles

    I have a DGV that contains three columns: StoreID, DateStamp and GUID. I want to highlight duplicate or triplicate data if the StoreID exists more than once on the same DateStamp.

    I have done some searching and I have found some ways to highlight columns or rows based on a certain value, but my needs require me to know if a StoreID exists more than once on the same day.

    Any thoughts on how to program this would be much appreciated.
    Regards,

    Matt Butler, MBA, BSIT/SE, MCBP
    Owner, Intense IT, LLC
    Find us on Facebook
    Follow us on Twitter
    Link up on LinkedIn
    mb (at) i2t.us

    CODE BANK SUBMISSIONS: Converting Images to Base64 and Back Again

  2. #2
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: DGV Styles

    I'm assuming the data is bound correct? Here is a way to do it with LINQ, although it might be better to create a stored procedure to do this, especially with a large dataset.

    vb.net Code:
    1. Public Class Form1
    2.  
    3.     Private Sub Button1_Click(ByVal sender As System.Object, _
    4.                               ByVal e As System.EventArgs) _
    5.                               Handles Button1.Click
    6.  
    7.         '//sample datasource
    8.         Dim dt = New DataTable()
    9.         With dt.Columns
    10.             '//add columns
    11.             .Add("StoreId", GetType(Integer))
    12.             .Add("DateStamp", GetType(Date))
    13.             .Add("Guid", GetType(Guid))
    14.         End With
    15.         With dt.Rows
    16.             '//add rows
    17.             .Add(1, Date.Now.Date, Guid.NewGuid())
    18.             .Add(1, Date.Now.Date, Guid.NewGuid())
    19.             .Add(2, Date.Now.AddDays(1.0R).Date, Guid.NewGuid())
    20.             .Add(3, Date.Now.AddDays(2.0R).Date, Guid.NewGuid())
    21.             .Add(2, Date.Now.AddDays(1.0R).Date, Guid.NewGuid())
    22.             .Add(1, Date.Now.AddDays(1.0R).Date, Guid.NewGuid())
    23.         End With
    24.  
    25.         '//set datasource
    26.         Me.DataGridView1.DataSource = dt
    27.  
    28.         '//highlighting code
    29.         Dim query = dt.AsEnumerable() _
    30.                       .GroupBy(Function(key) _
    31.                                    New With {Key .StoreId = _
    32.                                                 key.Field(Of Integer)("StoreId"), _
    33.                                              Key .DateStamp = _
    34.                                                 key.Field(Of Date)("DateStamp")}) _
    35.                       .Where(Function(g) g.Count() > 1) _
    36.                       .Select(Function(g) g.Key)
    37.  
    38.         '//Sql Translation
    39.         '//  SELECT dt.StoreId, dt.DateStamp
    40.         '//  FROM(dt)
    41.         '//  GROUP BY dt.StoreId, dt.DateStamp
    42.         '//  HAVING (((Count(dt.DateStamp))>1));
    43.  
    44.         For Each row As DataGridViewRow In Me.DataGridView1.Rows
    45.             For Each anon In query
    46.                 Dim storeId = row.Cells("StoreId").Value
    47.                 Dim dateStamp = row.Cells("DateStamp").Value
    48.                 If Object.Equals(storeId, anon.StoreId) AndAlso _
    49.                    Object.Equals(dateStamp, anon.DateStamp) Then
    50.                     '//row is a duplicate
    51.                     row.DefaultCellStyle.BackColor = Color.Red
    52.                 End If
    53.             Next
    54.         Next
    55.  
    56.     End Sub
    57.  
    58. End Class

  3. #3
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: DGV Styles

    What is the backend? SQL Server (2008) could work like this

    Select StoreID,DateStamp,GUID,
    ROW_NUMBER() OVER (PARTITION BY StoreID,DateStamp ORDER BY StoreID,DateStamp) AS MyRowNum
    From TableName

    You can hide that last column and then base the highlighting on that column being > 1
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  4. #4

    Thread Starter
    Hyperactive Member mbutler755's Avatar
    Join Date
    May 2008
    Location
    Peoria, AZ
    Posts
    417

    Re: DGV Styles

    Quote Originally Posted by GaryMazzone View Post
    What is the backend?
    MS SQL Server 2005 (I can't get them to upgrade).
    Regards,

    Matt Butler, MBA, BSIT/SE, MCBP
    Owner, Intense IT, LLC
    Find us on Facebook
    Follow us on Twitter
    Link up on LinkedIn
    mb (at) i2t.us

    CODE BANK SUBMISSIONS: Converting Images to Base64 and Back Again

  5. #5
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: DGV Styles

    That might work in 2005 also (at least I believe that ROW_NUMBER() was included in that release)
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  6. #6

    Thread Starter
    Hyperactive Member mbutler755's Avatar
    Join Date
    May 2008
    Location
    Peoria, AZ
    Posts
    417

    Re: DGV Styles

    Quote Originally Posted by ForumAccount View Post
    I'm assuming the data is bound correct? Here is a way to do it with LINQ, although it might be better to create a stored procedure to do this, especially with a large dataset.

    vb.net Code:
    1. Public Class Form1
    2.  
    3.     Private Sub Button1_Click(ByVal sender As System.Object, _
    4.                               ByVal e As System.EventArgs) _
    5.                               Handles Button1.Click
    6.  
    7.         '//sample datasource
    8.         Dim dt = New DataTable()
    9.         With dt.Columns
    10.             '//add columns
    11.             .Add("StoreId", GetType(Integer))
    12.             .Add("DateStamp", GetType(Date))
    13.             .Add("Guid", GetType(Guid))
    14.         End With
    15.         With dt.Rows
    16.             '//add rows
    17.             .Add(1, Date.Now.Date, Guid.NewGuid())
    18.             .Add(1, Date.Now.Date, Guid.NewGuid())
    19.             .Add(2, Date.Now.AddDays(1.0R).Date, Guid.NewGuid())
    20.             .Add(3, Date.Now.AddDays(2.0R).Date, Guid.NewGuid())
    21.             .Add(2, Date.Now.AddDays(1.0R).Date, Guid.NewGuid())
    22.             .Add(1, Date.Now.AddDays(1.0R).Date, Guid.NewGuid())
    23.         End With
    24.  
    25.         '//set datasource
    26.         Me.DataGridView1.DataSource = dt
    27.  
    28.         '//highlighting code
    29.         Dim query = dt.AsEnumerable() _
    30.                       .GroupBy(Function(key) _
    31.                                    New With {Key .StoreId = _
    32.                                                 key.Field(Of Integer)("StoreId"), _
    33.                                              Key .DateStamp = _
    34.                                                 key.Field(Of Date)("DateStamp")}) _
    35.                       .Where(Function(g) g.Count() > 1) _
    36.                       .Select(Function(g) g.Key)
    37.  
    38.         '//Sql Translation
    39.         '//  SELECT dt.StoreId, dt.DateStamp
    40.         '//  FROM(dt)
    41.         '//  GROUP BY dt.StoreId, dt.DateStamp
    42.         '//  HAVING (((Count(dt.DateStamp))>1));
    43.  
    44.         For Each row As DataGridViewRow In Me.DataGridView1.Rows
    45.             For Each anon In query
    46.                 Dim storeId = row.Cells("StoreId").Value
    47.                 Dim dateStamp = row.Cells("DateStamp").Value
    48.                 If Object.Equals(storeId, anon.StoreId) AndAlso _
    49.                    Object.Equals(dateStamp, anon.DateStamp) Then
    50.                     '//row is a duplicate
    51.                     row.DefaultCellStyle.BackColor = Color.Red
    52.                 End If
    53.             Next
    54.         Next
    55.  
    56.     End Sub
    57.  
    58. End Class
    Thanks for the code. I think there is something wrong with the way that I am declaring the dataTable. This is what I have thus far:

    Code:
    SEE BELOW FOR UP-TO-DATE CODE
    Right now, my dgv is completely blank. If I comment out the code, I get my entire table (as normal).
    Last edited by mbutler755; Jan 20th, 2011 at 01:35 PM. Reason: changed code
    Regards,

    Matt Butler, MBA, BSIT/SE, MCBP
    Owner, Intense IT, LLC
    Find us on Facebook
    Follow us on Twitter
    Link up on LinkedIn
    mb (at) i2t.us

    CODE BANK SUBMISSIONS: Converting Images to Base64 and Back Again

  7. #7

    Thread Starter
    Hyperactive Member mbutler755's Avatar
    Join Date
    May 2008
    Location
    Peoria, AZ
    Posts
    417

    Re: DGV Styles

    OK, everything is blank because I am creating a new dataTable and assigning it to the dgv. I think it needs to look something closer to this, but AsEnumberable is not available on the BindingSource.

    Code:
            'TODO: This line of code loads data into the 'AlohadbDataSet.BriadBackup' table. You can move, or remove it, as needed.
            Me.BriadBackupTableAdapter.FillTodaysBackups(Me.AlohadbDataSet.BriadBackup)
            Label4.Text = Me.BriadBackupTableAdapter.getNumberOfBackedUpStores
            Label5.Text = Me.BriadBackupTableAdapter.getNumberOfUniqueStores
    
            '   HIGHLIGHTING CODE
            '
            Dim dt = BriadBackupBindingSource
            Me.DataGridView1.DataSource = dt
            Dim query = dt.AsEnumerable() _
                      .GroupBy(Function(key) _
                                   New With {Key .StoreID = _
                                       key.Field(Of Integer)("StoreID"), _
                                            Key .DateStamp = _
                                            key.Field(Of Date)("DateStamp")}) _
                                    .Where(Function(g) g.Count() > 1) _
                                    .Select(Function(g) g.Key)
    
            For Each row As DataGridViewRow In Me.DataGridView1.Rows
                For Each anon In query
                    Dim StoreID = row.Cells("StoreID").Value
                    Dim DateStamp = row.Cells("DateStamp").Value
                    If Object.Equals(StoreID, anon.StoreID) AndAlso _
                        Object.Equals(DateStamp, anon.DateStamp) Then
                        row.DefaultCellStyle.BackColor = Color.Red
                        row.DefaultCellStyle.ForeColor = Color.White
    
                    End If
                Next
            Next
    Regards,

    Matt Butler, MBA, BSIT/SE, MCBP
    Owner, Intense IT, LLC
    Find us on Facebook
    Follow us on Twitter
    Link up on LinkedIn
    mb (at) i2t.us

    CODE BANK SUBMISSIONS: Converting Images to Base64 and Back Again

  8. #8

    Thread Starter
    Hyperactive Member mbutler755's Avatar
    Join Date
    May 2008
    Location
    Peoria, AZ
    Posts
    417

    Re: DGV Styles

    Most up-to-date:

    Code:
            Dim dt = New DataTable()
            ' Me.DataGridView1.DataSource = dt
            Dim query = dt.AsEnumerable() _
                      .GroupBy(Function(key) _
                                   New With {Key .StoreID = _
                                       key.Field(Of Integer)("StoreID"), _
                                            Key .DateStamp = _
                                            key.Field(Of Date)("DateStamp")}) _
                                    .Where(Function(g) g.Count() > 1) _
                                    .Select(Function(g) g.Key)
    
            For Each row As DataGridViewRow In Me.DataGridView1.Rows
                For Each anon In query
                    Dim StoreID = row.Cells("StoreID").Value
                    Dim DateStamp = row.Cells("DateStamp").Value
                    If Object.Equals(StoreID, anon.StoreID) AndAlso _
                        Object.Equals(DateStamp, anon.DateStamp) Then
                        row.DefaultCellStyle.BackColor = Color.Red
                        row.DefaultCellStyle.ForeColor = Color.White
    
                    End If
                Next
            Next
    Regards,

    Matt Butler, MBA, BSIT/SE, MCBP
    Owner, Intense IT, LLC
    Find us on Facebook
    Follow us on Twitter
    Link up on LinkedIn
    mb (at) i2t.us

    CODE BANK SUBMISSIONS: Converting Images to Base64 and Back Again

Tags for this Thread

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