Click here to Skip to main content
15,916,189 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Firs My Name Hiren.
I have Made Application For Account.
And This One Of Part Called Ledger(WinForm).

So My Question Is DataGridView Any Cell Selected Then I have Type Name Like "Hiren"

My Curser Selected One By One Text h,i,r,e,n, and Who's Cell Value Hiren That Cell Selected.

if you do not understand this Question please said.

What I have tried:

if (Char.IsLetter(e.KeyChar))
{
    for (int i = 0; i < (Masterdatagrid.Rows.Count); i++)
    {
        if (Masterdatagrid.Rows[i].Cells["CardName"].Value.ToString().StartsWith(e.KeyChar.ToString(), true, CultureInfo.InvariantCulture))
        {
            Masterdatagrid.FirstDisplayedScrollingRowIndex = i;
            Masterdatagrid.Rows[i].Selected = true;
            return; // stop looping
        }
    }
}
Posted
Updated 7-Jun-18 22:34pm
v4
Comments
CHill60 21-May-18 8:49am    
Your question or problem is not clear. However, I personally would not use a KeyUp event for this level of validation - especially as you have put a "BeginEdit" in there. Try using the Leave or Validate events
Member 13359885 7-Jun-18 8:28am    
how can i add photo
CHill60 7-Jun-18 12:08pm    
Add photo to what?
Member 13359885 8-Jun-18 2:31am    
I was updated my question. sorry for the inconvenience. please help me.
CHill60 8-Jun-18 3:51am    
I already did - don't use the KeyUp event for this type of validation. Use the Validate event

1 solution

After reading your question, my understanding is that:
1. You press any char over datagirdview cell and you want to highlight the cell of the grid, which contains that press character inside its content.

Suppose you have data grid view and contains Patient column inside it.
Step 1: Press key "D", so it will highlight the cells of the Patient column of the grid, which contains "D" in their content.

IF this is the requirement:
You can use two events for it as per as my understanding:
1.
DataGridView1.KeyUp
For getting data to highlight.

2.
DataGridView1.CellPainting
For Highlight or paint the data into grid cell.



------------Code.

1. Get the data table.
Function GetTable() As DataTable
        ' Create new DataTable instance.
        Dim table As New DataTable

        ' Create four typed columns in the DataTable.
        table.Columns.Add("Dosage", GetType(Integer))
        table.Columns.Add("Drug", GetType(String))
        table.Columns.Add("Patient", GetType(String))
        table.Columns.Add("Date", GetType(DateTime))
        table.Columns.Add("Select", GetType(Boolean))

        ' Add five rows with those columns filled in the DataTable.
        table.Rows.Add(25, "Indocin", "David", DateTime.Now, False)
        table.Rows.Add(50, "Enebrel", "Sam", DateTime.Now, False)
        table.Rows.Add(10, "Hydralazine", "Christoff", DateTime.Now, False)
        table.Rows.Add(21, "Combivent", "Janet", DateTime.Now, False)
        table.Rows.Add(100, "Dilantin", "Melanie", DateTime.Now, False)
        Return table
    End Function


2. Bind DataGridView with a table in form load.

Private Sub WindowFormForGridSearch_Load(sender As Object, e As EventArgs) Handles Me.Load

       DataGridView1.DataSource = GetTable()

   End Sub


3. Get the data from user enter from key.

// Global variable to hold sreach data.
Dim l_strSerachData As String
    Private Sub DataGridView1_KeyUp(sender As Object, e As KeyEventArgs) Handles DataGridView1.KeyUp

        l_strSerachData = e.KeyData.ToString()
        DataGridView1.DataSource = GetTable() ' It is bind to call paint.

    End Sub


4. Paint the grid cell with l_strSerachData value into "Patient" column of the grid.

Private Sub DataGridView1_CellPainting(sender As Object, e As DataGridViewCellPaintingEventArgs) Handles DataGridView1.CellPainting

        ' Highlight the Patient column data only.
        If Me.DataGridView1.Columns("Patient").Index = e.ColumnIndex AndAlso e.RowIndex >= 0 Then

            ' Data need to highlight into Grid cell.
            Dim sw As String = l_strSerachData
            If Not String.IsNullOrEmpty(sw) Then

                If Not String.IsNullOrWhiteSpace(e.FormattedValue.ToString()) Then

                    Dim val As String = Replace(DirectCast(e.FormattedValue, String), vbCrLf, String.Empty)
                    Dim sindx As Integer = val.ToLower.IndexOf(sw.ToLower)
                    If sindx >= 0 Then

                        e.Handled = True
                        e.PaintBackground(e.CellBounds, True)

                        'the highlite rectangle
                        Dim hl_rect As New Rectangle()
                        hl_rect.Y = e.CellBounds.Y + 2
                        hl_rect.Height = e.CellBounds.Height - 5

                        'find the size of the text before the search word
                        'and the size of the search word
                        Dim sBefore As String = val.Substring(0, sindx)
                        Dim sWord As String = val.Substring(sindx, sw.Length)
                        Dim s1 As Size = TextRenderer.MeasureText(e.Graphics, sBefore, e.CellStyle.Font, e.CellBounds.Size)
                        Dim s2 As Size = TextRenderer.MeasureText(e.Graphics, sWord, e.CellStyle.Font, e.CellBounds.Size)

                        'adjust the widths to make the highlite more accurate
                        If s1.Width > 5 Then
                            hl_rect.X = e.CellBounds.X + s1.Width - 5
                            hl_rect.Width = s2.Width - 6
                        Else
                            hl_rect.X = e.CellBounds.X + 2
                            hl_rect.Width = s2.Width - 6
                        End If

                        'use darker highlight when the row is selected
                        Dim hl_brush As SolidBrush

                        hl_brush = New SolidBrush(Color.Yellow)
                        'paint the background behind the search word
                        e.Graphics.FillRectangle(hl_brush, hl_rect)
                        hl_brush.Dispose()

                        'paint the content as usual
                        e.PaintContent(e.CellBounds)
                    End If

                End If

            End If

        End If

    End Sub
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900