删除数据网格视图中的行 [英] Deleting rows in data grid view

查看:71
本文介绍了删除数据网格视图中的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在验证数据网格视图窗口表单,下面是我的代码听到我删除行并分配值= 0,但我需要删除行请帮助并给我一个代码(DgvBranch.Rows(索引) ).Cells(0).Value =0)



 私人  Sub  DgvBranch_CellEndEdit( ByVal  sender 作为系统。对象 ByVal  e 作为系统.Windows.Forms.DataGridViewCellEventArgs)句柄 DgvBranch.CellEndEdit 

尝试

如果 (IsNumeric(DgvBranch.Rows(e.RowIndex).Cells( e.ColumnIndex).Value))然后
DgvBranch.Rows(e.RowInde x)。细胞(e.ColumnIndex).Value = 字符串 .Empty
退出 Sub
结束 如果
如果(e.ColumnIndex = 1 那么
Dim value As Int32 = AsInt(DgvBranch.Rows(e.RowIndex).Cells(e.ColumnIndex).Value)
Dim fromValue As Int32 = AsInt(DgvBranch.Rows(e.RowIndex).Cells( 0 )。值)
如果(Convert.ToInt32(value)< Convert.ToInt32(fromValue))然后
MessageBox.Show( 结束日期应该是开始日期的最大值

如果 currentValue> 0 然后
DgvBranch.Rows(e.RowIndex).Cells(e.ColumnIndex)。 Value = currentValue

Else
DgvBranch.Rows(e.RowIndex).Cells(e.ColumnIndex).Value = 字符串 .Empty
结束 如果
退出 Sub

结束 如果

Dim nextToVal As Int32 = AsInt(DgvBranch.Rows(e.RowIndex + 1 )。单元格(e.ColumnIndex).Value)

如果(下一步) ToVal> 0 value> = nextToVal)然后
DgvBranch.CommitEdit( New DataGridViewDataErrorContexts())

If (MessageBox.Show( 需要删除子seq数据 确认,MessageBoxButtons.YesNo)= DialogResult.Yes)然后
' 听到我删除行并指定值= 0,但我需要删除行
对于 index = e.RowIndex + 1 DgvBranch.Rows.Count - 1
D gvBranch.Rows(index).Cells( 0 )。Value = 0
DgvBranch.Rows(index).Cells( 1 )。Value = 0
DgvBranch.Rows(index).Cells( 2 ).Value = 0
下一步
ElseIf (currentValue> 0 然后
DgvBranch.Rows(e.RowIndex).Cells(e.ColumnIndex) .Value = currentValue
ElseIf (nextToVal> 0 然后
DgvBranch.Rows(e.RowIndex + 1 )。单元格( 0 )。值=值+ 1
结束 如果
ElseIf (nextToVal> 0 然后
DgvBranch.Rows(e.RowIndex + 1 )。Cells( 0 )。值=值+ 1
结束 如果

结束 如果
Catch ex As 异常
MainForm.ErrorMsg.StatusBarMsg(ex .Message.ToString,Bx_UI_COM_ErrorMsg.ErrorComponent.MessageType.bx_Error)
结束 尝试

结束 Sub

解决方案

你忽略了提到这是WPF或WinForms,但这无论如何都可行:

http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.rows.aspx [ ^ ]



请参阅文章中的RemoveAt代码。



如果如果源绑定到datagridview,则应该删除底层DataTable或Collection中的entery。


要删除整行。对吗?

如果是这样的话,就这样使用

DgvBranch.DeleteRow(index)


Hai i am validating data grid view windows forms ,below is my code Hear i am remove the rows and assign the value =0 ,But i need do delete the rows Please help and Give me a code (DgvBranch.Rows(index).Cells(0).Value = "0")

Private Sub DgvBranch_CellEndEdit(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DgvBranch.CellEndEdit

        Try

            If Not (IsNumeric(DgvBranch.Rows(e.RowIndex).Cells(e.ColumnIndex).Value)) Then
                DgvBranch.Rows(e.RowIndex).Cells(e.ColumnIndex).Value = String.Empty
                Exit Sub
            End If
            If (e.ColumnIndex = 1) Then
                Dim value As Int32 = AsInt(DgvBranch.Rows(e.RowIndex).Cells(e.ColumnIndex).Value)
                Dim fromValue As Int32 = AsInt(DgvBranch.Rows(e.RowIndex).Cells(0).Value)
                If (Convert.ToInt32(value) < Convert.ToInt32(fromValue)) Then
                    MessageBox.Show("End day should be max of start day")

                    If currentValue > 0 Then
                        DgvBranch.Rows(e.RowIndex).Cells(e.ColumnIndex).Value = currentValue

                    Else
                        DgvBranch.Rows(e.RowIndex).Cells(e.ColumnIndex).Value = String.Empty
                    End If
                    Exit Sub

                End If

                Dim nextToVal As Int32 = AsInt(DgvBranch.Rows(e.RowIndex + 1).Cells(e.ColumnIndex).Value)

                If (nextToVal > 0 And value >= nextToVal) Then
                    DgvBranch.CommitEdit(New DataGridViewDataErrorContexts())

                    If (MessageBox.Show("Need to remove sub seq data", "Confirm", MessageBoxButtons.YesNo) = DialogResult.Yes) Then
'Hear i am remove the rows and assign the value =0 ,But i need do delete the rows 
                        For index = e.RowIndex + 1 To DgvBranch.Rows.Count - 1
                            DgvBranch.Rows(index).Cells(0).Value = "0"
                            DgvBranch.Rows(index).Cells(1).Value = "0"
                            DgvBranch.Rows(index).Cells(2).Value = "0"
                        Next
                    ElseIf (currentValue > 0) Then
                        DgvBranch.Rows(e.RowIndex).Cells(e.ColumnIndex).Value = currentValue
                    ElseIf (nextToVal > 0) Then
                        DgvBranch.Rows(e.RowIndex + 1).Cells(0).Value = value + 1
                    End If
                ElseIf (nextToVal > 0) Then
                    DgvBranch.Rows(e.RowIndex + 1).Cells(0).Value = value + 1
                End If

            End If
        Catch ex As Exception
            MainForm.ErrorMsg.StatusBarMsg(ex.Message.ToString, Bx_UI_COM_ErrorMsg.ErrorComponent.MessageType.bx_Error)
        End Try

    End Sub

解决方案

You neglected to mention of this was WPF or WinForms, but this sould work anyway:
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.rows.aspx[^]

See the RemoveAt code in the article.

If you have the source binded to a datagridview, you should detlete the entery in the underlying DataTable or Collection.


you want to delete the entire row . right?
if that is the case, use like this
DgvBranch.DeleteRow(index)


这篇关于删除数据网格视图中的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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