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
  1. Home
  2. Web Development
  3. ASP.NET
  4. how to highlight the search result in datagridview using c#.

how to highlight the search result in datagridview using c#.

Scheduled Pinned Locked Moved ASP.NET
csharpdatabasehelptutorial
3 Posts 2 Posters 1 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
    Member 12016106
    wrote on last edited by
    #1

    foreach (GridViewRow row in GridView_pm.Rows) { if (row.Cells["MachineTypeNo"].Value.ToString().Equals(txt_search.Text)) { GridView_pm.Rows[row.Index].DefaultCellStyle.BackColor =("yellow"); } else { GridView_pm.Rows[row.Index].Visible = false; } I don't really sure about it..can you pls help me.

    T 1 Reply Last reply
    0
    • M Member 12016106

      foreach (GridViewRow row in GridView_pm.Rows) { if (row.Cells["MachineTypeNo"].Value.ToString().Equals(txt_search.Text)) { GridView_pm.Rows[row.Index].DefaultCellStyle.BackColor =("yellow"); } else { GridView_pm.Rows[row.Index].Visible = false; } I don't really sure about it..can you pls help me.

      T Offline
      T Offline
      taleebanwar
      wrote on last edited by
      #2

      You should write the code in row data bound event of the gridview. The following code will change the value of the corresponding row to yellow color.

      protected void GridView_pm_RowDataBound(object sender, GridViewRowEventArgs e)
      {
      if (!String.IsNullOrWhiteSpace(txt_search.Text))
      {
      if (e.Row.RowType == DataControlRowType.DataRow)
      {
      var rowView = e.Row.DataItem as DataRowView;
      if (String.Compare(rowView["MachineTypeNo"].ToString(), txt_search.Text,true) == 0)
      {
      e.Row.BackColor = System.Drawing.Color.Yellow;
      }
      else
      {
      e.Row.BackColor = System.Drawing.Color.White;
      }
      }
      }
      }

      Thanks & Regards Taleeb (trystwithdotnet.blogspot.com)

      M 1 Reply Last reply
      0
      • T taleebanwar

        You should write the code in row data bound event of the gridview. The following code will change the value of the corresponding row to yellow color.

        protected void GridView_pm_RowDataBound(object sender, GridViewRowEventArgs e)
        {
        if (!String.IsNullOrWhiteSpace(txt_search.Text))
        {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
        var rowView = e.Row.DataItem as DataRowView;
        if (String.Compare(rowView["MachineTypeNo"].ToString(), txt_search.Text,true) == 0)
        {
        e.Row.BackColor = System.Drawing.Color.Yellow;
        }
        else
        {
        e.Row.BackColor = System.Drawing.Color.White;
        }
        }
        }
        }

        Thanks & Regards Taleeb (trystwithdotnet.blogspot.com)

        M Offline
        M Offline
        Member 12016106
        wrote on last edited by
        #3

        Thank you for your sharing..

        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