WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

System_Window_Forms

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

DataGridView

 Keywords:  DataGridView dotNet .Net Form Grid Control

;***************************************************************************
;**   Use a Windows .Net Form with a DataGridView Control
;**
;** Purpose: Displays data in a customizable grid usng a DataGridView control.
;** Inputs:  CSV
;** Outputs: Results written to screen and an output file
;** Reference:
;**       REQUIRES WinBatch 2013A or newer
;**
;** Developer: Deana Falk 2014.02.11
;**
;** Notes: http://www.codeproject.com/Articles/16951/Populating-data-from-a-CSV-file-to-a-DataGridView
;***************************************************************************
If Version( )< '2013A'
   Pause('Notice', 'Need 2013A or Newer Version of WinBatch')
   Exit
EndIf

infile = 'C:\TEMP\Data\CSV\TestIn.csv'
If !FileExist( infile )
   Pause('Notice', 'Input CSV Does Not Exist')
   Exit
EndIf

outfile = DirScript():'Result.csv'
If FileExist( outfile ) Then FileDelete( outfile )

 _True = ObjectType( 'BOOL', -1 )
 _False = ObjectType( 'BOOL', 0 )

;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; ObjectClrOption - Load assembly into the WinBatch process
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ObjectClrOption( 'use', 'System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' )
ObjectClrOption( 'use', 'System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' )

;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; ObjectClrNew - Create a class implemented by a managed assembly.
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Form1 = ObjectClrNew('System.Windows.Forms.Form')
Button1 =  ObjectClrNew('System.Windows.Forms.Button')
DataGridView1 =  ObjectClrNew('System.Windows.Forms.DataGridView')
StreamReader = ObjectClrNew("System.IO.StreamReader", infile)  ;, Encoding.UTF8
BindingSource = ObjectClrNew("System.Windows.Forms.BindingSource")

; Enumerations
enumStartPos = ObjectClrNew('System.Windows.Forms.FormStartPosition')
enumDialogResult = ObjectClrNew( 'System.Windows.Forms.DialogResult')
enumViewHeaderBorderStyle = ObjectClrNew( 'System.Windows.Forms.DataGridViewHeaderBorderStyle' )
enumViewCellBorderStyle  = ObjectClrNew( 'System.Windows.Forms.DataGridViewCellBorderStyle' )
enumDataGridViewSelectionMode = ObjectClrNew( 'System.Windows.Forms.DataGridViewSelectionMode' )
enumDockStyle = ObjectClrNew('System.Windows.Forms.DockStyle')

;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; Define Form1
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; Set the caption bar text of the form.
Form1.Text = 'DataGridView'
; Set the start position of the form to the center of the screen.
Form1.StartPosition = ObjectClrType( 'System.Windows.Forms.FormStartPosition', enumStartPos.CenterScreen ) ;CenterScreen
; Set the accept button of the form to button1.
Form1.AcceptButton = button1
; Obtain the Width of the form for later use with controls
Form1.Size =   ObjectClrNew( 'System.Drawing.Size',900,550)
formwidth = Form1.Width

;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; Define DataGrid
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; Position  DataGrid
DataGridView1.Location = ObjectClrNew('System.Drawing.Point',0,10)
; Define width to width of the form
DataGridView1.Width = formwidth
; Specify tabindex
DataGridView1.TabIndex  = 0
DataGridView1.ColumnCount = 5
DataGridView1.Name = "DataGridView"
DataGridView1.Location = ObjectClrNew('System.Drawing.Point',8,8)
DataGridView1.Size =   ObjectClrNew( 'System.Drawing.Size',500,250); Size(500, 250)
DataGridView1.ColumnHeadersBorderStyle = ObjectClrType( 'System.Windows.Forms.DataGridViewHeaderBorderStyle', enumViewHeaderBorderStyle.Single)
DataGridView1.CellBorderStyle = ObjectClrType( 'System.Windows.Forms.DataGridViewCellBorderStyle', enumViewCellBorderStyle.Single)
DataGridView1.RowHeadersVisible = _False
;DataGridView1.SelectionMode = ObjectClrType('System.Windows.Forms.DataGridViewSelectionMode',enumDataGridViewSelectionMode.FullRowSelect)
DataGridView1.MultiSelect = _False

If FileExist(infile)
   ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   ; Load WIL array Data From a CSV into DataGridView1
   ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   While StreamReader.Peek() <> -1
      TextLine = StreamReader.ReadLine()
      arrRow = Arrayize( TextLine, ',' )
      DataGridView1.Rows.Add(arrRow)
   EndWhile
Else
   Pause('File Exist?','No')
EndIf

;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; Define Button1
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; Set the position of the button on the form.
button1.Location = ObjectClrNew('System.Drawing.Point',100,410)
; Set the text of button1 to 'OK'.
button1.Text = 'OK'
; Specify tabindex
button1.TabIndex  = 1
; IMPORTANT: The dialog box can be assigned one of the values of the DialogResult enumeration by assigning it to the DialogResult property of a Button on the form.
button1.DialogResult = ObjectClrType( 'System.Windows.Forms.DialogResult', enumDialogResult.OK )
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; Add Controls
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Form1.Controls.Add(Button1)
Form1.Controls.Add(DataGridView1)
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; Display the form as a modal dialog box.
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ret = Form1.ShowDialog()
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; Get Results
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;Read each cell
;For j = 0 To (DataGridView1.Rows.Count - 2)
;   For i = 0 To (DataGridView1.Columns.Count - 1)
;      cellvalue = DataGridView1.Item(i, j).Value
;      Pause("Row ":j:" Column ":i, cellvalue)
;   Next
;Next
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; Clean up
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Button1.Dispose()
DataGridView1.Dispose()
Form1.Dispose()

Exit

Article ID:   W17846
Filename:   DataGridView.txt
File Created: 2014:05:09:13:12:26
Last Updated: 2014:05:09:13:12:26