CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 18

Threaded View

  1. #12
    Join Date
    Jun 2007
    Location
    .NET 3.5 Beta SP1, Visual Basic 2008 Express
    Posts
    225

    Re: DataGirdViewComboBox saving problem

    Quote Originally Posted by brjames32
    OR

    is there a way of modifying the below code to ignore updating the relevant column in the spreadsheet if the datagridview combobox value is null or 0??
    Code:
    If ExcelYear = "7" Then
                    Dim MyExcelConnection As New System.Data.OleDb.OleDbConnection _
                    ("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=S:\Penalty Points\08-09\Year 7 2008-09.xls;Extended Properties=""Excel 8.0;HDR=Yes;IMEX=0""")
                    MyExcelConnection.Open()
                    With myInsertCommand
                        .CommandText = "Insert INTO [Sheet1$] ([StudentId],[StudentName],[ClassGrp],[TheDate],[Merits],[Demerits],[Penalty],[Lesson]," + _
                                            "[TeachingGrp],[Staff],[Description],[Action],[MeritType]) VALUES(@1,@2,@3,@4,@5,@6,@7,@8,@9,@10,@11,@12,@13)"
                        .CommandType = CommandType.Text
                        .Connection = MyExcelConnection
                        For Each Row As DataGridViewRow In DataGridView1.Rows
                            For Col As Int32 = 1 To Row.Cells.Count
                                .Parameters.Add(New OleDb.OleDbParameter("@" & Col, OleDb.OleDbType.Char)).Value = CStr(Row.Cells(Col - 1).Value)
                            Next Col
                            .ExecuteNonQuery()
                            .Parameters.Clear()
                        Next
                    End With
                    MyExcelConnection.Close()
                    MyExcelConnection.Dispose()
                    MyExcelConnection = Nothing
    Two things. One, For <something> to <something> step <some number>.
    Two, Yes, you can check for null. Thirdly, int32? that's integer.

    Code:
    If ExcelYear = "7" Then
                    Dim MyExcelConnection As New System.Data.OleDb.OleDbConnection _
                    ("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=S:\Penalty Points\08-09\Year 7 2008-09.xls;Extended Properties=""Excel 8.0;HDR=Yes;IMEX=0""")
                    MyExcelConnection.Open()
                    With myInsertCommand
                        .CommandText = "Insert INTO [Sheet1$] ([StudentId],[StudentName],[ClassGrp],[TheDate],[Merits],[Demerits],[Penalty],[Lesson]," + _
                                            "[TeachingGrp],[Staff],[Description],[Action],[MeritType]) VALUES(@1,@2,@3,@4,@5,@6,@7,@8,@9,@10,@11,@12,@13)"
                        .CommandType = CommandType.Text
                        .Connection = MyExcelConnection
                        For Each Row As DataGridViewRow In DataGridView1.Rows
                            For Col As integer = 1 To Row.Cells.Count Step 1
    
                                if row(col).value.toString <> system.dbnull.value.tostring then
                                .Parameters.Add(New OleDb.OleDbParameter("@" & Col, OleDb.OleDbType.Char)).Value = CStr(Row.Cells(Col - 1).Value)
                                 end if
                            Next Col
                            .ExecuteNonQuery()
                            .Parameters.Clear()
                        Next
                    End With
                    MyExcelConnection.Close()
                    MyExcelConnection.Dispose()
                    MyExcelConnection = Nothing
    And don't even bother asking me how VB has a string representation for Null, it works, so I ain't asking.

    Also, i've never quite had much luck with doing for each row in datagridview.rows. It almost never seems to work. You can do this and accomplish the same thing, i know this works.

    Code:
    For i As Integer = 0 To DataGridView1.Rows.Count - 1 Step 1
    
                For a As Integer = 0 To DataGridView1.Rows(i).Cells.Count - 1 Step 1
                    If DataGridView1.Rows(i).Cells(a).Value.ToString <> System.DBNull.Value.ToString Then
    Last edited by ccubed; July 30th, 2008 at 08:09 AM.
    Microsoft Visual Basic 2008 Express Edition
    .NET Framwork 3.5 Beta SP1

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

Featured