Question Show Check Box in DataGrid

kaizen

Member
Joined
Mar 12, 2008
Messages
8
Programming Experience
5-10
Hi All.

I am using the code below to show the results of a dataset that I want to import in to a database.

I want to add a new column (as the first one) that shows a check box. If the check box is ticked then I will import the record if it is not then I wil skip the record.

How do i show a check box as the first column each time?

Code:
Select Case txtPath.Text.Substring(txtPath.Text.Length - 4).ToString
                    Case ".mdb"
                        com.CommandText = "Select * INTO tmpImport from " & txtTable.Text & " in '" & txtPath.Text & "'"
                        com.ExecuteNonQuery()
                    Case ".xls"

                        com.CommandText = "SELECT * INTO tmpImport FROM [" & Replace(txtTable.Text, "'", "") & "] in '' [Excel 8.0;HDR=NO;IMEX=1;Database=" & txtPath.Text & "]"
                        com.ExecuteNonQuery()

                    Case ".csv"

                        Using MyReader As New Microsoft.VisualBasic.FileIO.TextFieldParser(txtPath.Text)
                            MyReader.TextFieldType = FileIO.FieldType.Delimited
                            MyReader.SetDelimiters(",")
                            fieldStr = ""


                            If Not MyReader.EndOfData Then
                                Try
                                    currentRow = MyReader.ReadFields()
                                    ColInx = 1
                                    fStr = ""
                                    For Each currentField In currentRow
                                        fieldStr = fieldStr & "Field" & ColInx & " varchar(255),"
                                        fStr = fStr & "Field" & ColInx & ","
                                        ColInx = ColInx + 1
                                    Next
                                    If fieldStr.Length > 0 Then
                                        fieldStr = fieldStr.Substring(0, fieldStr.Length - 1)
                                        fStr = fStr.Substring(0, fStr.Length - 1)
                                    End If

                                    com.CommandText = "Create table tmpImport ( " & fieldStr & " )"
                                    com.ExecuteNonQuery()

                                Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException

                                End Try

                            End If
                        End Using
                        Using MyReader As New Microsoft.VisualBasic.FileIO.TextFieldParser(txtPath.Text)
                            MyReader.TextFieldType = FileIO.FieldType.Delimited
                            MyReader.SetDelimiters(",")
                            While Not MyReader.EndOfData
                                Try
                                    currentRow = MyReader.ReadFields()
                                    ColInx = 1
                                    VStr = ""
                                    For Each currentField In currentRow
                                        If currentField.Length < 254 Then
                                            VStr = VStr & "'" & escapeStr(currentField) & "',"
                                        Else
                                            com.CommandText = "alter table tmpImport alter column Field" & ColInx & " text"
                                            com.ExecuteNonQuery()
                                            VStr = VStr & "'" & escapeStr(currentField) & "',"
                                        End If
                                        ColInx = ColInx + 1
                                    Next

                                    If VStr.Length > 0 Then VStr = VStr.Substring(0, VStr.Length - 1)
                                    com.CommandText = "insert into tmpImport (" & fStr & ") values (" & VStr & ")"
                                    com.ExecuteNonQuery()

                                Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException

                                End Try
                            End While
                        End Using


                    Case ".txt"

                        Using MyReader As New Microsoft.VisualBasic.FileIO.TextFieldParser(txtPath.Text)
                            MyReader.TextFieldType = FileIO.FieldType.Delimited

                            If chkTab.Checked Then
                                MyReader.SetDelimiters(vbTab)
                            ElseIf chlComma.Checked Then
                                MyReader.SetDelimiters(",")
                            ElseIf chkOther.Checked Then
                                MyReader.SetDelimiters(txtOther.Text)
                            End If


                            fieldStr = ""


                            If Not MyReader.EndOfData Then
                                Try
                                    currentRow = MyReader.ReadFields()
                                    ColInx = 1
                                    fStr = ""
                                    For Each currentField In currentRow
                                        fieldStr = fieldStr & "Field" & ColInx & " varchar(255),"
                                        fStr = fStr & "Field" & ColInx & ","
                                        ColInx = ColInx + 1
                                    Next
                                    If fieldStr.Length > 0 Then
                                        fieldStr = fieldStr.Substring(0, fieldStr.Length - 1)
                                        fStr = fStr.Substring(0, fStr.Length - 1)
                                    End If

                                    com.CommandText = "Create table tmpImport ( " & fieldStr & " )"
                                    com.ExecuteNonQuery()

                                Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException

                                End Try
                            End If
                        End Using
                        Using MyReader As New Microsoft.VisualBasic.FileIO.TextFieldParser(txtPath.Text)
                            MyReader.TextFieldType = FileIO.FieldType.Delimited
                            If chkTab.Checked Then
                                MyReader.SetDelimiters(vbTab)
                            ElseIf chlComma.Checked Then
                                MyReader.SetDelimiters(",")
                            ElseIf chkOther.Checked Then
                                MyReader.SetDelimiters(txtOther.Text)
                            End If
                            While Not MyReader.EndOfData
                                Try
                                    currentRow = MyReader.ReadFields()
                                    ColInx = 1
                                    VStr = ""
                                    For Each currentField In currentRow
                                        If currentField.Length < 254 Then
                                            VStr = VStr & "'" & escapeStr(currentField) & "',"
                                        Else
                                            com.CommandText = "alter table tmpImport alter column Field" & ColInx & " text"
                                            com.ExecuteNonQuery()
                                            VStr = VStr & "'" & escapeStr(currentField) & "',"
                                        End If
                                        ColInx = ColInx + 1
                                    Next

                                    If VStr.Length > 0 Then VStr = VStr.Substring(0, VStr.Length - 1)
                                    com.CommandText = "insert into tmpImport (" & fStr & ") values (" & VStr & ")"
                                    com.ExecuteNonQuery()

                                Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException

                                End Try
                            End While
                        End Using

                End Select



                ' Catch ex As Exception
                ' MsgBox("error occure while import file. please check that it not open by other application " + ex.Message)
                ' Me.Dispose()
                ' Exit Sub
                ' End Try



                grdiDs = New DataSet
                grdMap.DataBindings.Clear()

                comImport.Connection = con


                Dim ds As New DataSet
                Dim da As New OleDb.OleDbDataAdapter(comImport)

                comImport.CommandText = "Select * from tmpImport"
                importDA.Fill(grdiDs, "tmpImport")

                'fill main mapping grid
                grdMap.SetDataBinding(grdiDs, "tmpImport")

                'con.Close()
                'con = Nothing


                For ColInx = 0 To grdiDs.Tables(0).Columns.Count - 1

                    mytc = New DataGridTextBoxColumn
                    mytc.HeaderText = "Not Mapping"
                    mytc.MappingName = grdiDs.Tables(0).Columns(ColInx).ColumnName
                    mytc.Alignment = HorizontalAlignment.Left
                    myts.GridColumnStyles.Add(mytc)
                Next



                myts.MappingName = grdiDs.Tables(0).TableName
                myts.AllowSorting = False
                grdMap.TableStyles.Clear()
                grdMap.TableStyles.Add(myts)
Thanks for you help.
 
Last edited:
Top Bottom