Click here to Skip to main content
15,896,153 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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")

VB
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
Posted
Updated 22-Apr-13 21:56pm
v2

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.
 
Share this answer
 
Comments
DhananjayanP 23-Apr-13 3:54am    
Windows form
you want to delete the entire row . right?
if that is the case, use like this
DgvBranch.DeleteRow(index)
 
Share this answer
 

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