Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Searching within datagridview

Searching within datagridview

Scheduled Pinned Locked Moved C#
helpdatabasealgorithms
8 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Martin31088
    wrote on last edited by
    #1

    Hey everyone, I've got a datagridview linked to an access database with a seach function. It's working well, but with one problem. It only displays the row(s) when you search for the complete User Name. It would be a big time saver if the search would locate any row(s) which contains the txtSearchID, so you would not need to type out the whole user name every time. At the moment I am using: Command.CommandText = "SELECT * FROM Users WHERE [User ID] = '" + txtSearchID.Text + "'"; OleDbDataAdapter Adapter = new OleDbDataAdapter(Command); Adapter.Fill(ds); I'm sure theres a simple answer, just can't quite figure it out. Any help greatly appreciated Martin

    C M 2 Replies Last reply
    0
    • M Martin31088

      Hey everyone, I've got a datagridview linked to an access database with a seach function. It's working well, but with one problem. It only displays the row(s) when you search for the complete User Name. It would be a big time saver if the search would locate any row(s) which contains the txtSearchID, so you would not need to type out the whole user name every time. At the moment I am using: Command.CommandText = "SELECT * FROM Users WHERE [User ID] = '" + txtSearchID.Text + "'"; OleDbDataAdapter Adapter = new OleDbDataAdapter(Command); Adapter.Fill(ds); I'm sure theres a simple answer, just can't quite figure it out. Any help greatly appreciated Martin

      C Offline
      C Offline
      celso_cabaleiro
      wrote on last edited by
      #2

      In access try Command.CommandText = "SELECT * FROM Users WHERE [User ID] = '*" + txtSearchID.Text + "*'";

      1 Reply Last reply
      0
      • M Martin31088

        Hey everyone, I've got a datagridview linked to an access database with a seach function. It's working well, but with one problem. It only displays the row(s) when you search for the complete User Name. It would be a big time saver if the search would locate any row(s) which contains the txtSearchID, so you would not need to type out the whole user name every time. At the moment I am using: Command.CommandText = "SELECT * FROM Users WHERE [User ID] = '" + txtSearchID.Text + "'"; OleDbDataAdapter Adapter = new OleDbDataAdapter(Command); Adapter.Fill(ds); I'm sure theres a simple answer, just can't quite figure it out. Any help greatly appreciated Martin

        M Offline
        M Offline
        musefan
        wrote on last edited by
        #3

        you will want to use 'LIKE' instead of '=' example...

        Command.CommandText = "SELECT * FROM Users WHERE [User ID] LIKE '%" + txtSearchID.Text + "%'";

        of course what you really want to do is...

        Command.CommandText = "SELECT * FROM Users WHERE [User ID] LIKE '%@1'";
        Command.Parameters.Add("@1", OleDbType.Char).Value = txtSearchID.Text;

        Life goes very fast. Tomorrow, today is already yesterday.

        C 1 Reply Last reply
        0
        • M musefan

          you will want to use 'LIKE' instead of '=' example...

          Command.CommandText = "SELECT * FROM Users WHERE [User ID] LIKE '%" + txtSearchID.Text + "%'";

          of course what you really want to do is...

          Command.CommandText = "SELECT * FROM Users WHERE [User ID] LIKE '%@1'";
          Command.Parameters.Add("@1", OleDbType.Char).Value = txtSearchID.Text;

          Life goes very fast. Tomorrow, today is already yesterday.

          C Offline
          C Offline
          celso_cabaleiro
          wrote on last edited by
          #4

          I think he is using an access database, not Sql Server. Better than %, he must use *

          Command.CommandText = "SELECT * FROM Users WHERE [User ID] LIKE '*" + txtSearchID.Text + "*'";

          but use parameters it´s a good choice.

          M 1 Reply Last reply
          0
          • C celso_cabaleiro

            I think he is using an access database, not Sql Server. Better than %, he must use *

            Command.CommandText = "SELECT * FROM Users WHERE [User ID] LIKE '*" + txtSearchID.Text + "*'";

            but use parameters it´s a good choice.

            M Offline
            M Offline
            musefan
            wrote on last edited by
            #5

            I use % with access and it works fine. What does * do that is better?

            Life goes very fast. Tomorrow, today is already yesterday.

            C 1 Reply Last reply
            0
            • M musefan

              I use % with access and it works fine. What does * do that is better?

              Life goes very fast. Tomorrow, today is already yesterday.

              C Offline
              C Offline
              celso_cabaleiro
              wrote on last edited by
              #6

              Nothing into access it's better, but the "Jet sql" suggest using like with * as wild pattern. If you use %, the OleDb Provider make the translation for you. If you try

              select * from table where field1 like '%something%'

              in the query panel of access you get an empty result.

              M 1 Reply Last reply
              0
              • C celso_cabaleiro

                Nothing into access it's better, but the "Jet sql" suggest using like with * as wild pattern. If you use %, the OleDb Provider make the translation for you. If you try

                select * from table where field1 like '%something%'

                in the query panel of access you get an empty result.

                M Offline
                M Offline
                musefan
                wrote on last edited by
                #7

                OK, well thanks for the info, is good to know :) But either way in the context it is being used then it does not matter if use * or %

                Life goes very fast. Tomorrow, today is already yesterday.

                C 1 Reply Last reply
                0
                • M musefan

                  OK, well thanks for the info, is good to know :) But either way in the context it is being used then it does not matter if use * or %

                  Life goes very fast. Tomorrow, today is already yesterday.

                  C Offline
                  C Offline
                  celso_cabaleiro
                  wrote on last edited by
                  #8

                  Yes, it´s true, the performing is the same. If you want to know more about old things like the tenebrous jet sql, you have this and about the wildcard characters here. :~

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups