|
-
Oct 6th, 2011, 10:44 AM
#1
Thread Starter
Addicted Member
[RESOLVED] Datagridview Select
Im new with datagridview. . . .and i want to delete a certain row or the user will select a item in datagrid but im find it hard to do it. . . .here's my code hope that u can elp me with this. . . .
Code:
If dread.HasRows Then
Dim cmd1 As New MySqlCommand("DELETE FROM product WHERE p_code = '" & listgrid.SelectedRows(0).Selected & "'", conn1)
d = cmd1.ExecuteReader
For Each row As DataGridViewRow In listgrid.SelectedRows
listgrid.Rows.Remove(row)
Next
End If
-
Oct 6th, 2011, 10:50 AM
#2
Re: Datagridview Select
Code:
listgrid.SelectedRows ??
so here if the row is selected then only the particular row should be removed
how the selected is identified, is there any check box column in it
-
Oct 6th, 2011, 10:59 AM
#3
Thread Starter
Addicted Member
Re: Datagridview Select
 Originally Posted by make me rain
Code:
listgrid.SelectedRows ??
so here if the row is selected then only the particular row should be removed
how the selected is identified, is there any check box column in it
no there are no check box in my datagridview. . . .im new with datagrid. . . .can u show ho to delete it and will update datagrid??i have my database. . . .
-
Oct 6th, 2011, 11:09 AM
#4
Re: Datagridview Select
it is in your code
Code:
DELETE FROM product WHERE p_code = '" & listgrid.SelectedRows(0).Selected & "'", conn1)
d = cmd1.ExecuteReader
after loading your data in to the datagridview
(1) you need to give a specific way to the user to select the row or rows
(2) one way of doing it is providing the check box columns in the grid
(3) and if the user selects any such row ( i mean if selected is true) then that row should be deleted from the database + from grid as well
so how you are populating your grid
-
Oct 6th, 2011, 11:12 AM
#5
Thread Starter
Addicted Member
Re: Datagridview Select
 Originally Posted by make me rain
it is in your code
Code:
DELETE FROM product WHERE p_code = '" & listgrid.SelectedRows(0).Selected & "'", conn1)
d = cmd1.ExecuteReader
after loading your data in to the datagridview
(1) you need to give a specific way to the user to select the row or rows
(2) one way of doing it is providing the check box columns in the grid
(3) and if the user selects any such row ( i mean if selected is true) then that row should be deleted from the database + from grid as well
so how you are populating your grid
can you at least show me how to do it . . . .
this is how i populate my grid. . . .
Code:
listgrid.Rows.Item(i).Cells(0).Value = txtquantity.Text
listgrid.Rows.Item(i).Cells(1).Value = txtdsprod.Text
listgrid.Rows.Item(i).Cells(2).Value = txtprice.Text
listgrid.Rows.Item(i).Cells(3).Value = txtSubtotal.Text
-
Oct 6th, 2011, 11:20 AM
#6
-
Oct 6th, 2011, 11:42 AM
#7
Re: Datagridview Select
I don't know what & how you are doing the database operations, with out going in to that i am just showing the removing and adding of the rows in datagridview control
fins the attachment here with
Last edited by make me rain; Nov 3rd, 2011 at 11:50 AM.
-
Oct 7th, 2011, 09:01 AM
#8
Thread Starter
Addicted Member
Re: Datagridview Select
it was a useful information sir but i can edit the value of the datagrid how can i disable it without??when i disable it i cant click the remove item. . . .I have a database and i want to delete the selected rows and the user cant edit the datagrid. . . .
-
Oct 7th, 2011, 09:23 AM
#9
Re: Datagridview Select
put it in your form load event
vb Code:
With Me.DataGridView1 .Columns(0).ReadOnly = True .Columns(1).ReadOnly = True .Columns(2).ReadOnly = True .Columns(3).ReadOnly = True End With
-
Oct 7th, 2011, 12:34 PM
#10
Thread Starter
Addicted Member
Re: Datagridview Select
it's working now. . . .but how could i do this with a database. . . .i have a database and i want to update the database everytime i delete a item in datagridview. . . .
-
Oct 7th, 2011, 12:39 PM
#11
-
Oct 7th, 2011, 12:57 PM
#12
Re: Datagridview Select
fallow the steps
(1) add one more column in the attached datagridview control
(2) alter this sub routine as below
vb Code:
Private Sub Btn_drop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_drop.Click Dim I As Int16 Dim ISrowSelected As Boolean Dim TTLrows As Int16 Dim DeleteID As Long With Me.DataGridView1 TTLrows = .RowCount - 1 For I = TTLrows To 0 Step -1 ISrowSelected = .Rows(I).Cells(5).Value DeleteID = .Rows(I).Cells(4).Value ' not here you need to specify 'which column containes the primary key / unique key value 'that is .Cells(4).value replace 4 accordingly If ISrowSelected = True Then Call DelteDBrecord(DeleteID) ' this sub routine will delete records in database 'for editing you need to just alter your SQL 'That is SQL_drop variable in sub routine DelteDBrecord 'thats it .Rows.Remove(.Rows(I)) End If Next End With End Sub
(3) add this sub routine in to your project
vb Code:
Private Sub DelteDBrecord(ByVal uniqueValue As Long) 'unique ID is may be a autonumber field 'or the primary key column Dim SQL_drop As String = "DELETE FROM databnaseTable WHERE databnaseTable.FieldValue = @uniqueValue;" 'create the MySQL command object as shown below with the proper arguments Dim CMD_drop As New mysqlcommand(SQL_drop, YourConnectionobject) With CMD_drop .parameters.addwithvalue("@uniqueValue", uniqueValue) 'OpenAccess your connection here .executenonquery() 'Close your connection here .dispose() 'now this dewletes the vlues in database 'the unique key (primary key is datagrid view column 5 value End With End Sub
if the post helps you please close the thread
-
Oct 7th, 2011, 02:15 PM
#13
Thread Starter
Addicted Member
Re: Datagridview Select
Thank you sir. . . I have a one last question how do i populate the datagrid with my data in my database programatically without using datasource of the datagrid. . . .Im new with datagrid hope that you can elp me sir . . . .
-
Oct 7th, 2011, 06:11 PM
#14
Thread Starter
Addicted Member
Re: Datagridview Select
Im confuse with this line what it means. . . .this is my 1st tym to encounter this. . . .can u explain it to me. . . .plzzzz. . . .
Code:
databnaseTable.FieldValue = @uniqueValue
-
Oct 7th, 2011, 09:14 PM
#15
Re: Datagridview Select
past this sub in any module or at the top of your form class
vb Code:
Public Sub FillGrid(ByVal DgV As DataGridView, ByVal SQL As String, ByVal DGVcol As Int16, Optional ByVal PARA As String = "") 'intx 'fills datagridview with / without criteria Dim WITHcry As Boolean If PARA Is Nothing OrElse PARA = "" Then WITHcry = False Else WITHcry = True End If Dim i, j As Integer Dim CMD_withCry As New MySqlCommand(SQL, yourconnectionObject) Dim Da_FillGrid As New MySqlDataAdapter(CMD_withCry) Dim Ds_FillGrid As New DataSet If WITHcry = True Then CMD_withCry.Parameters.AddWithValue("@PARA", PARA) End If open your connection here Da_FillGrid.Fill(Ds_FillGrid, "T1") close your connection here Dim cOLC As Int16 = DGVcol 'DgV.Columns.Count DgV.Rows.Clear() With Ds_FillGrid.Tables("T1") j = .Rows.Count GridFillerCount = j If j <= 0 Then Exit Sub Else DgV.Rows.Clear() DgV.Rows.Add(j) For i = 0 To cOLC - 1 For K = 0 To j - 1 DgV.Rows(K).Cells(i).Value = .Rows(K).Item(i).ToString Next Next End If End With CMD_withCry.Dispose() Da_FillGrid.Dispose() Ds_FillGrid.Dispose() End Sub
this is how to fill the datagridview
vb Code:
Dim SQL_CurrentHolding As String = "SELECT * FROM current_hldg_grid;" Call FillGrid(Me.DGV_Holding, SQL_CurrentHolding, 8) note here 'fill the arguments correctly (1) SQL statement (2) if your SQL statement is having any parameter then use it as (a) SELECT * FROM current_hldg_grid WHERE current_hldg_grid.username = @PARA;" (2)pass your datagridview control name (3) pass number of columns to fill for example 6 columns ( exclude check box column) (4) pass the parameter value ( optional) if it is there in your SQL statement
-
Oct 7th, 2011, 09:28 PM
#16
Re: Datagridview Select
databnaseTable.FieldValue = @uniqueValue
this is the SQL statement , it is like a verb in english,
what it does is
for ex:
assume there is table by name usertable
and username is a field in table usertable
Code:
SELECT username from usertable;
this gives all the username in the table usertable
if you want to see a particular user in the table then the SYNTAX will be like
Code:
SELECT fieldname FROM tablename WHERE tablename.fieldname = 'SUKOI';
this means you are searching the table / looking for a record by criteria as 'SUKOI'
Accordingly when ever you are
fetching record
editng record
deleting record
you need to tell the SQL where exactly it should go & after going in to particular record what it should do
that is what is this
Code:
"DELETE FROM databnaseTable WHERE databnaseTable.FieldValue = @uniqueValue;"
because you don't know what is the @uniqueValue
it will be fetched while the application run, that is you are deleting only the rows which are selected by the user ( check box column)
so the @ indicates the parameter , & this will be passed during the runtime
that is waht this
vb Code:
With CMD_drop .parameters.addwithvalue("@uniqueValue", uniqueValue)
after passing the parameter you are executing the query
that is what this
vb Code:
.executenonquery() 'Close your connection here
-
Oct 7th, 2011, 11:10 PM
#17
Thread Starter
Addicted Member
Re: [RESOLVED] Datagridview Select
thanks 4 the info make me rain. . . .u elp me alot. . . .
-
Oct 7th, 2011, 11:17 PM
#18
Re: [RESOLVED] Datagridview Select
Sukoi with out fail
you need to rate this good , bad meaning full, meaning less, understandable , hope less, outdated
etc....
this improves many thing on the forum
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|