sainavya1215
asked on
To Author : iboutckine
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)
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)
ASKER
2Q) column values should not be changed by user.... a row can be deleted but columns cannot be edited though
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.Mouse EventArgs) 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.Row Number).To String)
'col number
MsgBox((dg.CurrentCell.Col umnNumber) .ToString)
Else
'header name ( column)
If hti.Type = DataGrid.HitTestType.Colum nHeader Then
MsgBox(ds.Tables(0).Column s(hti.Colu mn).ToStri ng)'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
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.Mouse
' 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
'row number
MsgBox((dg.CurrentCell.Row
'col number
MsgBox((dg.CurrentCell.Col
Else
'header name ( column)
If hti.Type = DataGrid.HitTestType.Colum
MsgBox(ds.Tables(0).Column
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:(
instead
ExecuteBobQuery.
read
ExecuteNonQuery
typing is not my strongest skill:(
ASKER
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).It em(0)
MsgBox("serial_number=" & serial_number)
end_serial_number = ds.Tables(0).Rows(iRow).It em(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
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
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).It
MsgBox("serial_number=" & serial_number)
end_serial_number = ds.Tables(0).Rows(iRow).It
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.Mouse EventArgs) 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.Row Number).To String)
End If
'then delete row from dataset
ds.Tables(0).Rows(dg.Curre ntCell.Row Number).De lete
then rebind datagrid
dg.DataSource = ""
dg.DataSource = ds.Tables(0)
I think it must work. Writing right here (did not check)
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.Mouse
' 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.Row
End If
'then delete row from dataset
ds.Tables(0).Rows(dg.Curre
then rebind datagrid
dg.DataSource = ""
dg.DataSource = ds.Tables(0)
I think it must work. Writing right here (did not check)
ASKER
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.InvalidCastExcepti on' 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.CurrentC ell.RowNum ber).ToStr ing) '----->>>>>CONTROL DOES NOT GO TO HERE
End If
'then delete row from dataset
ds.Tables(0).Rows(DataGrid 1.CurrentC ell.RowNum ber).Delet e()
DataGrid1.DataSource = "" ' ------------>>>>>>>>>>>>>t hough it deletes I get error HERE
DataGrid1.DataSource = ds.Tables(0)
End Sub
==========================
I get an exception Error at Datagrid1.datasource="" /Error: An unhandled exception of type 'System.InvalidCastExcepti
Dim pt = New Point(e.X, e.Y)
Dim hti As DataGrid.HitTestInfo = DataGrid1.HitTest(pt)
If hti.Type = DataGrid1.HitTestType.Cell
'row number
MsgBox((DataGrid1.CurrentC
End If
'then delete row from dataset
ds.Tables(0).Rows(DataGrid
DataGrid1.DataSource = "" ' ------------>>>>>>>>>>>>>t
DataGrid1.DataSource = ds.Tables(0)
End Sub
sorry. my mistake use to clear datagrid
DataGrid1.DataSource = Nothing
DataGrid1.DataSource = Nothing
ASKER
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.DeletedRowIna ccessibleE xception' occurred in system.data.dll
Additional information: Can't delete this row since it's already deleted.
Error comes at ds.Tables(0).Rows(DataGrid 1.CurrentC ell.RowNum ber).Delet e()
Code :
Private Sub DataGrid1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.Mouse EventArgs) 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.CurrentC ell.RowNum ber).ToStr ing)
End If
'then delete row from dataset
ds.Tables(0).Rows(DataGrid 1.CurrentC ell.RowNum ber).Delet e()
DataGrid1.DataSource = Nothing
DataGrid1.DataSource = ds.Tables(0)
End Sub
Additional information: Can't delete this row since it's already deleted.
Error comes at ds.Tables(0).Rows(DataGrid
Code :
Private Sub DataGrid1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.Mouse
Dim pt = New Point(e.X, e.Y)
Dim hti As DataGrid.HitTestInfo = DataGrid1.HitTest(pt)
If hti.Type = DataGrid1.HitTestType.Cell
'row number
MsgBox((DataGrid1.CurrentC
End If
'then delete row from dataset
ds.Tables(0).Rows(DataGrid
DataGrid1.DataSource = Nothing
DataGrid1.DataSource = ds.Tables(0)
End Sub
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
ok not a problem take ur time...thx for your prompt replies. i appreciate that
ASKER
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.Mouse EventArgs) 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.Row Number).To String)
'then delete row from dataset
ds.Tables(0).Rows(dg.Curre ntCell.Row Number).De lete()
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
Private Sub dg_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.Mouse
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.Row
'then delete row from dataset
ds.Tables(0).Rows(dg.Curre
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
I was starting to like ExecuteBobQuery! Some of my best friends are Bob!
ASKER
hi iboutchkine,
Question posted on working grid
https://www.experts-exchange.com/questions/20955420/author-iboutchkine.html
Question posted on working grid
https://www.experts-exchange.com/questions/20955420/author-iboutchkine.html
ASKER
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