[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Problem with DataGrid in vb.net

Posted on 2005-04-25
3
Medium Priority
?
265 Views
Last Modified: 2008-02-01
below is my code

I am Adding deleting and repeating the records and also editing the last column, which contains integer, and programmatically calculating the total at bottom.

But the problem is when I repeat the record and after editing the last column which contains amount i move my cursor out of that edited cell then what I can see that its select the two rows one on which I hit mouse and second is last one.

Why is happening please suggest

Imports System
'Imports System.Convert
'Imports System.String
'Imports System.Int32
Imports System.Drawing
Imports System.Collections
Imports System.ComponentModel
Imports System.Windows.Forms
Imports System.Data
Imports System.Text
Imports System.Xml
Imports System.Console
Imports System.Data.OleDb
Imports System.IO
Imports System.Data.Odbc
Imports System.Web
Imports System.Data.SqlClient

Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents Datagrid1 As System.Windows.Forms.DataGrid
    Friend WithEvents Open_File As System.Windows.Forms.Button
    Friend WithEvents Add_Blank As System.Windows.Forms.Button
    Friend WithEvents Delete_Row As System.Windows.Forms.Button
    Friend WithEvents Save_File As System.Windows.Forms.Button
    Friend WithEvents Check_File As System.Windows.Forms.Button
    Friend WithEvents Srch_Files As System.Windows.Forms.Button
    Friend WithEvents Recent_Files As System.Windows.Forms.Button
    Friend WithEvents Exit_ As System.Windows.Forms.Button
    Friend WithEvents Rept_Rec As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.Datagrid1 = New System.Windows.Forms.DataGrid
        Me.Open_File = New System.Windows.Forms.Button
        Me.Srch_Files = New System.Windows.Forms.Button
        Me.Recent_Files = New System.Windows.Forms.Button
        Me.Exit_ = New System.Windows.Forms.Button
        Me.Add_Blank = New System.Windows.Forms.Button
        Me.Delete_Row = New System.Windows.Forms.Button
        Me.Save_File = New System.Windows.Forms.Button
        Me.Check_File = New System.Windows.Forms.Button
        Me.Rept_Rec = New System.Windows.Forms.Button
        CType(Me.Datagrid1, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.SuspendLayout()
        '
        'Datagrid1
        '
        Me.Datagrid1.AlternatingBackColor = System.Drawing.Color.FromArgb(CType(255, Byte), CType(255, Byte), CType(192, Byte))
        Me.Datagrid1.DataMember = ""
        Me.Datagrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText
        Me.Datagrid1.Location = New System.Drawing.Point(128, 64)
        Me.Datagrid1.Name = "Datagrid1"
        Me.Datagrid1.Size = New System.Drawing.Size(656, 368)
        Me.Datagrid1.TabIndex = 0
        '
        'Open_File
        '
        Me.Open_File.Location = New System.Drawing.Point(176, 24)
        Me.Open_File.Name = "Open_File"
        Me.Open_File.Size = New System.Drawing.Size(104, 23)
        Me.Open_File.TabIndex = 1
        Me.Open_File.Text = "Open New File"
        '
        'Srch_Files
        '
        Me.Srch_Files.Location = New System.Drawing.Point(312, 24)
        Me.Srch_Files.Name = "Srch_Files"
        Me.Srch_Files.Size = New System.Drawing.Size(96, 23)
        Me.Srch_Files.TabIndex = 2
        Me.Srch_Files.Text = "Search Files"
        '
        'Recent_Files
        '
        Me.Recent_Files.Location = New System.Drawing.Point(432, 23)
        Me.Recent_Files.Name = "Recent_Files"
        Me.Recent_Files.Size = New System.Drawing.Size(96, 23)
        Me.Recent_Files.TabIndex = 3
        Me.Recent_Files.Text = "Recent Files"
        '
        'Exit_
        '
        Me.Exit_.Location = New System.Drawing.Point(552, 22)
        Me.Exit_.Name = "Exit_"
        Me.Exit_.TabIndex = 4
        Me.Exit_.Text = "Exit"
        '
        'Add_Blank
        '
        Me.Add_Blank.Location = New System.Drawing.Point(8, 96)
        Me.Add_Blank.Name = "Add_Blank"
        Me.Add_Blank.Size = New System.Drawing.Size(104, 23)
        Me.Add_Blank.TabIndex = 5
        Me.Add_Blank.Text = "Add Blank"
        '
        'Delete_Row
        '
        Me.Delete_Row.Location = New System.Drawing.Point(8, 168)
        Me.Delete_Row.Name = "Delete_Row"
        Me.Delete_Row.Size = New System.Drawing.Size(104, 23)
        Me.Delete_Row.TabIndex = 6
        Me.Delete_Row.Text = "Delete Row"
        '
        'Save_File
        '
        Me.Save_File.Location = New System.Drawing.Point(8, 208)
        Me.Save_File.Name = "Save_File"
        Me.Save_File.Size = New System.Drawing.Size(104, 23)
        Me.Save_File.TabIndex = 7
        Me.Save_File.Text = "Save File"
        '
        'Check_File
        '
        Me.Check_File.Location = New System.Drawing.Point(8, 248)
        Me.Check_File.Name = "Check_File"
        Me.Check_File.Size = New System.Drawing.Size(104, 23)
        Me.Check_File.TabIndex = 9
        Me.Check_File.Text = "Check Files"
        '
        'Rept_Rec
        '
        Me.Rept_Rec.Location = New System.Drawing.Point(8, 130)
        Me.Rept_Rec.Name = "Rept_Rec"
        Me.Rept_Rec.Size = New System.Drawing.Size(104, 23)
        Me.Rept_Rec.TabIndex = 10
        Me.Rept_Rec.Text = "Repeat Record"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(808, 549)
        Me.Controls.Add(Me.Rept_Rec)
        Me.Controls.Add(Me.Add_Blank)
        Me.Controls.Add(Me.Check_File)
        Me.Controls.Add(Me.Save_File)
        Me.Controls.Add(Me.Delete_Row)
        Me.Controls.Add(Me.Exit_)
        Me.Controls.Add(Me.Recent_Files)
        Me.Controls.Add(Me.Srch_Files)
        Me.Controls.Add(Me.Open_File)
        Me.Controls.Add(Me.Datagrid1)
        Me.Name = "Form1"
        Me.Text = "Form1"
        CType(Me.Datagrid1, System.ComponentModel.ISupportInitialize).EndInit()
        Me.ResumeLayout(False)

    End Sub

#End Region
    Public MyCombo As New ComboBox
    Dim rowcount As Integer
    Dim flagcount As Boolean = False
    Dim datatable1 As New DataTable("Payfile")
    Private Sub Open_File_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Open_File.Click
        filepath()
        datatable1.Columns(1).ReadOnly = True
    End Sub
    Public Sub filepath()
        Dim dialog As New System.Windows.Forms.OpenFileDialog
        dialog.Title = "Select file"
        dialog.Filter = "csv(*.csv)|*.csv"
        dialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
        dialog.Multiselect = True
        dialog.CheckFileExists = True
        Dim dr As DialogResult = dialog.ShowDialog()

        If dr = DialogResult.OK Then
            Dim path As String = dialog.FileName
            exceldata(path)
        Else
            Return
        End If
    End Sub
    Public Sub exceldata(ByVal p As String)
        Dim dtCol As DataColumn = Nothing
        Dim arrstrFunctionalArea As String() = Nothing
        Dim cmbFunctionArea As System.Windows.Forms.ComboBox
        Dim oFile As System.IO.File
        Dim oRead As System.IO.StreamReader
        oRead = oFile.OpenText(p)
        arrstrFunctionalArea = New String(10) {}
        arrstrFunctionalArea(0) = "1"
        arrstrFunctionalArea(1) = "2"
        arrstrFunctionalArea(2) = "3"
        arrstrFunctionalArea(3) = "4"
        arrstrFunctionalArea(4) = "5"
        arrstrFunctionalArea(5) = "6"
        arrstrFunctionalArea(6) = "7"
        arrstrFunctionalArea(7) = "8"
        arrstrFunctionalArea(8) = "9"
        arrstrFunctionalArea(9) = "10"
        arrstrFunctionalArea(10) = "11"
 
        Dim i As Integer
        For i = 0 To 10
            Dim str As String = arrstrFunctionalArea(i)
            dtCol = New DataColumn(str)
            dtCol.DataType = System.Type.GetType("System.String")
            dtCol.DefaultValue = ""
            datatable1.Columns.Add(dtCol)
        Next i

        While oRead.Peek <> -1

            Dim LineIn As String() = Split(oRead.ReadLine(), ",")
            Dim dtrow As DataRow = datatable1.NewRow
            For i = 0 To 10
                Dim str As String = LineIn(i)
                dtrow.Item(i) = str
            Next i
            datatable1.Rows.Add(dtrow)

        End While
        Datagrid1.DataSource = datatable1
        Dim ts As New DataGridTableStyle
        ts.MappingName = "Payfile"
        Datagrid1.TableStyles.Add(ts)
        ' DataGrid1.DataSource = tab
        ' Datagrid1.TableStyles("authors").GridColumnStyles.Remove(DataGrid1.TableStyles("authors").GridColumnStyles("au_id"))

        Datagrid1.TableStyles("Payfile").GridColumnStyles("1").Width = 50
        Datagrid1.TableStyles("Payfile").GridColumnStyles("2").Width = 25
        Datagrid1.TableStyles("Payfile").GridColumnStyles("3").Width = 70
        Datagrid1.TableStyles("Payfile").GridColumnStyles("4").Width = 60
        Datagrid1.TableStyles("Payfile").GridColumnStyles("5").Width = 70
        Datagrid1.TableStyles("Payfile").GridColumnStyles("6").Width = 80
        Datagrid1.TableStyles("Payfile").GridColumnStyles("7").Width = 80
        Datagrid1.TableStyles("Payfile").GridColumnStyles("8").Width = 25
        Datagrid1.TableStyles("Payfile").GridColumnStyles("9").Width = 25
        Datagrid1.TableStyles("Payfile").GridColumnStyles("10").Width = 30
        Datagrid1.TableStyles("Payfile").GridColumnStyles("11").Width = 50
        flagcount = True
    End Sub



    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        '      AddHandler datatable1.ColumnChanged, New DataColumnChangeEventHandler(AddressOf hand1)
        AddHandler datatable1.ColumnChanged, AddressOf UpdateTotal

    End Sub

    Private Sub Add_Blank_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Add_Blank.Click
        flagcount = False
        Dim posi As Integer = Datagrid1.CurrentRowIndex
        Dim newrowposi As Integer = Datagrid1.CurrentRowIndex + 1
        If posi > 0 Then
            Dim col1 As String = datatable1.Rows(posi).Item("1").ToString
            Dim col2 As String = datatable1.Rows(posi).Item("2").ToString
            Dim col3 As String = datatable1.Rows(posi).Item("3").ToString
            Dim col4 As String = datatable1.Rows(posi).Item("4").ToString

            Dim dtrow = datatable1.NewRow

            dtrow.Item("1") = col1
            dtrow.Item("2") = col2
            dtrow.Item("3") = col3
            dtrow.Item("4") = col4

            dtrow.Item("5") = ""
            dtrow.Item("6") = ""
            dtrow.Item("7") = ""
            dtrow.Item("8") = ""
            dtrow.Item("9") = ""
            dtrow.Item("10") = ""
            dtrow.Item("11") = ""
            datatable1.Rows.InsertAt(dtrow, newrowposi)
            '  Me.Datagrid1.Select(newrowposi)
            datatable1.AcceptChanges()
            Datagrid1.Refresh()
            flagcount = True
        Else
            MsgBox("Please Select The Row First")
            Exit Sub
        End If

    End Sub

    Private Sub Delete_Row_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Delete_Row.Click
        datatable1.Rows(Datagrid1.CurrentRowIndex).Delete()
        datatable1.AcceptChanges()
        'Datagrid1.Refresh()

    End Sub

    Private Sub Save_File_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Save_File.Click
        Dim intColumn, intRow, intColumnValue As Integer
        Dim strAppPath = System.Reflection.Assembly.GetExecutingAssembly.Location.Substring(0, System.Reflection.Assembly.GetExecutingAssembly.Location.LastIndexOf("\") + 1)

        Dim fs As New FileStream(strAppPath & "payfile-armac-023014.csv", FileMode.Create, FileAccess.Write)
        'declaring a FileStream and creating a document file named file with
        'access mode of writing
        Dim s As New StreamWriter(fs)
        'creating a new StreamWriter and passing the filestream object fs as argument
        Dim joinstr As String = ""

        For intRow = 0 To datatable1.Rows.Count - 1
            joinstr = ""

            For intColumnValue = 0 To datatable1.Columns.Count - 1
                joinstr = joinstr & "," & datatable1.Rows(intRow).ItemArray(intColumnValue).ToString
            Next
            'MsgBox(joinstr)
            joinstr = Mid(joinstr, 2)
            s.WriteLine(joinstr)
        Next
        '  s.WriteLine("This concept is interesting.")
        'writing text to the newly created file
        s.Close()
    End Sub

    Private Sub Save_As_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        ' Dim filesaveas As New SaveFileDialog

        Dim filesaveas As New System.Windows.Forms.SaveFileDialog
        filesaveas.Filter = "csv Files(*.csv)|*.csv"
        filesaveas.FileName() = "payfile-armac-023014.csv"
        filesaveas.ShowDialog()

    End Sub

    Private Sub Rept_Rec_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Rept_Rec.Click
        flagcount = False
        Dim posi As Integer = Datagrid1.CurrentRowIndex
        Dim newrowposi2 As Integer = Nothing
        newrowposi2 = Datagrid1.CurrentRowIndex + 1
        If posi > 0 Then
            Dim col1 As String = datatable1.Rows(posi).Item("1").ToString
            Dim col2 As String = datatable1.Rows(posi).Item("2").ToString
            Dim col3 As String = datatable1.Rows(posi).Item("3").ToString
            Dim col4 As String = datatable1.Rows(posi).Item("4").ToString
            Dim col5 As String = datatable1.Rows(posi).Item("5").ToString
            Dim col6 As String = datatable1.Rows(posi).Item("6").ToString
            Dim col7 As String = datatable1.Rows(posi).Item("7").ToString
            Dim col8 As String = datatable1.Rows(posi).Item("8").ToString
            Dim col9 As String = datatable1.Rows(posi).Item("9").ToString
            Dim col10 As String = datatable1.Rows(posi).Item("10").ToString
            Dim col11 As String = datatable1.Rows(posi).Item("11").ToString

            Dim dtrow = datatable1.NewRow

            dtrow.Item("1") = col1
            dtrow.Item("2") = col2
            dtrow.Item("3") = col3
            dtrow.Item("4") = col4
            dtrow.Item("5") = col5
            dtrow.Item("6") = col6
            dtrow.Item("7") = col7
            dtrow.Item("8") = col8
            dtrow.Item("9") = col9
            dtrow.Item("10") = col10
            dtrow.Item("11") = col11

            datatable1.Rows.InsertAt(dtrow, newrowposi2)
            Dim rindex As Integer = Me.Datagrid1.CurrentRowIndex
            Me.Datagrid1.Select(rindex)

            ' datatable1.AcceptChanges()
            rowcount = datatable1.Rows.Count
            calamount()
            datatable1.Rows(rowcount - 1).Item("4") = rowcount
            datatable1.AcceptChanges()

            Datagrid1.Refresh()

         
            flagcount = True
        Else
            MsgBox("Please Select The Row First")
            Exit Sub
        End If
    End Sub

    Private Sub dataGrid1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Datagrid1.MouseUp

        Dim pt = New Point(e.X, e.Y)

        Dim hti As DataGrid.HitTestInfo = dataGrid1.HitTest(pt)

        If hti.Type = DataGrid.HitTestType.Cell Then

            dataGrid1.CurrentCell = New DataGridCell(hti.Row, hti.Column)

            dataGrid1.Select(hti.Row)

        End If

    End Sub

    Private Sub UpdateTotal(ByVal sender As Object, ByVal e As DataColumnChangeEventArgs)

        If flagcount = True Then
            Dim posi As Integer = Datagrid1.CurrentRowIndex
            Me.Datagrid1.Select(posi)
            flagcount = False
            e.Row.AcceptChanges()
            Dim i As Integer
            Dim totalval As Double
            Dim rowcount As Integer = datatable1.Rows.Count
            For i = 1 To datatable1.Rows.Count - 1
                Dim cyrval As String = datatable1.Rows(i).Item("11").ToString
                totalval = totalval + Val(datatable1.Rows(i).Item("11"))
            Next
            datatable1.Rows(datatable1.Rows.Count - 1).Item("5") = totalval
            datatable1.AcceptChanges()
            Datagrid1.Refresh()
            ' Me.Datagrid1.Select(posi)
            flagcount = True
        End If
    End Sub

    Public Sub calamount()
        flagcount = False
        Dim i As Integer
        Dim totalval As Double
        For i = 1 To datatable1.Rows.Count - 1
            Dim cyrval As String = datatable1.Rows(i).Item("11").ToString
            totalval = totalval + Val(datatable1.Rows(i).Item("11"))
        Next
        datatable1.Rows(datatable1.Rows.Count - 1).Item("5") = totalval
        datatable1.AcceptChanges()
        Datagrid1.Refresh()
        flagcount = True
    End Sub
    Private Sub Exit__Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Exit_.Click
        flagcount = False
        Me.Close()
    End Sub
End Class

0
Comment
Question by:rvsglobal
1 Comment
 
LVL 16

Accepted Solution

by:
RobertRFreeman earned 2000 total points
ID: 13859046
These commands select the rows.  By default you can select more than one row.

dataGrid1.Select(hti.Row)
Me.Datagrid1.Select(posi)

If you only want one row highlighted at a time, then turn multiselect off.
Alternatively, you can clear out the selected rows collection before setting the selected row.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
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…
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…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses
Course of the Month19 days, 18 hours left to enroll

873 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