?
Solved

How do I fill in a datagridview from code?

Posted on 2013-02-05
12
Medium Priority
?
259 Views
Last Modified: 2013-02-09
Eventually this datagridview will be filled in from a background sql query, but for now I am just experimenting with the datagridview.  When I push the button it should create the month worh of information.  When I run the program it fills in the date and then I get a error box for each column that has a combobox on it that says value assigned is an error. When I click the okay in places the first item in the row and proceds to the next combobox error.  I can fill in the entire table if I click okay enough times.
what is the value I am trying to assign to the column.  Tried item, name column etc nothing I try to assign when trying to build the row seems to work.
the first combo box has three words in it:  HERE, OUT OF OFFICE, WEEKEND.  The value would go into the sHrDet variable.

thanks  Gary

  Private Sub BTNCreateWage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BTNCreateWage.Click
        Module1.daysInMonth = System.DateTime.DaysInMonth(DTP1.Value.Year, DTP1.Value.Month)
        Dim ADDDATE As Date = Now
        Dim I As Integer
        Dim sDate As String
        Dim sHr, sPto, sHrDet, sNp, sPtoDet, sNpDet As Integer

        For I = 1 To Module1.daysInMonth
            sDate = I & "/" & Module1.HoldMonth & "/" & DTP1.Value.Year

            sHrDet = 1
            sHr = 8
            sNpDet = 1
            sNp = 0
            sPtoDet = 1
            sPto = 0
            DGV1.Rows.Add(New String() {sDate, sHrDet, sHr, sPtoDet, sPto, sNpDet, sNp})

        Next
    End Sub
End Class
0
Comment
Question by:javagair
  • 6
  • 4
  • 2
12 Comments
 
LVL 3

Expert Comment

by:Nalinkumarbalaji
ID: 38855259
Bind the data from the SQL itself. Find the below sample. This is using SQL.

Imports System.Data
Imports System.Data.SqlClient

Private Sub LoadGrid()

Dim ConnectionString_Local = "Persist Security Info=False;User ID=sa;pwd=sa; Initial Catalog=DatabaseName;Data Source=ServerName"
Dim SQLConn_Local As SqlClient.SqlConnection
SQLConn_Local = New SqlConnection(ConnectionString_Local)

Dim DAGrid As SqlDataAdapter

Dim DTGrid As New DataTable
DAGrid = New SqlDataAdapter("Select sHr, sPto, sHrDet, sNp, sPtoDet, sNpDet from YourTable", SQLConn_Local)

SQLConn_Local.Open()
DAGrid.Fill(DTGrid)
SQLConn_Local.Close()
DGResultSet.Visible = True
DGResultSet.DataSource = DTGrid
DGResultSet.DataBind()

End Sub

If you are using the Code to Bind the Data instead of SQL then you have to Create the Datatable, then define the Columns, And Import the Colums Using Datarow into to Datatable and then Select the Datasource for Grid and Bind the same.

Check the below link.
http://msdn.microsoft.com/en-in/library/system.data.datarow(v=vs.71).aspx
0
 
LVL 3

Expert Comment

by:Nalinkumarbalaji
ID: 38855354
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 38855636
'the first combo box has three words in it:  HERE, OUT OF OFFICE, WEEKEND'
Your First Combo is waiting for as string or int ? Is there a value member in your First comboBox ...?
Otherwise you have to assign a string as display member only
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:javagair
ID: 38856945
First off there is no database to use yet.
I was hoping to fill the table using some kind of index.
Like I said this is just an experiment I'm doing so I understand the intricacies of the control itself.
Something like if you are in column one use the third item for that combobox in this row.  I don't want to care what words are in the collection just that there are three of them and fill in with the second one.

thanks
gary
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 38856985
Try to set sHrDet="OUT OF OFFICE"
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 38857106
The way you try to add row is correct...But in this way you have to fill the cells in the format you declare...that is why i asked you if sHrDet is waiting a string or int...
if your combobox has 3 items and only that are strings ,set string to your variable.
Dim row0 As String() = {variable1, variable2, variable3, variable4, variable5}
        Me.RecordDatagridView.Rows.Add(row0)

Open in new window


John
0
 

Author Comment

by:javagair
ID: 38863789
the following code is equivalent to your last post.  The first string is accepted, the second string errors and the third string always chooses the first item in the collection regardless of what I assign here.
Since collections have a index; I still think there has to be a way of calling the index for the column and the index for the collection to get the value there.

thanks
gary

  Private Sub BTNCreateWage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BTNCreateWage.Click
        Module1.daysInMonth = System.DateTime.DaysInMonth(DTP1.Value.Year, DTP1.Value.Month)
        Dim ADDDATE As Date = Now
        Dim I As Integer
        Dim sDate, sNpDet, sHrDet, sPtoDet As String
        Dim sHr, sPto, sNp As Integer

        For I = 1 To Module1.daysInMonth
            sDate = I & "/" & Module1.HoldMonth & "/" & DTP1.Value.Year

            sHrDet = "WEEKEND"  'last item in combo collection
            sHr = 8                          'integer for hours
            sNpDet = "NON-PAID" 'last item in combo collection
            sNp = 0                          'integer for hours
            sPtoDet = "PTO"           'last item in this column's collecton
            sPto = 0                         'integer for hours
            DGV1.Rows.Add(New String() {sDate, sHrDet, sHr, sPtoDet, sPto, sNpDet, sNp})

        Next
    End Sub
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 38863998
Are you Storing these value in a database?
if no ...There is no need to use a DatagridViewCombobox...just insert the cell value in the desired format..
otherwise you can use not the index but the Value Memeber of the corresponding DatagridViewCombobox..

Set the appearance -> display style to nothing
The non Combobox columns at the default cell style set the desire format of the specific column ...
then while you inserting the row ,try the values to have the same format .
0
 
LVL 18

Accepted Solution

by:
John (Yiannis) Toutountzoglou earned 2000 total points
ID: 38864488
I can not see anything wrong ...
you pass an integer as time...it works for me try this...
 Me.DataGridView1.Columns(1).DefaultCellStyle.Format = "N0"
        Me.DataGridView1.Columns(3).DefaultCellStyle.Format = "N0"
        Me.DataGridView1.Columns(5).DefaultCellStyle.Format = "N0"
        Dim row0 As String() = {"WEEKEND", "8", "NON-PAID", "9", "PTO", "10"}
Me.DataGridView1.Rows.Add(row0)

Open in new window

Columns(1)->sHr
Columns(3)->sNp
Columns(5)->sPto

just include the integer with " "
0
 

Author Comment

by:javagair
ID: 38871393
this code works
not exactly sure why PTO started filling in but it does, added a null answer to the top of the collection.  Now I get the entire table to fill.

gary

Private Sub BTNCreateWage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BTNCreateWage.Click
    If LBLName.Text <> "NAME" Then

        Module1.daysInMonth = System.DateTime.DaysInMonth(DTP1.Value.Year, DTP1.Value.Month)
        Dim ADDDATE As Date = Now
        Dim I As Integer
        Dim sHrDet, sPtoDet, MyDate As String
        Dim sHr, sPto As Integer
        Dim sDate, checkDate As Date
        Dim check As Boolean
        Dim Holiday() As Object = {"20130101", "20130121", "20130218", "20130527", "20130704", "20130902", "20131014", "20131111", "20131128", "20131225"}
        sHrDet = ""
        sPtoDet = ""
        sPto = 0
        sHr = 0
        For I = 1 To Module1.daysInMonth
            ' MyDate = I & "/" & Module1.HoldMonth & "/" & DTP1.Value.Year
            MyDate = DTP1.Value.Year & String.Format("{0:0#}", Module1.HoldMonth) & String.Format("{0:0#}", I)
            sDate = Module1.GetDateYMMDD(MyDate)
            check = False
            For j = 0 To Holiday.Length - 1
                checkDate = Module1.GetDateYMMDD(Holiday(j))
                If sDate = checkDate Then
                    check = False
                    sHrDet = "OUT OF OFFICE"
                    sPto = 8
                    sHr = 0
                    sPtoDet = "PTO"           'last item in this column's collecton
                    Exit For
                Else
                    check = True
                End If


            Next
            If check = True Then
                If DateTime.Parse(sDate).DayOfWeek = DayOfWeek.Saturday Or DateTime.Parse(sDate).DayOfWeek = DayOfWeek.Sunday Then
                    sHrDet = "WEEKEND"
                    sHr = 0
                    sPto = 0
                    sPtoDet = ""           'last item in this column's collecton
                Else
                    sHrDet = "HERE"
                    sHr = 8
                    sPto = 0
                    sPtoDet = ""           'last item in this column's collecton
                End If
            End If



            DGV1.Rows.Add(New String() {sDate, sHrDet, sHr, sPtoDet, sPto})

        Next


    Else
        MsgBox("You must Fill in User Number and Soc. Sec Number", MsgBoxStyle.Information)
        Exit Sub

    End If


End Sub
0
 

Author Closing Comment

by:javagair
ID: 38871399
John

took all of your comments added  them together and got the datagrid to work.
accepted only one but all were useful

thanks

gary
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 38871407
Glad to Help (guide) you gary ....!!!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question