dcsimg
Results 1 to 6 of 6

Thread: How do I get the DataGridView to add a new row when records are more than col count?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2011
    Posts
    173

    How do I get the DataGridView to add a new row when records are more than col count?

    Hi guys,
    I have a DataGridView with 5 columns:
    Code:
            DataGridView1.ColumnCount = 5
            DataGridView1.Columns(0).Name = "Mon"
            DataGridView1.Columns(1).Name = "Tue"
            DataGridView1.Columns(2).Name = "Wed"
            DataGridView1.Columns(3).Name = "Thu"
            DataGridView1.Columns(4).Name = "Fri"
    I'm getting a set of strings from my database:
    Code:
    DataGridView1.Rows.Add(New String() {"", "", "2,4,8", "15,5,3", "4,7,9", "sdfsdgf", "sad555", "", "2,4,8", "15,5,3", "4,7,9", "sdfsdgf", "sad555"})
    But since I have only 5 columns, only the first 5 records are showing ({"", "", "2,4,8", "15,5,3", "4,7,9"), the rest are just gone (, "sdfsdgf", "sad555", "", "2,4,8", "15,5,3", "4,7,9", "sdfsdgf", "sad555"}).
    How do I get the DataGridView to add a new row when the records are more than the column count?

    Thanks

  2. #2
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    5,546

    Re: How do I get the DataGridView to add a new row when records are more than col cou

    Out of the back of my head.
    I would do a count on the new values I need (p.e. String().count or osmething) and then a loop on each value and doing an row.add
    So not adding them directly like you do but with a lopp.

    Code:
    For each strvalue in string()
    .... Row.add(strvalue)
    ...
    ...
    Edit.
    Something else that I may misunderstood.
    If you need to add extra columns if the values are more than 5 then you also need to add the missing columns into the loop.
    Last edited by sapator; May 21st, 2019 at 09:30 AM.
    Slow as hell.

  3. #3
    Fanatic Member kpmc's Avatar
    Join Date
    Sep 2017
    Posts
    1,013

    Re: How do I get the DataGridView to add a new row when records are more than col cou

    the question is, what do you want to do with the string elements that do not have a corresponding column?

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    May 2011
    Posts
    173

    Re: How do I get the DataGridView to add a new row when records are more than col cou

    Quote Originally Posted by sapator View Post
    Out of the back of my head.
    I would do a count on the new values I need (p.e. String().count or osmething) and then a loop on each value and doing an row.add
    So not adding them directly like you do but with a lopp.

    Code:
    For each strvalue in string()
    .... Row.add(strvalue)
    ...
    ...
    Edit.
    Something else that I may misunderstood.
    If you need to add extra columns if the values are more than 5 then you also need to add the missing columns into the loop.
    If I loop through them like this:
    Code:
            Dim test As String() = {"", "", "05/12/19,4,8,2", "15,5,3", "4,7,9", "sdfsdgf", "sad555", "", "2,4,8", "15,5,3", "4,7,9", "sdfsdgf", "sad555"}
            For Each str As String In test
                DataGridView1.Rows.Add(test)
            Next
    I get the first 5 strings in each row...

    Quote Originally Posted by kpmc View Post
    the question is, what do you want to do with the string elements that do not have a corresponding column?
    I'm building a calendar, so I have 5 columns (Mon - Fri)
    I'm getting 30 strings from the database (1 for each day), each string containing date and 3 sets of numbers ("mm/dd,1,2,3")
    So, when the strings reach the end of the row (last column - Fri), they need to continue to the next row (first column - Mon)

  5. #5
    Fanatic Member kpmc's Avatar
    Join Date
    Sep 2017
    Posts
    1,013

    Re: How do I get the DataGridView to add a new row when records are more than col cou

    You will find that the .DayOfWeek property can help you do what youre trying to do

  6. #6
    Frenzied Member ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    1,986

    Re: How do I get the DataGridView to add a new row when records are more than col cou

    I think you should start with getting the Dates in the DGV, then all you need todo
    is load the Data from the Database to the Columns

    you select the Calendar week from the Combobox

    here an Image
    Name:  calweek.jpg
Views: 23
Size:  20.9 KB

    the code in the Form

    Code:
     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
          
            Dim StartDate As Date = GetCalendarWeek(CInt(cboCalWeek.Text), CInt(TextBox1.Text))
            Dim EndDate As Date = StartDate.AddDays(6)
            Dim CurrentDate As Date = StartDate
            Dim datevalue As Date = CurrentDate
    
            While (CurrentDate <= EndDate)
                DataGridView1.Columns.Add(CurrentDate.ToShortDateString, CurrentDate.ToString("dddd dd.MM.yyyy"))
                CurrentDate = CurrentDate.AddDays(1)
    
            End While
            With DataGridView1
                For i = 0 To 6
                    .Columns(i).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
                Next
            End With
            DataGridView1.Rows.Add(5)
        End Sub
    
        Function GetWeekfromDate(ByVal Datum As Date) As Byte
            Return CByte(DatePart(DateInterval.WeekOfYear, _
            Datum, FirstDayOfWeek.System, _
            FirstWeekOfYear.System))
        End Function
    
        Public Function GetCalendarWeek(ByVal nWeek As Integer, _
          ByVal nYear As Integer) As Date
            ' Wochentag des 4. Januar des Jahres ermitteln
            Dim dStart As New Date(nYear, 1, 4)
            Dim nDay As Integer = (dStart.DayOfWeek + 6) Mod 7 + 1
            ' Beginn der 1. KW des Jahres
            Dim dFirst As Date = dStart.AddDays(1 - nDay)
            ' Gesuchte KW ermitteln
            Return dFirst.AddDays((nWeek - 1) * 7)
        End Function
    
        Public Function WeekCount(ByVal nYear As Integer) As Integer
            Return GetCalendarWeek(1, nYear + 1).Subtract(GetCalendarWeek(1, nYear)).Days \ 7
        End Function
    
        Private Sub Form5_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Label1.Text = CStr(CDate(Now()))
            TextBox1.Text = "2019"
    
            'get the current week  from Today
            cboCalWeek.Text = CStr(GetWeekfromDate(CDate(Label1.Text)))
    
            'get the weeks from Year
            For i = 1 To WeekCount(CInt(TextBox1.Text))
                cboCalWeek.Items.Add(i)
            Next
    
    
            Button1.PerformClick()
        End Sub
    
        Private Sub cboCalWeek_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboCalWeek.SelectedIndexChanged
            DataGridView1.DataSource = Nothing
            DataGridView1.DataMember = String.Empty
            DataGridView1.Columns.Clear()
    
            'change the Dates in DGV Header based on selected Cal-Week
            Dim dStart As Date = GetCalendarWeek(CInt(cboCalWeek.Text), CInt(TextBox1.Text))
          
            Label1.Text = CStr(dStart)
           
            Button1.PerformClick()
        End Sub

    should give you a start

    HTH
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width