Click here to Skip to main content
15,909,591 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a 4 column DataGridView. The allowusertoaddrows is set to false. I add the first row in the Load event. The CellEndEdit event adds a new row and increases the row index by 1. The goal of the code is to set the focus to the 3rd column (index 2)add data, press tab and then move to the same 3rd column in the next row and again add data. After adding sufficient data then sort the column. The code below does that, however after the sort, using the column header click or adding a button click to sort, it adds a new row at the top (index 0). What I think I need it to disable the CellEndEdit when sorting. Is there perhaps another way. Here is my code.
VB
Private i As Integer
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
       DataGridView1.Rows.Add()
       DataGridView1.CurrentCell = DataGridView1(2, i)
   End Sub
Private Sub DataGridView1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles DataGridView1.KeyPress
       If e.KeyChar = vbTab Then
           DataGridView1.CurrentCell = DataGridView1(2, i)
           DataGridView1.Focus()
       End If
   End Sub
Private Sub DataGridView1_CellEndEdit(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellEndEdit
       DataGridView1.Rows.Add()
       i = i + 1
       SendKeys.Send(vbTab)
   End Sub
Posted
Updated 14-May-14 21:46pm
v2

As far as i can understand, you are entering the values manually.

So when you enter enough values, you click on button or column header to sort the column.

Now i dont think that after you have added the last value you clicked any where else but directly went out for sorting.

If you click anywhere else, you will find that then also the extra row will be added.
Also the extra row will be added while sorting for the first time only.

This is happening because even when you enter the last value and click anywhere else, CellEndEdit event is called.

The following code might do the trick

Private Sub DataGridView1_CellEndEdit(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellEndEdit
        DataGridView1.Rows.Add()
        i = i + 1
        DataGridView1.CurrentCell = DataGridView1(2, i)
    End Sub

    Private Sub DataGridView1_Sorted(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DataGridView1.Sorted
        Dim totalRowContent As String
        For rwNum As Integer = 0 To DataGridView1.Rows.Count - 1

            If rwNum = DataGridView1.Rows.Count Then
                Exit For
            End If

            totalRowContent = ""
            For colNum As Integer = 0 To DataGridView1.Columns.Count - 1
                totalRowContent = totalRowContent + DataGridView1(colNum, rwNum).FormattedValue
            Next
            If totalRowContent = "" Then
                DataGridView1.Rows.RemoveAt(rwNum)
                rwNum -= 1
            End If
        Next
    End Sub


And remember, SendKeys is a very dangerous thing, avoid it as far as possible. You used SendKeys for calling the KeyPress event which in turn set the focus to the next desired cell.

I simplified it by setting the focus in CellEndEdit event only and I used the Sorted event of DGV to delete extra rows present.
 
Share this answer
 
I am a new member of CodeProject, so I am not sure who I am sending this to, but thank you very much for taking the time to respond. In the meantime I resolved my issue (kinda). Your suggestion did not help, however you gave me two very good ideas for my other project, so it was well worth it for me. I may regret it but I love SendKeys as you will see in my solution. What is missing in your code and the main thrust of my effort is to have the focus on Column3 only, and remove the extra row which my code does. Other columns will be already filled. Column3 will be the finish times in 24hr format of a sailboat race. Column 4 the finish place, i.e., first, second, third, etc. Here is my code:

Private i As Integer = 0
Private Sub DataGridView1_CellEndEdit(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellEndEdit
DataGridView1.Sort(DataGridView1.Columns(2), System.ComponentModel.ListSortDirection.Ascending)
DataGridView1.Rows.Add()
i = i + 1
SendKeys.Send(vbTab)
SendKeys.Send(vbTab)
SendKeys.Send(vbTab)
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
DataGridView1.Rows.Add()
DataGridView1.CurrentCell = DataGridView1(2, 0)
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If DataGridView1(2, 0).Value = Nothing Then
DataGridView1.Rows.RemoveAt(0)
i = i - 1
ElseIf DataGridView1(2, i).Value = Nothing Then
DataGridView1.Rows.RemoveAt(i)
i = i - 1
End If
For j = 0 To i
DataGridView1(3, j).Value = j + 1
Next
End Sub

As you can see I sort after each entry in column3 and then when finished entering finish times I click on a button which removes the extra row either at the beginning or the end and then fills column4 with 1,2,3,4,5 etc.
 
Share this answer
 
Comments
kartikguha 23-May-14 3:54am    
Hey!!!

Welcome to Code Project.

Glad to help you :)

And as far as you, not knowing whom are you replying to, the name of the members are written right under their comments/solutions :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900