How to search data from textbox and fill specific coloumns of datagridview from sql database?











up vote
0
down vote

favorite












I am trying to search itemcode by typing in textbox and i want to get specific columns in same row from SQL database. I have made columns with headers in datagridview and i want that searched data in that specific datagridview columns. I have written code but it's not working. I do not know how to do this. I am new and trying to learn vb. Please give some suggestion.



This is my code:



Private Sub Button13_Click(sender As Object, e As EventArgs) Handles Button13.Click

Using cn As New SqlConnection("server= PANKAJSQLEXPRESS; 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)
cn.Open()
Dim dr As SqlDataReader = cmd2.ExecuteReader()
dt.Load(dr)
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
cmd2.ExecuteNonQuery()
Next
End Using
End Using
End Sub









share|improve this question
























  • That is not a valid SQL select statement; start by getting the basic SQL syntax correct. Then work on how many parameters you need in the statement, and how to provide them.
    – Andy G
    Nov 22 at 16:39










  • Can you tell me how to correct this code?
    – pankaj babbar
    Nov 22 at 17:22










  • CellLeave is the wrong event to handle. You should be handling CellValueChanged. As for the SQL, you don't need us to tell you what to do when it's so easy to find information for yourself. Do some reading on SQL syntax. As it is, you have half a SELECT and half an INSERT. If you can read, you can find out how to write a proper SELECT statement. That's basic stuff that you shouldn't need us to teach you.
    – jmcilhinney
    Nov 22 at 22:51















up vote
0
down vote

favorite












I am trying to search itemcode by typing in textbox and i want to get specific columns in same row from SQL database. I have made columns with headers in datagridview and i want that searched data in that specific datagridview columns. I have written code but it's not working. I do not know how to do this. I am new and trying to learn vb. Please give some suggestion.



This is my code:



Private Sub Button13_Click(sender As Object, e As EventArgs) Handles Button13.Click

Using cn As New SqlConnection("server= PANKAJSQLEXPRESS; 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)
cn.Open()
Dim dr As SqlDataReader = cmd2.ExecuteReader()
dt.Load(dr)
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
cmd2.ExecuteNonQuery()
Next
End Using
End Using
End Sub









share|improve this question
























  • That is not a valid SQL select statement; start by getting the basic SQL syntax correct. Then work on how many parameters you need in the statement, and how to provide them.
    – Andy G
    Nov 22 at 16:39










  • Can you tell me how to correct this code?
    – pankaj babbar
    Nov 22 at 17:22










  • CellLeave is the wrong event to handle. You should be handling CellValueChanged. As for the SQL, you don't need us to tell you what to do when it's so easy to find information for yourself. Do some reading on SQL syntax. As it is, you have half a SELECT and half an INSERT. If you can read, you can find out how to write a proper SELECT statement. That's basic stuff that you shouldn't need us to teach you.
    – jmcilhinney
    Nov 22 at 22:51













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I am trying to search itemcode by typing in textbox and i want to get specific columns in same row from SQL database. I have made columns with headers in datagridview and i want that searched data in that specific datagridview columns. I have written code but it's not working. I do not know how to do this. I am new and trying to learn vb. Please give some suggestion.



This is my code:



Private Sub Button13_Click(sender As Object, e As EventArgs) Handles Button13.Click

Using cn As New SqlConnection("server= PANKAJSQLEXPRESS; 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)
cn.Open()
Dim dr As SqlDataReader = cmd2.ExecuteReader()
dt.Load(dr)
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
cmd2.ExecuteNonQuery()
Next
End Using
End Using
End Sub









share|improve this question















I am trying to search itemcode by typing in textbox and i want to get specific columns in same row from SQL database. I have made columns with headers in datagridview and i want that searched data in that specific datagridview columns. I have written code but it's not working. I do not know how to do this. I am new and trying to learn vb. Please give some suggestion.



This is my code:



Private Sub Button13_Click(sender As Object, e As EventArgs) Handles Button13.Click

Using cn As New SqlConnection("server= PANKAJSQLEXPRESS; 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)
cn.Open()
Dim dr As SqlDataReader = cmd2.ExecuteReader()
dt.Load(dr)
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
cmd2.ExecuteNonQuery()
Next
End Using
End Using
End Sub






vb.net






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 8 at 15:00









marc_s

568k12810991249




568k12810991249










asked Nov 22 at 16:27









pankaj babbar

136




136












  • That is not a valid SQL select statement; start by getting the basic SQL syntax correct. Then work on how many parameters you need in the statement, and how to provide them.
    – Andy G
    Nov 22 at 16:39










  • Can you tell me how to correct this code?
    – pankaj babbar
    Nov 22 at 17:22










  • CellLeave is the wrong event to handle. You should be handling CellValueChanged. As for the SQL, you don't need us to tell you what to do when it's so easy to find information for yourself. Do some reading on SQL syntax. As it is, you have half a SELECT and half an INSERT. If you can read, you can find out how to write a proper SELECT statement. That's basic stuff that you shouldn't need us to teach you.
    – jmcilhinney
    Nov 22 at 22:51


















  • That is not a valid SQL select statement; start by getting the basic SQL syntax correct. Then work on how many parameters you need in the statement, and how to provide them.
    – Andy G
    Nov 22 at 16:39










  • Can you tell me how to correct this code?
    – pankaj babbar
    Nov 22 at 17:22










  • CellLeave is the wrong event to handle. You should be handling CellValueChanged. As for the SQL, you don't need us to tell you what to do when it's so easy to find information for yourself. Do some reading on SQL syntax. As it is, you have half a SELECT and half an INSERT. If you can read, you can find out how to write a proper SELECT statement. That's basic stuff that you shouldn't need us to teach you.
    – jmcilhinney
    Nov 22 at 22:51
















That is not a valid SQL select statement; start by getting the basic SQL syntax correct. Then work on how many parameters you need in the statement, and how to provide them.
– Andy G
Nov 22 at 16:39




That is not a valid SQL select statement; start by getting the basic SQL syntax correct. Then work on how many parameters you need in the statement, and how to provide them.
– Andy G
Nov 22 at 16:39












Can you tell me how to correct this code?
– pankaj babbar
Nov 22 at 17:22




Can you tell me how to correct this code?
– pankaj babbar
Nov 22 at 17:22












CellLeave is the wrong event to handle. You should be handling CellValueChanged. As for the SQL, you don't need us to tell you what to do when it's so easy to find information for yourself. Do some reading on SQL syntax. As it is, you have half a SELECT and half an INSERT. If you can read, you can find out how to write a proper SELECT statement. That's basic stuff that you shouldn't need us to teach you.
– jmcilhinney
Nov 22 at 22:51




CellLeave is the wrong event to handle. You should be handling CellValueChanged. As for the SQL, you don't need us to tell you what to do when it's so easy to find information for yourself. Do some reading on SQL syntax. As it is, you have half a SELECT and half an INSERT. If you can read, you can find out how to write a proper SELECT statement. That's basic stuff that you shouldn't need us to teach you.
– jmcilhinney
Nov 22 at 22:51












2 Answers
2






active

oldest

votes

















up vote
1
down vote













I used a TextBox located outside the DataGridView to enter the item code to search for. I added a Button to do the search and retrieve the data to a DataReader.



The DataReader then loads the DataTable which is declared as a form level (class level variable). We want to use the same DataTable every time we search so the items will be added to the grid. With the Load method if the DataTable already contains rows, the incoming data from the data source is merged with the existing rows. The DataTable is then bound to the DataGridView. Each time the user enters an item code in the TextBox and clicks the Search Button a new row will be added to the grid.



To make nicer looking Column headers use as alias in your Select statement. The As clause following the databse column name is the alias and will show up in the DataGridView as a header. If you have a space in your alias it must be enclosed in single quotes.



Private dt As DataTable

Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
dt = New DataTable()
End Sub

Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
Using cn As New SqlConnection("server= PANKAJSQLEXPRESS; 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", txtItemCode.Text)
cn.Open()
Dim dr As SqlDataReader = cmd2.ExecuteReader()
dt.Load(dr)
DataGridView1.DataSource = dt
End Using
End Using
End Sub


P.S. Very glad to see the use of parameters and Using blocks! :-)






share|improve this answer





















  • i have already made coloums in datagridview so when i search all this data by textbox it should be in specific cell or coloum of datagridview.
    – pankaj babbar
    Nov 23 at 4:35


















up vote
0
down vote













How about this?



'"C:your_pathNorthwind.mdb"
Imports System
Imports System.Data
Imports System.Data.OleDb
Imports System.Windows.Forms

Public Class Form1
Inherits System.Windows.Forms.Form

Private bindingSource1 As New BindingSource()
Private dataAdapter As New OleDbDataAdapter()

<STAThreadAttribute()> _
Public Shared Sub Main()
Application.Run(New Form1())
End Sub

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:your_pathNorthwind.mdb"
Dim selectCommand As String
Dim connection As New OleDbConnection(connectionString)

selectCommand = "Select * From MyExcelTable ORDER BY ID"
Me.dataAdapter = New OleDbDataAdapter(selectCommand, connection)

With DataGridView1
.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells
.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.ColumnHeader
.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.ColumnHeader
End With

Dim commandBuilder As New OleDbCommandBuilder(Me.dataAdapter)
Dim table As New DataTable()

table.Locale = System.Globalization.CultureInfo.InvariantCulture

Me.dataAdapter.Fill(table)
Me.bindingSource1.DataSource = table

Dim data As New DataSet()

data.Locale = System.Globalization.CultureInfo.InvariantCulture

DataGridView1.DataSource = Me.bindingSource1
Me.DataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.Aqua
Me.DataGridView1.AutoResizeColumns( _
DataGridViewAutoSizeColumnsMode.AllCells)

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click, btnUpdate.Click
Dim table As New DataTable()

Me.bindingSource1 = Me.DataGridView1.DataSource
table = Me.bindingSource1.DataSource

Me.dataAdapter.Update(table)
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click, btnClose.Click
Me.Close()
End Sub

Private Sub TextBox1_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox1.TextChanged, TextBox1.Click

Dim connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:your_pathNorthwind.mdb"
Dim selectCommand As String
Dim connection As New OleDbConnection(connectionString)

'selectCommand = "Select * From MyExcelTable where Fname = '" & TextBox1.Text & "'"
'"SELECT * FROM Customers WHERE Address LIKE '" & strAddressSearch & "%'"
'or ending with:
'"SELECT * FROM Customers WHERE Address LIKE '%" & strAddressSearch & "'"

selectCommand = "Select * From MyExcelTable where Fname Like '%" & TextBox1.Text & "%'"
Me.dataAdapter = New OleDbDataAdapter(selectCommand, connection)

With DataGridView1
.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells
.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.ColumnHeader
.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.ColumnHeader
End With

Dim commandBuilder As New OleDbCommandBuilder(Me.dataAdapter)
Dim table As New DataTable()

table.Locale = System.Globalization.CultureInfo.InvariantCulture

Me.dataAdapter.Fill(table)
Me.bindingSource1.DataSource = table

Dim data As New DataSet()

data.Locale = System.Globalization.CultureInfo.InvariantCulture

DataGridView1.DataSource = Me.bindingSource1
Me.DataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.Aqua
Me.DataGridView1.AutoResizeColumns( _
DataGridViewAutoSizeColumnsMode.AllCells)

End Sub
End Class


enter image description here






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53435014%2fhow-to-search-data-from-textbox-and-fill-specific-coloumns-of-datagridview-from%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote













    I used a TextBox located outside the DataGridView to enter the item code to search for. I added a Button to do the search and retrieve the data to a DataReader.



    The DataReader then loads the DataTable which is declared as a form level (class level variable). We want to use the same DataTable every time we search so the items will be added to the grid. With the Load method if the DataTable already contains rows, the incoming data from the data source is merged with the existing rows. The DataTable is then bound to the DataGridView. Each time the user enters an item code in the TextBox and clicks the Search Button a new row will be added to the grid.



    To make nicer looking Column headers use as alias in your Select statement. The As clause following the databse column name is the alias and will show up in the DataGridView as a header. If you have a space in your alias it must be enclosed in single quotes.



    Private dt As DataTable

    Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    dt = New DataTable()
    End Sub

    Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
    Using cn As New SqlConnection("server= PANKAJSQLEXPRESS; 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", txtItemCode.Text)
    cn.Open()
    Dim dr As SqlDataReader = cmd2.ExecuteReader()
    dt.Load(dr)
    DataGridView1.DataSource = dt
    End Using
    End Using
    End Sub


    P.S. Very glad to see the use of parameters and Using blocks! :-)






    share|improve this answer





















    • i have already made coloums in datagridview so when i search all this data by textbox it should be in specific cell or coloum of datagridview.
      – pankaj babbar
      Nov 23 at 4:35















    up vote
    1
    down vote













    I used a TextBox located outside the DataGridView to enter the item code to search for. I added a Button to do the search and retrieve the data to a DataReader.



    The DataReader then loads the DataTable which is declared as a form level (class level variable). We want to use the same DataTable every time we search so the items will be added to the grid. With the Load method if the DataTable already contains rows, the incoming data from the data source is merged with the existing rows. The DataTable is then bound to the DataGridView. Each time the user enters an item code in the TextBox and clicks the Search Button a new row will be added to the grid.



    To make nicer looking Column headers use as alias in your Select statement. The As clause following the databse column name is the alias and will show up in the DataGridView as a header. If you have a space in your alias it must be enclosed in single quotes.



    Private dt As DataTable

    Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    dt = New DataTable()
    End Sub

    Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
    Using cn As New SqlConnection("server= PANKAJSQLEXPRESS; 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", txtItemCode.Text)
    cn.Open()
    Dim dr As SqlDataReader = cmd2.ExecuteReader()
    dt.Load(dr)
    DataGridView1.DataSource = dt
    End Using
    End Using
    End Sub


    P.S. Very glad to see the use of parameters and Using blocks! :-)






    share|improve this answer





















    • i have already made coloums in datagridview so when i search all this data by textbox it should be in specific cell or coloum of datagridview.
      – pankaj babbar
      Nov 23 at 4:35













    up vote
    1
    down vote










    up vote
    1
    down vote









    I used a TextBox located outside the DataGridView to enter the item code to search for. I added a Button to do the search and retrieve the data to a DataReader.



    The DataReader then loads the DataTable which is declared as a form level (class level variable). We want to use the same DataTable every time we search so the items will be added to the grid. With the Load method if the DataTable already contains rows, the incoming data from the data source is merged with the existing rows. The DataTable is then bound to the DataGridView. Each time the user enters an item code in the TextBox and clicks the Search Button a new row will be added to the grid.



    To make nicer looking Column headers use as alias in your Select statement. The As clause following the databse column name is the alias and will show up in the DataGridView as a header. If you have a space in your alias it must be enclosed in single quotes.



    Private dt As DataTable

    Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    dt = New DataTable()
    End Sub

    Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
    Using cn As New SqlConnection("server= PANKAJSQLEXPRESS; 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", txtItemCode.Text)
    cn.Open()
    Dim dr As SqlDataReader = cmd2.ExecuteReader()
    dt.Load(dr)
    DataGridView1.DataSource = dt
    End Using
    End Using
    End Sub


    P.S. Very glad to see the use of parameters and Using blocks! :-)






    share|improve this answer












    I used a TextBox located outside the DataGridView to enter the item code to search for. I added a Button to do the search and retrieve the data to a DataReader.



    The DataReader then loads the DataTable which is declared as a form level (class level variable). We want to use the same DataTable every time we search so the items will be added to the grid. With the Load method if the DataTable already contains rows, the incoming data from the data source is merged with the existing rows. The DataTable is then bound to the DataGridView. Each time the user enters an item code in the TextBox and clicks the Search Button a new row will be added to the grid.



    To make nicer looking Column headers use as alias in your Select statement. The As clause following the databse column name is the alias and will show up in the DataGridView as a header. If you have a space in your alias it must be enclosed in single quotes.



    Private dt As DataTable

    Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    dt = New DataTable()
    End Sub

    Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
    Using cn As New SqlConnection("server= PANKAJSQLEXPRESS; 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", txtItemCode.Text)
    cn.Open()
    Dim dr As SqlDataReader = cmd2.ExecuteReader()
    dt.Load(dr)
    DataGridView1.DataSource = dt
    End Using
    End Using
    End Sub


    P.S. Very glad to see the use of parameters and Using blocks! :-)







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 23 at 0:18









    Mary

    2,7512718




    2,7512718












    • i have already made coloums in datagridview so when i search all this data by textbox it should be in specific cell or coloum of datagridview.
      – pankaj babbar
      Nov 23 at 4:35


















    • i have already made coloums in datagridview so when i search all this data by textbox it should be in specific cell or coloum of datagridview.
      – pankaj babbar
      Nov 23 at 4:35
















    i have already made coloums in datagridview so when i search all this data by textbox it should be in specific cell or coloum of datagridview.
    – pankaj babbar
    Nov 23 at 4:35




    i have already made coloums in datagridview so when i search all this data by textbox it should be in specific cell or coloum of datagridview.
    – pankaj babbar
    Nov 23 at 4:35












    up vote
    0
    down vote













    How about this?



    '"C:your_pathNorthwind.mdb"
    Imports System
    Imports System.Data
    Imports System.Data.OleDb
    Imports System.Windows.Forms

    Public Class Form1
    Inherits System.Windows.Forms.Form

    Private bindingSource1 As New BindingSource()
    Private dataAdapter As New OleDbDataAdapter()

    <STAThreadAttribute()> _
    Public Shared Sub Main()
    Application.Run(New Form1())
    End Sub

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Dim connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:your_pathNorthwind.mdb"
    Dim selectCommand As String
    Dim connection As New OleDbConnection(connectionString)

    selectCommand = "Select * From MyExcelTable ORDER BY ID"
    Me.dataAdapter = New OleDbDataAdapter(selectCommand, connection)

    With DataGridView1
    .AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells
    .AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.ColumnHeader
    .AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.ColumnHeader
    End With

    Dim commandBuilder As New OleDbCommandBuilder(Me.dataAdapter)
    Dim table As New DataTable()

    table.Locale = System.Globalization.CultureInfo.InvariantCulture

    Me.dataAdapter.Fill(table)
    Me.bindingSource1.DataSource = table

    Dim data As New DataSet()

    data.Locale = System.Globalization.CultureInfo.InvariantCulture

    DataGridView1.DataSource = Me.bindingSource1
    Me.DataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.Aqua
    Me.DataGridView1.AutoResizeColumns( _
    DataGridViewAutoSizeColumnsMode.AllCells)

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click, btnUpdate.Click
    Dim table As New DataTable()

    Me.bindingSource1 = Me.DataGridView1.DataSource
    table = Me.bindingSource1.DataSource

    Me.dataAdapter.Update(table)
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click, btnClose.Click
    Me.Close()
    End Sub

    Private Sub TextBox1_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox1.TextChanged, TextBox1.Click

    Dim connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:your_pathNorthwind.mdb"
    Dim selectCommand As String
    Dim connection As New OleDbConnection(connectionString)

    'selectCommand = "Select * From MyExcelTable where Fname = '" & TextBox1.Text & "'"
    '"SELECT * FROM Customers WHERE Address LIKE '" & strAddressSearch & "%'"
    'or ending with:
    '"SELECT * FROM Customers WHERE Address LIKE '%" & strAddressSearch & "'"

    selectCommand = "Select * From MyExcelTable where Fname Like '%" & TextBox1.Text & "%'"
    Me.dataAdapter = New OleDbDataAdapter(selectCommand, connection)

    With DataGridView1
    .AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells
    .AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.ColumnHeader
    .AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.ColumnHeader
    End With

    Dim commandBuilder As New OleDbCommandBuilder(Me.dataAdapter)
    Dim table As New DataTable()

    table.Locale = System.Globalization.CultureInfo.InvariantCulture

    Me.dataAdapter.Fill(table)
    Me.bindingSource1.DataSource = table

    Dim data As New DataSet()

    data.Locale = System.Globalization.CultureInfo.InvariantCulture

    DataGridView1.DataSource = Me.bindingSource1
    Me.DataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.Aqua
    Me.DataGridView1.AutoResizeColumns( _
    DataGridViewAutoSizeColumnsMode.AllCells)

    End Sub
    End Class


    enter image description here






    share|improve this answer

























      up vote
      0
      down vote













      How about this?



      '"C:your_pathNorthwind.mdb"
      Imports System
      Imports System.Data
      Imports System.Data.OleDb
      Imports System.Windows.Forms

      Public Class Form1
      Inherits System.Windows.Forms.Form

      Private bindingSource1 As New BindingSource()
      Private dataAdapter As New OleDbDataAdapter()

      <STAThreadAttribute()> _
      Public Shared Sub Main()
      Application.Run(New Form1())
      End Sub

      Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
      Dim connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:your_pathNorthwind.mdb"
      Dim selectCommand As String
      Dim connection As New OleDbConnection(connectionString)

      selectCommand = "Select * From MyExcelTable ORDER BY ID"
      Me.dataAdapter = New OleDbDataAdapter(selectCommand, connection)

      With DataGridView1
      .AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells
      .AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.ColumnHeader
      .AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.ColumnHeader
      End With

      Dim commandBuilder As New OleDbCommandBuilder(Me.dataAdapter)
      Dim table As New DataTable()

      table.Locale = System.Globalization.CultureInfo.InvariantCulture

      Me.dataAdapter.Fill(table)
      Me.bindingSource1.DataSource = table

      Dim data As New DataSet()

      data.Locale = System.Globalization.CultureInfo.InvariantCulture

      DataGridView1.DataSource = Me.bindingSource1
      Me.DataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.Aqua
      Me.DataGridView1.AutoResizeColumns( _
      DataGridViewAutoSizeColumnsMode.AllCells)

      End Sub

      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click, btnUpdate.Click
      Dim table As New DataTable()

      Me.bindingSource1 = Me.DataGridView1.DataSource
      table = Me.bindingSource1.DataSource

      Me.dataAdapter.Update(table)
      End Sub

      Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click, btnClose.Click
      Me.Close()
      End Sub

      Private Sub TextBox1_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox1.TextChanged, TextBox1.Click

      Dim connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:your_pathNorthwind.mdb"
      Dim selectCommand As String
      Dim connection As New OleDbConnection(connectionString)

      'selectCommand = "Select * From MyExcelTable where Fname = '" & TextBox1.Text & "'"
      '"SELECT * FROM Customers WHERE Address LIKE '" & strAddressSearch & "%'"
      'or ending with:
      '"SELECT * FROM Customers WHERE Address LIKE '%" & strAddressSearch & "'"

      selectCommand = "Select * From MyExcelTable where Fname Like '%" & TextBox1.Text & "%'"
      Me.dataAdapter = New OleDbDataAdapter(selectCommand, connection)

      With DataGridView1
      .AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells
      .AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.ColumnHeader
      .AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.ColumnHeader
      End With

      Dim commandBuilder As New OleDbCommandBuilder(Me.dataAdapter)
      Dim table As New DataTable()

      table.Locale = System.Globalization.CultureInfo.InvariantCulture

      Me.dataAdapter.Fill(table)
      Me.bindingSource1.DataSource = table

      Dim data As New DataSet()

      data.Locale = System.Globalization.CultureInfo.InvariantCulture

      DataGridView1.DataSource = Me.bindingSource1
      Me.DataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.Aqua
      Me.DataGridView1.AutoResizeColumns( _
      DataGridViewAutoSizeColumnsMode.AllCells)

      End Sub
      End Class


      enter image description here






      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        How about this?



        '"C:your_pathNorthwind.mdb"
        Imports System
        Imports System.Data
        Imports System.Data.OleDb
        Imports System.Windows.Forms

        Public Class Form1
        Inherits System.Windows.Forms.Form

        Private bindingSource1 As New BindingSource()
        Private dataAdapter As New OleDbDataAdapter()

        <STAThreadAttribute()> _
        Public Shared Sub Main()
        Application.Run(New Form1())
        End Sub

        Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:your_pathNorthwind.mdb"
        Dim selectCommand As String
        Dim connection As New OleDbConnection(connectionString)

        selectCommand = "Select * From MyExcelTable ORDER BY ID"
        Me.dataAdapter = New OleDbDataAdapter(selectCommand, connection)

        With DataGridView1
        .AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells
        .AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.ColumnHeader
        .AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.ColumnHeader
        End With

        Dim commandBuilder As New OleDbCommandBuilder(Me.dataAdapter)
        Dim table As New DataTable()

        table.Locale = System.Globalization.CultureInfo.InvariantCulture

        Me.dataAdapter.Fill(table)
        Me.bindingSource1.DataSource = table

        Dim data As New DataSet()

        data.Locale = System.Globalization.CultureInfo.InvariantCulture

        DataGridView1.DataSource = Me.bindingSource1
        Me.DataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.Aqua
        Me.DataGridView1.AutoResizeColumns( _
        DataGridViewAutoSizeColumnsMode.AllCells)

        End Sub

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click, btnUpdate.Click
        Dim table As New DataTable()

        Me.bindingSource1 = Me.DataGridView1.DataSource
        table = Me.bindingSource1.DataSource

        Me.dataAdapter.Update(table)
        End Sub

        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click, btnClose.Click
        Me.Close()
        End Sub

        Private Sub TextBox1_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox1.TextChanged, TextBox1.Click

        Dim connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:your_pathNorthwind.mdb"
        Dim selectCommand As String
        Dim connection As New OleDbConnection(connectionString)

        'selectCommand = "Select * From MyExcelTable where Fname = '" & TextBox1.Text & "'"
        '"SELECT * FROM Customers WHERE Address LIKE '" & strAddressSearch & "%'"
        'or ending with:
        '"SELECT * FROM Customers WHERE Address LIKE '%" & strAddressSearch & "'"

        selectCommand = "Select * From MyExcelTable where Fname Like '%" & TextBox1.Text & "%'"
        Me.dataAdapter = New OleDbDataAdapter(selectCommand, connection)

        With DataGridView1
        .AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells
        .AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.ColumnHeader
        .AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.ColumnHeader
        End With

        Dim commandBuilder As New OleDbCommandBuilder(Me.dataAdapter)
        Dim table As New DataTable()

        table.Locale = System.Globalization.CultureInfo.InvariantCulture

        Me.dataAdapter.Fill(table)
        Me.bindingSource1.DataSource = table

        Dim data As New DataSet()

        data.Locale = System.Globalization.CultureInfo.InvariantCulture

        DataGridView1.DataSource = Me.bindingSource1
        Me.DataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.Aqua
        Me.DataGridView1.AutoResizeColumns( _
        DataGridViewAutoSizeColumnsMode.AllCells)

        End Sub
        End Class


        enter image description here






        share|improve this answer












        How about this?



        '"C:your_pathNorthwind.mdb"
        Imports System
        Imports System.Data
        Imports System.Data.OleDb
        Imports System.Windows.Forms

        Public Class Form1
        Inherits System.Windows.Forms.Form

        Private bindingSource1 As New BindingSource()
        Private dataAdapter As New OleDbDataAdapter()

        <STAThreadAttribute()> _
        Public Shared Sub Main()
        Application.Run(New Form1())
        End Sub

        Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:your_pathNorthwind.mdb"
        Dim selectCommand As String
        Dim connection As New OleDbConnection(connectionString)

        selectCommand = "Select * From MyExcelTable ORDER BY ID"
        Me.dataAdapter = New OleDbDataAdapter(selectCommand, connection)

        With DataGridView1
        .AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells
        .AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.ColumnHeader
        .AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.ColumnHeader
        End With

        Dim commandBuilder As New OleDbCommandBuilder(Me.dataAdapter)
        Dim table As New DataTable()

        table.Locale = System.Globalization.CultureInfo.InvariantCulture

        Me.dataAdapter.Fill(table)
        Me.bindingSource1.DataSource = table

        Dim data As New DataSet()

        data.Locale = System.Globalization.CultureInfo.InvariantCulture

        DataGridView1.DataSource = Me.bindingSource1
        Me.DataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.Aqua
        Me.DataGridView1.AutoResizeColumns( _
        DataGridViewAutoSizeColumnsMode.AllCells)

        End Sub

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click, btnUpdate.Click
        Dim table As New DataTable()

        Me.bindingSource1 = Me.DataGridView1.DataSource
        table = Me.bindingSource1.DataSource

        Me.dataAdapter.Update(table)
        End Sub

        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click, btnClose.Click
        Me.Close()
        End Sub

        Private Sub TextBox1_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox1.TextChanged, TextBox1.Click

        Dim connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:your_pathNorthwind.mdb"
        Dim selectCommand As String
        Dim connection As New OleDbConnection(connectionString)

        'selectCommand = "Select * From MyExcelTable where Fname = '" & TextBox1.Text & "'"
        '"SELECT * FROM Customers WHERE Address LIKE '" & strAddressSearch & "%'"
        'or ending with:
        '"SELECT * FROM Customers WHERE Address LIKE '%" & strAddressSearch & "'"

        selectCommand = "Select * From MyExcelTable where Fname Like '%" & TextBox1.Text & "%'"
        Me.dataAdapter = New OleDbDataAdapter(selectCommand, connection)

        With DataGridView1
        .AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells
        .AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.ColumnHeader
        .AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.ColumnHeader
        End With

        Dim commandBuilder As New OleDbCommandBuilder(Me.dataAdapter)
        Dim table As New DataTable()

        table.Locale = System.Globalization.CultureInfo.InvariantCulture

        Me.dataAdapter.Fill(table)
        Me.bindingSource1.DataSource = table

        Dim data As New DataSet()

        data.Locale = System.Globalization.CultureInfo.InvariantCulture

        DataGridView1.DataSource = Me.bindingSource1
        Me.DataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.Aqua
        Me.DataGridView1.AutoResizeColumns( _
        DataGridViewAutoSizeColumnsMode.AllCells)

        End Sub
        End Class


        enter image description here







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 24 at 16:33









        ryguy72

        3,8011619




        3,8011619






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53435014%2fhow-to-search-data-from-textbox-and-fill-specific-coloumns-of-datagridview-from%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            What visual should I use to simply compare current year value vs last year in Power BI desktop

            Alexandru Averescu

            Trompette piccolo