Datagridview搜索值使用按键事件相同的dificuty [英] Datagridview search value using key press event same dificaluty

查看:109
本文介绍了Datagridview搜索值使用按键事件相同的dificuty的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Firs My Name Hiren。

我已申请帐户。

而这一部分名为Ledger(WinForm)。



所以我的问题是DataGridView任何选择的单元格然后我的类型名称就像Hiren



我的Curser选择逐个文本h, i,r,e,n,以及Who的Cell Value Hiren那个单元格选择。



如果你不明白这个问题请说。



我的尝试:



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
        }
    }
}

推荐答案

阅读完之后问题,我的理解是:

1.你按下datagirdview单元格上的任何字符,你想要突出显示网格的单元格,其中包含其内容中的按字符。



假设您有数据网格视图并且其中包含Patient列。

步骤1:按D键,这样它将突出显示Patient列的单元格网格,其内容中包含D。



如果这是要求:

您可以根据我的理解使用两个事件:

1.
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

获取要突出显示的数据。



2。

For getting data to highlight.

2.

DataGridView1.CellPainting

用于突出显示或将数据绘制到网格单元格中。







------------代码。



1。获取数据表。

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

< br $> b $ b

2。使用表格加载表绑定DataGridView。





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。从用户输入数据从键输入。





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。使用l_strSerachData值将网格单元格绘制到网格的患者列中。





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


这篇关于Datagridview搜索值使用按键事件相同的dificuty的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆