Results 1 to 18 of 18

Thread: [RESOLVED] Datagridview Select

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2011
    Posts
    137

    Resolved [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

  2. #2
    PowerPoster make me rain's Avatar
    Join Date
    Sep 2008
    Location
    india/Hubli
    Posts
    2,208

    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
    The averted nuclear war
    My notes:

    PrOtect your PC. MSDN Functions .OOP LINUX forum
    .LINQ LINQ videous
    If some one helps you please rate them with out fail , forum doesn't expects any thing other than this

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Mar 2011
    Posts
    137

    Re: Datagridview Select

    Quote Originally Posted by make me rain View Post
    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. . . .

  4. #4
    PowerPoster make me rain's Avatar
    Join Date
    Sep 2008
    Location
    india/Hubli
    Posts
    2,208

    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
    The averted nuclear war
    My notes:

    PrOtect your PC. MSDN Functions .OOP LINUX forum
    .LINQ LINQ videous
    If some one helps you please rate them with out fail , forum doesn't expects any thing other than this

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Mar 2011
    Posts
    137

    Re: Datagridview Select

    Quote Originally Posted by make me rain View Post
    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

  6. #6
    PowerPoster make me rain's Avatar
    Join Date
    Sep 2008
    Location
    india/Hubli
    Posts
    2,208

    Re: Datagridview Select

    Ok wait a minute
    The averted nuclear war
    My notes:

    PrOtect your PC. MSDN Functions .OOP LINUX forum
    .LINQ LINQ videous
    If some one helps you please rate them with out fail , forum doesn't expects any thing other than this

  7. #7
    PowerPoster make me rain's Avatar
    Join Date
    Sep 2008
    Location
    india/Hubli
    Posts
    2,208

    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.
    The averted nuclear war
    My notes:

    PrOtect your PC. MSDN Functions .OOP LINUX forum
    .LINQ LINQ videous
    If some one helps you please rate them with out fail , forum doesn't expects any thing other than this

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Mar 2011
    Posts
    137

    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. . . .

  9. #9
    PowerPoster make me rain's Avatar
    Join Date
    Sep 2008
    Location
    india/Hubli
    Posts
    2,208

    Re: Datagridview Select

    put it in your form load event

    vb Code:
    1. With Me.DataGridView1
    2.             .Columns(0).ReadOnly = True
    3.             .Columns(1).ReadOnly = True
    4.             .Columns(2).ReadOnly = True
    5.             .Columns(3).ReadOnly = True
    6.         End With
    The averted nuclear war
    My notes:

    PrOtect your PC. MSDN Functions .OOP LINUX forum
    .LINQ LINQ videous
    If some one helps you please rate them with out fail , forum doesn't expects any thing other than this

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Mar 2011
    Posts
    137

    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. . . .

  11. #11
    PowerPoster make me rain's Avatar
    Join Date
    Sep 2008
    Location
    india/Hubli
    Posts
    2,208

    Re: Datagridview Select

    wait a minute
    The averted nuclear war
    My notes:

    PrOtect your PC. MSDN Functions .OOP LINUX forum
    .LINQ LINQ videous
    If some one helps you please rate them with out fail , forum doesn't expects any thing other than this

  12. #12
    PowerPoster make me rain's Avatar
    Join Date
    Sep 2008
    Location
    india/Hubli
    Posts
    2,208

    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:
    1. Private Sub Btn_drop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_drop.Click
    2.  
    3.         Dim I As Int16
    4.         Dim ISrowSelected As Boolean
    5.         Dim TTLrows As Int16
    6.  
    7.         Dim DeleteID As Long
    8.  
    9.         With Me.DataGridView1
    10.             TTLrows = .RowCount - 1
    11.             For I = TTLrows To 0 Step -1
    12.  
    13.                 ISrowSelected = .Rows(I).Cells(5).Value
    14.                 DeleteID = .Rows(I).Cells(4).Value ' not here you need to specify
    15.                 'which column containes the primary key / unique key value
    16.                 'that is .Cells(4).value replace 4 accordingly
    17.  
    18.                 If ISrowSelected = True Then
    19.  
    20.                     Call DelteDBrecord(DeleteID) ' this sub routine will delete records in database
    21.                     'for editing you need to just alter your SQL
    22.                     'That is SQL_drop variable in sub routine DelteDBrecord
    23.                     'thats it
    24.                     .Rows.Remove(.Rows(I))
    25.                 End If
    26.             Next
    27.         End With
    28.     End Sub

    (3) add this sub routine in to your project
    vb Code:
    1. Private Sub DelteDBrecord(ByVal uniqueValue As Long)
    2.         'unique ID is may be a autonumber field
    3.         'or the primary key column
    4.  
    5.         Dim SQL_drop As String = "DELETE FROM databnaseTable WHERE databnaseTable.FieldValue = @uniqueValue;"
    6.  
    7.         'create the MySQL command object as shown below with the proper arguments
    8.         Dim CMD_drop As New mysqlcommand(SQL_drop, YourConnectionobject)
    9.  
    10.         With CMD_drop
    11.             .parameters.addwithvalue("@uniqueValue", uniqueValue)
    12.             'OpenAccess your connection here
    13.             .executenonquery()
    14.             'Close your connection here
    15.             .dispose()
    16.             'now this dewletes the vlues in database
    17.             'the unique key (primary key is datagrid view column 5 value
    18.         End With
    19.  
    20.     End Sub

    if the post helps you please close the thread
    The averted nuclear war
    My notes:

    PrOtect your PC. MSDN Functions .OOP LINUX forum
    .LINQ LINQ videous
    If some one helps you please rate them with out fail , forum doesn't expects any thing other than this

  13. #13

    Thread Starter
    Addicted Member
    Join Date
    Mar 2011
    Posts
    137

    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 . . . .

  14. #14

    Thread Starter
    Addicted Member
    Join Date
    Mar 2011
    Posts
    137

    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

  15. #15
    PowerPoster make me rain's Avatar
    Join Date
    Sep 2008
    Location
    india/Hubli
    Posts
    2,208

    Re: Datagridview Select

    past this sub in any module or at the top of your form class

    vb Code:
    1. Public Sub FillGrid(ByVal DgV As DataGridView, ByVal SQL As String, ByVal DGVcol As Int16, Optional ByVal PARA As String = "")
    2.         'intx
    3.         'fills datagridview with / without criteria
    4.  
    5.  
    6.         Dim WITHcry As Boolean
    7.  
    8.         If PARA Is Nothing OrElse PARA = "" Then
    9.             WITHcry = False
    10.         Else
    11.             WITHcry = True
    12.         End If
    13.  
    14.         Dim i, j As Integer
    15.         Dim CMD_withCry As New MySqlCommand(SQL, yourconnectionObject)
    16.  
    17.         Dim Da_FillGrid As New MySqlDataAdapter(CMD_withCry)
    18.         Dim Ds_FillGrid As New DataSet
    19.  
    20.         If WITHcry = True Then
    21.             CMD_withCry.Parameters.AddWithValue("@PARA", PARA)
    22.         End If
    23.         open your connection here
    24.         Da_FillGrid.Fill(Ds_FillGrid, "T1")
    25.         close your connection here
    26.  
    27.         Dim cOLC As Int16 = DGVcol 'DgV.Columns.Count
    28.         DgV.Rows.Clear()
    29.  
    30.         With Ds_FillGrid.Tables("T1")
    31.             j = .Rows.Count
    32.             GridFillerCount = j
    33.             If j <= 0 Then
    34.                 Exit Sub
    35.             Else
    36.                 DgV.Rows.Clear()
    37.                 DgV.Rows.Add(j)
    38.  
    39.                 For i = 0 To cOLC - 1
    40.                     For K = 0 To j - 1
    41.                         DgV.Rows(K).Cells(i).Value = .Rows(K).Item(i).ToString
    42.                     Next
    43.                 Next
    44.             End If
    45.         End With
    46.  
    47.         CMD_withCry.Dispose()
    48.         Da_FillGrid.Dispose()
    49.         Ds_FillGrid.Dispose()
    50.  
    51.  
    52.     End Sub

    this is how to fill the datagridview

    vb Code:
    1. Dim SQL_CurrentHolding As String = "SELECT * FROM current_hldg_grid;"
    2.         Call FillGrid(Me.DGV_Holding, SQL_CurrentHolding, 8)
    3.  
    4. note here
    5. 'fill the arguments correctly
    6. (1) SQL statement
    7. (2) if your SQL statement is having any parameter then use it as
    8.      (a) SELECT * FROM current_hldg_grid WHERE current_hldg_grid.username = @PARA;"
    9. (2)pass your datagridview control name
    10. (3) pass number of columns to fill for example 6 columns ( exclude check box column)
    11. (4) pass the parameter value ( optional) if it is there in your SQL statement
    The averted nuclear war
    My notes:

    PrOtect your PC. MSDN Functions .OOP LINUX forum
    .LINQ LINQ videous
    If some one helps you please rate them with out fail , forum doesn't expects any thing other than this

  16. #16
    PowerPoster make me rain's Avatar
    Join Date
    Sep 2008
    Location
    india/Hubli
    Posts
    2,208

    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:
    1. With CMD_drop
    2.             .parameters.addwithvalue("@uniqueValue", uniqueValue)
    after passing the parameter you are executing the query
    that is what this
    vb Code:
    1. .executenonquery()
    2.             'Close your connection here
    The averted nuclear war
    My notes:

    PrOtect your PC. MSDN Functions .OOP LINUX forum
    .LINQ LINQ videous
    If some one helps you please rate them with out fail , forum doesn't expects any thing other than this

  17. #17

    Thread Starter
    Addicted Member
    Join Date
    Mar 2011
    Posts
    137

    Re: [RESOLVED] Datagridview Select

    thanks 4 the info make me rain. . . .u elp me alot. . . .

  18. #18
    PowerPoster make me rain's Avatar
    Join Date
    Sep 2008
    Location
    india/Hubli
    Posts
    2,208

    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
    The averted nuclear war
    My notes:

    PrOtect your PC. MSDN Functions .OOP LINUX forum
    .LINQ LINQ videous
    If some one helps you please rate them with out fail , forum doesn't expects any thing other than this

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width