how to search from textbox and fill specific columns of datagridview from sql database? RRS feed

  • Question

  • i am trying to search itemcode by typing in textbox and i want to get specific coloums in same row from sql database. i have made coloumns with headers in datagridview and i want that searched data in that specific datagridview coloumns. my code is searching but adding new columns next to already made columns in datagridview but i want only searched data in already made columns. please tell me how to do this?

     Private Sub Button13_Click(sender As Object, e As EventArgs) Handles Button13.Click
            On Error Resume Next
            Using cn As New SqlConnection("server= PANKAJ\SQLEXPRESS; database = pankaj billing software; integrated security=true")
                Using cmd2 As New SqlCommand("select itemcode As 'Item Code', item,qty As Quantity, weight as Weight from stockdata Where itemcode = @itemcode;", cn)
                    cmd2.Parameters.AddWithValue("@itemcode", TextBox1.Text)
                    Dim dr As SqlDataReader = cmd2.ExecuteReader()
                    DataGridView1.DataSource = dt
                    For Each row As DataGridViewRow In DataGridView1.Rows
                        cmd2.Parameters.Add("@item", SqlDbType.VarChar)
                        cmd2.Parameters.Add("@qty", SqlDbType.VarChar)
                        cmd2.Parameters.Add("@weight", SqlDbType.VarChar)
                        With cmd2
                            row.Cells(1).Value = .Parameters("@item").Value
                            row.Cells(2).Value = .Parameters("@qty").Value
                            row.Cells(2).Value = .Parameters("@weight").Value
                        End With
                End Using
            End Using

    Saturday, November 24, 2018 12:30 AM

All replies

  • Please be clear about what is making new DataGridViewColumns and why use ExecuteNonQuery for a SELECT statement where you should be using ExecuteReader as you did prior to dt.load yet then use cmd2.ExecuteNonQuery after the With statement, that makes no sense.

    Lastly, I highly recommend not using On Error Resume Next but instead use nothing or use Try/Catch and make sure to not leave the Catch empty.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Saturday, November 24, 2018 2:27 AM
  • Hi,

    Do you want to put the results of the query into the columns specified by the datagridview?

    DataGridView1.AutoGenerateColumns = False DataGridView1.Columns("Column1").DataPropertyName = dt.Columns("Item Code").ToString DataGridView1.Columns("Column2").DataPropertyName = dt.Columns("Item").ToString DataGridView1.Columns("Column3").DataPropertyName = dt.Columns("Quantity").ToString DataGridView1.Columns("Column4").DataPropertyName = dt.Columns("Weight").ToString

     DataGridView1.Columns("Item Code").Visible = False
            DataGridView1.Columns("Item").Visible = False
            DataGridView1.Columns("Quantity").Visible = False
            DataGridView1.Columns("Weight").Visible = False

    Best Regards,


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact

    Tuesday, November 27, 2018 7:08 AM