I want to place a textbox on my Windows Application form. Â Then, I want the user to be able to put a value in the textbox and hit a button to then search my datagrid, based on a row specific specified in a combo. Â It would be nice to have the row highlight. Â
Example:
If my datagrid is showing columns:
ID   Number   Description   Serial
1 Â Â Â Â 983 Â Â Â Â Â Â test1 Â Â Â Â Â 093930
2 Â Â Â Â 872 Â Â Â Â Â Â test2 Â Â Â Â Â 930200
3 Â Â Â Â 902 Â Â Â Â Â Â test3 Â Â Â Â Â 930233
The user chooses "Number" from the Combobox, and types in "872" in the textbox, then row "2" highlights
You can bind the textbox to the same dataset the datagrid uses with conditions: when user chooses "Number", bind the textbox to "Number" column in the dataset, and so on.
Try something like this, First is the changed event for the combobox (cboWhere) , then the focus is moved to the textbox (txtsearch), then the focus moves to a search button which runs the main code (not great but it works), it highlights the search row in yellow.
You need to get the number of rows in your dataset ie.
numrows = dataset.tables(0).rows.count -1
Private Sub cboWhere_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboWhere.SelectedIndexChanged
    fieldsearch = cboWhere.SelectedItem.ToString
    txtsearch.Focus()
  End Sub
Private Sub txtsearch_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtsearch.TextChanged
    wts = txtsearch.Text
    btnsearch.focus
  End Sub
Try
      If wts = "" Then
        MsgBox("You Havent Entered Any Search Criteria", MsgBoxStyle.Critical, "Warning!")
        Exit Sub
      End If
      If LCase(fieldsearch) = "number" Then
        x = 0
        For x = 1 To datarowcount
            If (DataGrid1.Item(x, 1).ToString = wts Then
            DataGrid1.Select(x)
            DataGrid1.SelectionBackColor = Color.Khaki
            DataGrid1.SelectionForeColor = Color.Black
          End If
        Next
      End If
      If LCase(fieldsearch) = "description" Then
       x = 0
        For x = 1 To datarowcount
           If (DataGrid1.Item(x, 2).ToString = wts Then
            DataGrid1.Select(x)
            DataGrid1.SelectionBackColor = Color.Khaki
            DataGrid1.SelectionForeColor = Color.Black
          End If
        Next
      End If
      If LCase(fieldsearch) = "serial" Then
        x = 0
        For x = 1 To datarowcount
           If (DataGrid1.Item(x, 3).ToString = wts Then
            DataGrid1.Select(x)
            DataGrid1.SelectionBackColor = Color.Khaki
            DataGrid1.SelectionForeColor = Color.Black
          End If
        Next
      End If
Its a bit meesy but it does work for me, let me know if you want any more help
davidlars99
shittash, where does this variable come from
>Â Â datarowcount
Shittash
The dataset that your datagrid is binding to, count the number of rows in the dataset
A lot of your examples are for ASP.NET. Â I am using a Windows Application, I appolagize, I probably should have stated that. Â I keep forgetting that ASP.NET questions are posted in the VB.NET section. Â I realize that ASP.NET is VB for the web, but I am not very keen on ASP.NET.
davidlars99
Shittash,s example is not for ASP.NET!!!
chrisryhal
ASKER
Gotcha,
Shittash, I am having a difficult understanding how to declare portions of your code. Â i have included a copy of my current code.
==================CODE============
Option Strict On
Imports System.Data.SqlClient
Public Class frmMainForm
  Inherits System.Windows.Forms.Form
  Protected ToolingData As New DataSet()
  Protected Const SQL_CONNECTION_STRING As String = _
    "Server=k-tsvr3;" & _
    "DataBase=Triumph;" & _
    "Integrated Security=SSPI"
  Protected didPreviouslyConnect As Boolean = False
  Protected connectionString As String = SQL_CONNECTION_STRING
  Protected Const TOOLING_TABLE_NAME As String = "TL_Tooling"
#Region "Â Windows Form Designer generated code "
  Public Sub New()
    MyBase.New()
    'This call is required by the Windows Form Designer.
    InitializeComponent()
    'Add any initialization after the InitializeComponent() call
  End Sub
  'Form overrides dispose to clean up the component list.
  Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
    If disposing Then
      If Not (components Is Nothing) Then
        components.Dispose()
      End If
    End If
    MyBase.Dispose(disposing)
  End Sub
  'Required by the Windows Form Designer
  Private components As System.ComponentModel.IContainer
  'NOTE: The following procedure is required by the Windows Form Designer
  'It can be modified using the Windows Form Designer. Â
  'Do not modify it using the code editor.
  Friend WithEvents MainMenu1 As System.Windows.Forms.MainMenu
  Friend WithEvents MenuItem1 As System.Windows.Forms.MenuItem
  Friend WithEvents MenuItem2 As System.Windows.Forms.MenuItem
  Friend WithEvents ToolTrackerDataGrid As System.Windows.Forms.DataGrid
  Friend WithEvents txtID As System.Windows.Forms.TextBox
  Friend WithEvents MenuItem3 As System.Windows.Forms.MenuItem
  Friend WithEvents MenuItem4 As System.Windows.Forms.MenuItem
  Friend WithEvents Label1 As System.Windows.Forms.Label
  Friend WithEvents cmbStatus As System.Windows.Forms.ComboBox
  Friend WithEvents Label2 As System.Windows.Forms.Label
  Friend WithEvents cmbSortBy As System.Windows.Forms.ComboBox
  Friend WithEvents lblStartAt As System.Windows.Forms.Label
  Friend WithEvents txtStartAt As System.Windows.Forms.TextBox
  Friend WithEvents txtSearch As System.Windows.Forms.Button
  <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
    Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(frmMainForm))
    Me.ToolTrackerDataGrid = New System.Windows.Forms.DataGrid()
    Me.MainMenu1 = New System.Windows.Forms.MainMenu()
    Me.MenuItem1 = New System.Windows.Forms.MenuItem()
    Me.MenuItem2 = New System.Windows.Forms.MenuItem()
    Me.MenuItem3 = New System.Windows.Forms.MenuItem()
    Me.MenuItem4 = New System.Windows.Forms.MenuItem()
    Me.txtID = New System.Windows.Forms.TextBox()
    Me.Label1 = New System.Windows.Forms.Label()
    Me.cmbStatus = New System.Windows.Forms.ComboBox()
    Me.Label2 = New System.Windows.Forms.Label()
    Me.cmbSortBy = New System.Windows.Forms.ComboBox()
    Me.lblStartAt = New System.Windows.Forms.Label()
    Me.txtStartAt = New System.Windows.Forms.TextBox()
    Me.txtSearch = New System.Windows.Forms.Button()
    CType(Me.ToolTrackerDataGrid, System.ComponentModel.ISupportInitialize).BeginInit()
    Me.SuspendLayout()
    '
    'ToolTrackerDataGrid
    '
    Me.ToolTrackerDataGrid.DataMember = ""
    Me.ToolTrackerDataGrid.HeaderForeColor = System.Drawing.SystemColors.ControlText
    Me.ToolTrackerDataGrid.Location = New System.Drawing.Point(8, 72)
    Me.ToolTrackerDataGrid.Name = "ToolTrackerDataGrid"
    Me.ToolTrackerDataGrid.ReadOnly = True
    Me.ToolTrackerDataGrid.Size = New System.Drawing.Size(728, 384)
    Me.ToolTrackerDataGrid.TabIndex = 0
    '
    'MainMenu1
    '
    Me.MainMenu1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem1, Me.MenuItem3})
    '
    'MenuItem1
    '
    Me.MenuItem1.Index = 0
    Me.MenuItem1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem2})
    Me.MenuItem1.Text = "File"
    '
    'MenuItem2
    '
    Me.MenuItem2.Index = 0
    Me.MenuItem2.Text = "Exit"
    '
    'MenuItem3
    '
    Me.MenuItem3.Index = 1
    Me.MenuItem3.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem4})
    Me.MenuItem3.Text = "Tooling"
    '
    'MenuItem4
    '
    Me.MenuItem4.Index = 0
    Me.MenuItem4.Text = "Add"
    '
    'txtID
    '
    Me.txtID.Enabled = False
    Me.txtID.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
    Me.txtID.Location = New System.Drawing.Point(8, 104)
    Me.txtID.Name = "txtID"
    Me.txtID.Size = New System.Drawing.Size(48, 20)
    Me.txtID.TabIndex = 7
    Me.txtID.Text = ""
    Me.txtID.Visible = False
    '
    'Label1
    '
    Me.Label1.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
    Me.Label1.Location = New System.Drawing.Point(16, 40)
    Me.Label1.Name = "Label1"
    Me.Label1.Size = New System.Drawing.Size(48, 16)
    Me.Label1.TabIndex = 9
    Me.Label1.Text = "Status:"
    '
    'cmbStatus
    '
    Me.cmbStatus.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
    Me.cmbStatus.ItemHeight = 13
    Me.cmbStatus.Items.AddRange(New Object() {"ACTIVE", "INHOUSE", "OUT - VENDER PRODUCTION", "OUT- CUSTOMER", "REPAIR - VENDOR", "REPAIR - INHOUSE", "ENGINEERING", "RED TAGGED - NEW", "RED TAGGED - ENG", "RED TAGGED - APR", "INACTIVE"})
    Me.cmbStatus.Location = New System.Drawing.Point(64, 40)
    Me.cmbStatus.Name = "cmbStatus"
    Me.cmbStatus.Size = New System.Drawing.Size(208, 21)
    Me.cmbStatus.TabIndex = 8
    '
    'Label2
    '
    Me.Label2.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
    Me.Label2.Location = New System.Drawing.Point(8, 8)
    Me.Label2.Name = "Label2"
    Me.Label2.Size = New System.Drawing.Size(48, 16)
    Me.Label2.TabIndex = 10
    Me.Label2.Text = "Sort By:"
    '
    'cmbSortBy
    '
    Me.cmbSortBy.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
    Me.cmbSortBy.Items.AddRange(New Object() {"ToolID", "Tool Number", "Description", "Serial Number", "Customer", "Tool Owner", "Calibration Due", "Calibration Never Done"})
    Me.cmbSortBy.Location = New System.Drawing.Point(64, 8)
    Me.cmbSortBy.Name = "cmbSortBy"
    Me.cmbSortBy.Size = New System.Drawing.Size(208, 21)
    Me.cmbSortBy.TabIndex = 11
    Me.cmbSortBy.Text = "ToolID"
    '
    'lblStartAt
    '
    Me.lblStartAt.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
    Me.lblStartAt.Location = New System.Drawing.Point(432, 8)
    Me.lblStartAt.Name = "lblStartAt"
    Me.lblStartAt.Size = New System.Drawing.Size(48, 16)
    Me.lblStartAt.TabIndex = 12
    Me.lblStartAt.Text = "Start At:"
    '
    'txtStartAt
    '
    Me.txtStartAt.Location = New System.Drawing.Point(488, 8)
    Me.txtStartAt.Name = "txtStartAt"
    Me.txtStartAt.Size = New System.Drawing.Size(160, 20)
    Me.txtStartAt.TabIndex = 13
    Me.txtStartAt.Text = ""
    '
    'txtSearch
    '
    Me.txtSearch.Location = New System.Drawing.Point(656, 8)
    Me.txtSearch.Name = "txtSearch"
    Me.txtSearch.Size = New System.Drawing.Size(56, 23)
    Me.txtSearch.TabIndex = 14
    Me.txtSearch.Text = "Search"
    '
    'frmMainForm
    '
    Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
    Me.ClientSize = New System.Drawing.Size(744, 465)
    Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.txtSearch, Me.txtStartAt, Me.lblStartAt, Me.cmbSortBy, Me.Label2, Me.Label1, Me.cmbStatus, Me.txtID, Me.ToolTrackerDataGrid})
    Me.Icon = CType(resources.GetObject("$this.Icon"), System.Drawing.Icon)
    Me.Menu = Me.MainMenu1
    Me.Name = "frmMainForm"
    Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
    Me.Text = "Tool Tracker .NET"
    CType(Me.ToolTrackerDataGrid, System.ComponentModel.ISupportInitialize).EndInit()
    Me.ResumeLayout(False)
  End Sub
#End Region
  Dim dv As New DataView()
  Private Sub frmMainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim frmStatusMessage As New frmStatus()
    If Not didPreviouslyConnect Then
      frmStatusMessage.Show("Connecting to SQL Server")
    End If
    Dim isConnecting As Boolean = True
    While isConnecting
      Try
        Dim ToolTrackerConnection As New SqlConnection(connectionString)
        Dim ProductAdapter As New SqlDataAdapter( _
          "select * from TL_Tooling", _
          ToolTrackerConnection)
        ToolingData.Clear()
        ProductAdapter.Fill(ToolingData, TOOLING_TABLE_NAME)
        ToolTrackerDataGrid.DataSource = ToolingData.Tables(TOOLING_TABLE_NAME).DefaultView
        isConnecting = False
      Catch exc As Exception
        If connectionString = SQL_CONNECTION_STRING Then
          frmStatusMessage.Show("Connection Dead, Killing Tool Tracker")
          End
        End If
      End Try
    End While
    frmStatusMessage.Close()
  End Sub
  Private Sub ToolTrackerDataGrid_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles ToolTrackerDataGrid.DoubleClick
    Dim pt As System.Drawing.Point = ToolTrackerDataGrid.PointToClient(Cursor.Position)
    Dim hti As DataGrid.HitTestInfo = ToolTrackerDataGrid.HitTest(pt)
    If hti.Type = DataGrid.HitTestType.RowHeader Then
      txtID.Text = CType(ToolingData.Tables(0).DefaultView.Item(hti.Row).Item("ToolID"), String)
    End If
    frmMainForm_ID = txtID.Text 'Module Call
    Dim frmToolTrackerEditTool As New frmToolTrackerEditTool()
    frmToolTrackerEditTool.ShowDialog()
  End Sub
  Private Sub MenuItem4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem4.Click
    Dim frmToolTrackerAddNew As New frmToolTrackerAddNew()
    frmToolTrackerAddNew.ShowDialog(Me)
    frmToolTrackerAddNew.Dispose()
  End Sub
  Private Sub cmbStatus_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbStatus.SelectedIndexChanged
    If cmbStatus.Text = "ACTIVE" Then
      ToolingData.Tables(TOOLING_TABLE_NAME).DefaultView.RowFilter = ""
    Else
      ToolingData.Tables(TOOLING_TABLE_NAME).DefaultView.RowFilter = "StatusCode = '" & cmbStatus.Text & "'"
    End If
  End Sub
  Private Sub txtSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtSearch.Click
  End Sub
  Private Sub cmbSortBy_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbSortBy.SelectedIndexChanged
    Dim TS As New DataGridTableStyle()
    TS.MappingName = TOOLING_TABLE_NAME
    Dim Col As New DataGridTextBoxColumn()
    If cmbSortBy.Text = "ToolID" Then
      Col = New DataGridTextBoxColumn()
      Col.MappingName = "ToolID"
      Col.HeaderText = "ToolID"
      Col.Width = 50
      TS.GridColumnStyles.Add(Col)
      Col = New DataGridTextBoxColumn()
      Col.MappingName = "ToolNumber"
      Col.HeaderText = "Tool Number"
      Col.Width = 200
      TS.GridColumnStyles.Add(Col)
      Col = New DataGridTextBoxColumn()
      Col.MappingName = "Description"
      Col.HeaderText = "Description"
      Col.Width = 100
      TS.GridColumnStyles.Add(Col)
      Col = New DataGridTextBoxColumn()
      Col.MappingName = "SerialNumber"
      Col.HeaderText = "Serial Number"
      Col.Width = 100
      TS.GridColumnStyles.Add(Col)
      Col = New DataGridTextBoxColumn()
      Col.MappingName = "Location"
      Col.HeaderText = "Location"
      Col.Width = 100
      TS.GridColumnStyles.Add(Col)
      Col = New DataGridTextBoxColumn()
      Col.MappingName = "StatusCode"
      Col.HeaderText = "Current Status"
      Col.Width = 150
      TS.GridColumnStyles.Add(Col)
      ToolTrackerDataGrid.TableStyles.Clear()
      ToolTrackerDataGrid.TableStyles.Add(TS)
    End If
    If cmbSortBy.Text = "Tool Number" Then
      Col = New DataGridTextBoxColumn()
      Col.MappingName = "ToolNumber"
      Col.HeaderText = "Tool Number"
      Col.Width = 50
      TS.GridColumnStyles.Add(Col)
      Col = New DataGridTextBoxColumn()
      Col.MappingName = "ToolID"
      Col.HeaderText = "ToolID"
      Col.Width = 200
      TS.GridColumnStyles.Add(Col)
      Col = New DataGridTextBoxColumn()
      Col.MappingName = "Description"
      Col.HeaderText = "Description"
      Col.Width = 100
      TS.GridColumnStyles.Add(Col)
      Col = New DataGridTextBoxColumn()
      Col.MappingName = "SerialNumber"
      Col.HeaderText = "Serial Number"
      Col.Width = 100
      TS.GridColumnStyles.Add(Col)
      Col = New DataGridTextBoxColumn()
      Col.MappingName = "Location"
      Col.HeaderText = "Location"
      Col.Width = 100
      TS.GridColumnStyles.Add(Col)
      Col = New DataGridTextBoxColumn()
      Col.MappingName = "StatusCode"
      Col.HeaderText = "Current Status"
      Col.Width = 150
      TS.GridColumnStyles.Add(Col)
      ToolTrackerDataGrid.TableStyles.Clear()
      ToolTrackerDataGrid.TableStyles.Add(TS)
Private Sub txtSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtSearch.Click
 'You can use this if you assign the 'fieldsearch' variable to the selected item of the combobox
 'and the "number" is your colomn name, you need one of these blocks for every colomn you want
 'to search on. The wts varible is what to search for for your textbox. YOu need to put the
 'corresponding field number in the item(x,?) where colomn number is ? starting at 0
If LCase(fieldsearch) = "number" Then   Â
        x = 0
        For x = 1 To datarowcount
            If (DataGrid1.Item(x, 0).ToString = wts Then
            DataGrid1.Select(x)
            DataGrid1.SelectionBackColor = Color.Khaki
            DataGrid1.SelectionForeColor = Color.Black
          End If
        Next
      End If
end sub
chrisryhal
ASKER
Can you please explain assigning the FieldSearchVariale
Shittash
Ok
your combobox has lots of items a,b,c etc
if you do something simple like
if combobox1.selecteditem = "What ever is in you list" then fieldsearch = "The Colomn name"
in the combobox changed event.
So if in your list you have all of your colomn names A, B, C etc then fieldsearch will be A or B or C etc and that will have the declare the variable and your loop for that variable will do whatever you set. So if you wanted a row to go green for one type of search and yellow for another you just change the color.green or color.black etc.
My appologies, however, in this example, are you utilizing the code you posted above, because I see no reference to it. Â I am having horrible issues understanding what "fieldsearch" is. Â Also, when I place (in my case) the following in my changed event for the combo, I get declaration errors. Â I am extremely new to VB.NET datagrids, so I am sorry about that, and I appreciate your help. Â
In my case, here is what I put in my changed event:
=============
  Private Sub cmbSortBy_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbSortBy.SelectedIndexChanged
    If cmdSortBy.selecteditem = "Tool Number" Then fieldsearch = "ToolNumber"
=============
Fieldsearch and cmbSortby are asking for declaration.
chrisryhal
ASKER
I just need to understand FieldSearch and I think I should be good
Private Sub cmbSortBy_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbSortBy.SelectedIndexChanged
    If cmdSortBy.selecteditem = "Tool Number" Then fieldsearch = "ToolNumber"
end sub
Private Sub txtSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtSearch.Click
If LCase(fieldsearch) = "toolnumber" Then   Â
        x = 0
        For x = 1 To datarowcount
            If (DataGrid1.Item(x, 0).ToString = wts Then
            DataGrid1.Select(x)
            DataGrid1.SelectionBackColor = Color.Khaki
            DataGrid1.SelectionForeColor = Color.Black
          End If
        Next
      End If
  End Sub
chrisryhal
ASKER
.Windows.Forms.Control.x' is not accessible in this context because it is 'Private'.
chrisryhal
ASKER
I am sorry, I simply am not familier with datagrids, but am trying the best I can. Â I hate to be a pain. Â Here is how I implemented your code. Â I am getting declaration errors on the "WTS" and the "DataRowCount" and that "x" can't be used because the context is Private
  Private Sub txtSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtSearch.Click
    If LCase(fieldsearch) = "toolnumber" Then
      x = 0
      For x = 1 To datarowcount
            If (ToolTrackerDataGrid.Item(x, 0).ToString = wts Then
          ToolTrackerDataGrid.Select(x)
          ToolTrackerDataGrid.SelectionBackColor = Color.Khaki
          ToolTrackerDataGrid.SelectionForeColor = Color.Black
        End If
      Next
    End If
Hey Shittash, thanks a lot, I was finally able to implement your code after reviewing a view MSDN documents. Â I appreciate all of your help
CR
chrisryhal
ASKER
As far as the solution, I marked the one that was correct. Â It was a "user" error on my part not being very skilled with Datagrids. Â I am sure others will not have issues like I did.
CR
Shittash
no problem buddy  just ask if you want help with anything else....... points welcome of course!
http://aspnet.4guysfromrolla.com/demos/dgHighlight.aspx