We help IT Professionals succeed at work.

To Author : iboutckine

sainavya1215
sainavya1215 asked
on
273 Views
Last Modified: 2011-06-28
In the unbound Grid created :

1) I want to delete a row in the Grid ???
2) I want the user not to make changes to the columns in the Grid
   (if we make it readonly we cant delete a row)
Comment
Watch Question

Author

Commented:
lets say i have 2 colums and 2 rows added to the Grid

i selected one row and pressed del key .... when i press the button again it still says 2 rows 2 columns available. i think we have to manipulate the dataset with some related event of datagrid

Author

Commented:
2Q) column values should not be changed by user.... a row can be deleted but columns cannot be edited though

Commented:
Windows or Web form?
1) I want to delete a row in the Grid ???
2) I want the user not to make changes to the columns in the Grid
   (if we make it readonly we cant delete a row)
-------------------------------------------------

make the grid read only and then delete it via ExecuteBobQuery.
Here is how to do it

Click on the unique field in the row. The following code will give you the info about cell

Private Sub dg_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles dg.MouseDown
        ' X & Y are in the grid' coordinates. If they are in screen coordinates, call dataGrid1.PointToClient method
        Dim pt = New Point(e.X, e.Y)
        Dim hti As DataGrid.HitTestInfo = dg.HitTest(pt)

        If hti.Type = DataGrid.HitTestType.Cell Then
            'cell text)
            MessageBox.Show(dg(hti.Row, hti.Column).ToString())
            'row number
            MsgBox((dg.CurrentCell.RowNumber).ToString)
            'col number
            MsgBox((dg.CurrentCell.ColumnNumber).ToString)
        Else
            'header name ( column)
            If hti.Type = DataGrid.HitTestType.ColumnHeader Then
                MsgBox(ds.Tables(0).Columns(hti.Column).ToString)'ds was defined earlier
            End If
        End If

and then execute SQL
'if it is string
dim sSQL as string = "delete from Table where YourUniqueFIeld = '" & dg(hti.Row, hti.Column).ToString() & "'"
'if it is number
dim sSQL as string = "delete from Table where YourUniqueFIeld = " & dg(hti.Row, hti.Column)


  Dim cmd As OleDbCommand = New OleDbCommand()

        cmd.Connection = con
        cmd.CommandText = sSQL
            cmd.ExecuteNonQuery()
    End Sub

typo
instead
ExecuteBobQuery.
read
ExecuteNonQuery

typing is not my strongest skill:(

Author

Commented:
ibouthickine,

FIRSTLY points have been increased to this question

why is this statement posted
and then execute SQL
'if it is string
dim sSQL as string = "delete from Table where YourUniqueFIeld = '" & dg(hti.Row, hti.Column).ToString() & "'"
'if it is number
dim sSQL as string = "delete from Table where YourUniqueFIeld = " & dg(hti.Row, hti.Column)


  Dim cmd As OleDbCommand = New OleDbCommand()

        cmd.Connection = con
        cmd.CommandText = sSQL
            cmd.ExecuteNonQuery()
    End Sub
=========================================================================================
EARLIER THIS WAS THE CODE WORKING IF U can see the posting yesterdays
   Dim iRow As Integer
        Dim serial_number As Integer
        Dim end_serial_number As Integer


        MsgBox("We have " & ds.Tables(0).Rows.Count & " rows, and " & ds.Tables(0).Columns.Count & " columns")

        For iRow = 0 To ds.Tables(0).Rows.Count - 1
            'let's say serial_number in col1 andend_serial_number in col 2
            serial_number = ds.Tables(0).Rows(iRow).Item(0)
            MsgBox("serial_number=" & serial_number)

            end_serial_number = ds.Tables(0).Rows(iRow).Item(1)
            MsgBox("end_serial=" & end_serial_number)

        Next

   Dim ds As DataSet
    Dim dt As DataTable
    Dim data_column As DataColumn


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ' Populate the table.

        ' Populate the table.
        Dim serialNumbers(1) As Object
        serialNumbers(0) = TextBox1.Text.Trim
        serialNumbers(1) = TextBox2.Text.Trim

        dt.Rows.Add(serialNumbers)

        ' Accept the data.
        ds.AcceptChanges()

        ' Attach the DataGrids to the DataTables.
        DataGrid1.DataSource = dt


    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ' Build the DataSet.
        ds = New DataSet()

        '  
        dt = New DataTable("Articles")
        ds.Tables.Add(dt)
        dt.Columns.Add("serial number", GetType(Integer))
        dt.Columns.Add("end serial", GetType(Integer))


    End Sub

AS PER THIS CODE THERE IS NO CONNECTION INVOLVED AT ALL ..I really dont understand why command objects and CONN objects are coming into existance. We created an unbound grid which has no relation to the database at all

as u can see my datagrid name is datagrid1 and I want to delete a row
I thought that you want to delete a row in the database.
If all you want is to delete row in the datagrid, then delete row in the dataset and then rebind the datagrid

leave thsi code to determoine the row number
Private Sub dg_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles dg.MouseDown
        ' X & Y are in the grid' coordinates. If they are in screen coordinates, call dataGrid1.PointToClient method
        Dim pt = New Point(e.X, e.Y)
        Dim hti As DataGrid.HitTestInfo = dg.HitTest(pt)

        If hti.Type = DataGrid.HitTestType.Cell Then
            'row number
            MsgBox((dg.CurrentCell.RowNumber).ToString)
        End If


'then delete row from dataset
ds.Tables(0).Rows(dg.CurrentCell.RowNumber).Delete

then rebind datagrid
        dg.DataSource = ""
        dg.DataSource = ds.Tables(0)

I think it must work. Writing right here (did not check)

Author

Commented:
Assuming Name of Datagrid = DATAGRID1 I have the following code
=================================================
I get an exception Error at  Datagrid1.datasource="" /Error: An unhandled exception of type 'System.InvalidCastException' occurred in system.windows.forms.dll


    Dim pt = New Point(e.X, e.Y)
        Dim hti As DataGrid.HitTestInfo = DataGrid1.HitTest(pt)

        If hti.Type = DataGrid1.HitTestType.Cell Then
            'row number
            MsgBox((DataGrid1.CurrentCell.RowNumber).ToString)  '----->>>>>CONTROL DOES NOT GO TO HERE
        End If


        'then delete row from dataset
        ds.Tables(0).Rows(DataGrid1.CurrentCell.RowNumber).Delete()

        DataGrid1.DataSource = ""    ' ------------>>>>>>>>>>>>>though it deletes I get error HERE
        DataGrid1.DataSource = ds.Tables(0)
       
    End Sub
sorry. my mistake use to clear datagrid
DataGrid1.DataSource = Nothing

Author

Commented:
ok...I had 2 rows and 2 columns as done earlier .......I deleted the bottom row .........worked fine. again i wanted to remove the first one too ..........another Error comes up :  An unhandled exception of type 'System.Data.DeletedRowInaccessibleException' occurred in system.data.dll
Additional information: Can't delete this row since it's already deleted.

Error comes at     ds.Tables(0).Rows(DataGrid1.CurrentCell.RowNumber).Delete()
Code :
Private Sub DataGrid1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DataGrid1.MouseDown
       
        Dim pt = New Point(e.X, e.Y)
        Dim hti As DataGrid.HitTestInfo = DataGrid1.HitTest(pt)

        If hti.Type = DataGrid1.HitTestType.Cell Then
            'row number
            MsgBox((DataGrid1.CurrentCell.RowNumber).ToString)
        End If


        'then delete row from dataset
        ds.Tables(0).Rows(DataGrid1.CurrentCell.RowNumber).Delete()



        DataGrid1.DataSource = Nothing
        DataGrid1.DataSource = ds.Tables(0)


         
    End Sub
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
ok not a problem take ur time...thx for your prompt replies. i appreciate that

Author

Commented:
Excellent.........it works ......pls go ahead with ur work :)
ok Itries and this works fine

 Private Sub dg_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles dg.MouseDown

        Dim pt = New Point(e.X, e.Y)
        Dim hti As DataGrid.HitTestInfo = dg.HitTest(pt)

        If hti.Type = dg.HitTestType.Cell Then
            'row number
            MsgBox((dg.CurrentCell.RowNumber).ToString)
        'then delete row from dataset
        ds.Tables(0).Rows(dg.CurrentCell.RowNumber).Delete()

        ds.AcceptChanges()

        dg.DataSource = Nothing
        dg.DataSource = ds.Tables(0)
 End If

this line did the trick
        ds.AcceptChanges()

    End Sub



3 things to consider
1.You have to hit  the cell in order If hti.Type = dg.HitTestType.Cell Then to work
 2.If you want to hit anywhere then delete if statement
  If hti.Type = dg.HitTestType.Cell Then
3.You don't want to delete here (it is just the test)
when you hit the row save the row number in the variable and in another procedure (like button click)
do the actual deleting (you can include the messagebox confirmation - Do you want to delete this row?)
Because if you leave it as is user might accidently click on the grid and delete the row

Commented:
I was starting to like ExecuteBobQuery!  Some of my best friends are Bob!
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.