CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums
Results 1 to 2 of 2
  1. #1
    Join Date
    Nov 2018

    how to search from textbox and fill specific columns of datagridview from sql databas

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

  2. #2
    DataMiser is offline Super Moderator Power Poster
    Join Date
    Jul 2008

    Re: how to search from textbox and fill specific columns of datagridview from sql dat

    I'm not sure what that loop would actually do as coded. Granted I have not did much with binding in VB.Net nor with the data grid but it would seem to me that you would either bind the grid to your datatable or for go the data table and just loop through the reader data adding each row to the grid
    Always use [code][/code] tags when posting code.

Tags for this Thread

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