在datagridview中排序后阻止添加行 [英] Prevent adding a row after sorting in datagridview

查看:97
本文介绍了在datagridview中排序后阻止添加行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个4列的DataGridView。 allowusertoaddrows设置为false。我在Load事件中添加第一行。 CellEndEdit事件添加一个新行并将行索引增加1.代码的目标是将焦点设置为第3列(索引2)添加数据,按Tab键然后移动到下一行中的相同第3列并再次添加数据。添加足够的数据后,然后对列进行排序。下面的代码可以做到这一点,但是在排序之后,使用列标题单击或添加按钮单击进行排序,它会在顶部添加一个新行(索引0)。我认为我需要它在排序时禁用CellEndEdit。还有另一种方式吗?这是我的代码。

 私人 i  As  整数 
私有 Sub Form1_Load(发件人作为 对象,e As EventArgs)句柄 MyBase .Load
DataGridView1.Rows.Add()
DataGridView1.CurrentCell = DataGridView1( 2 ,i)
End Sub
私有 Sub DataGridView1_KeyPress(sender As Object ,e As KeyPressEventArgs)处理 DataGri dView1.KeyPress
如果 e.KeyChar = vbTab 那么
DataGridView1.CurrentCell = DataGridView1( 2 ,i)
DataGridView1.Focus()
结束 如果
结束 Sub
私有 Sub DataGridView1_CellEndEdit(发件人作为 < span class =code-keyword> Object ,e As DataGridViewCellEventArgs) Handles DataGridView1。 CellEndEdit
DataGridView1.Rows.Add()
i = i + 1
SendKeys.Send(vbTab)
结束 Sub

解决案

据我所知,你是手动输入值。



所以当你输入足够的值时,你点击按钮或用于对列进行排序的列标题。



现在我不认为在添加了最后一个值后,您点击了其他任何地方,但直接进行排序。



如果您点击其他任何地方,您会发现还会添加额外的行。

此外,在排序时还会添加额外的行仅限第一次。



这种情况​​正在发生,因为即使您输入最后一个值并单击其他任何地方,也会调用CellEndEdit事件。



以下代码可能会起作用



 Private Sub DataGridView1_CellEndEdit(ByVal sender As System.Object,ByVal e As System.Windows.Forms.DataGridViewCellEventArgs)处理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)处理DataGridView1.Sorted
Dim totalRowContent As String
for rwNum As Integer = 0 To DataGridView1.Rows.Count - 1

如果rwNum = DataGridView1.Rows.Count则
退出
结束如果

totalRowContent =
for colNum As Integer = 0 To DataGridView1.Columns.Count - 1
totalRowContent = totalRowContent + DataGridView1(colNum,rwNum).FormattedValue
下一个
如果totalRowContent =那么
DataGridView1.Rows.RemoveAt(rwNum)
rwNum - = 1
结束如果
下一个
结束Sub





请记住,SendKeys是一个非常危险的事情,尽量避免使用它。您使用SendKeys调用KeyPress事件,然后将焦点设置为下一个所需的单元格。



我通过仅在CellEndEdit事件中设置焦点来简化它并且我使用了DGV的Sorted事件删除了额外的行。


我是CodeProject的新成员,所以我不确定我发送给谁,但非常感谢你时间回应。与此同时,我解决了我的问题(有点)。你的建议没有帮助,但是你给了我两个非常好的想法,我的其他项目,所以这对我来说非常值得。我可能会后悔,但我喜欢SendKeys,你会在我的解决方案中看到。您的代码中缺少什么,我的主要工作是仅关注Column3,并删除我的代码所做的额外行。其他栏目已经填写完毕。第3列将是24小时格式的帆船比赛的结束时间。第4列是完成位置,即第一,第二,第三等。这是我的代码:



私有我作为整数= 0

Private Sub DataGridView1_CellEndEdit(sender As Object,e As DataGridViewCellEventArgs)处理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)

结束子

私有子Form1_Load(发件人为对象,e为EventArgs)处理MyBase.Load

DataGridView1.Rows.Add()

DataGridView1.CurrentCell = DataGridView1(2,0)

End Sub

Private Sub Button1_Click(发送者为对象,e为EventArgs)处理Button1.Click

如果DataGridView1(2,0).Value = Nothing那么

DataGridView 1.Rows.RemoveAt(0)

i = i - 1

ElseIf DataGridView1(2,i).Value = Nothing Then

DataGridView1。 Rows.RemoveAt(i)

i = i - 1

结束如果

for j = 0 to i

DataGridView1(3,j).Value = j + 1

下一页

结束次级



尽可能看到我在第3列中的每个条目后排序,然后在完成输入完成时间后,我点击一个按钮,在开头或结尾删除额外的行,然后用1,2,3,4,5等填充column4。

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.

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

解决方案

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.


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.


这篇关于在datagridview中排序后阻止添加行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆