Archivio Posts |
Anno 2014
Anno 2013
Anno 2012
|
Statistiche |
- Views Home Page: 73.755
- Views Posts: 551.451
- Views Gallerie: 0
- n° Posts: 210
- n° Commenti: 224
|
|
VB.NET - Filtrare i Dati di un DataGridView e popolare un'altro DataGridView con i risultati (Codice
VB.NET - Filtrare i Dati di un DataGridView e popolare un'altro DataGridView con i risultati (Codice )
Oggi vi voglio mettere a disposizione una funzione che vi permette di popolare un'altra DataGridView e di inserire i risultati della ricerca senza modificare il DataGridView principale.
Il motivo di utilizzare tale funzione è perché non vogliamo usare database. Vi ricordo che fare le ricerche con il database sono più veloci che usare questa semplice funzione.
Esempio di come richiamare la funzione :
CODICE VISUAL STUDIO 2010 - VISUAL BASIC .NET - VB.NET: 'FiltraTabella(search.Text, cmbCollon.Text, DataGridView1, DataGridView2)
FiltraTabella("Produt 1", "Product Name", DataGridView1, DataGridView2) 'Cerca nella colonna "Product Name" il valore "Product 1"
Codice del progetto , Ricordati di inserire due DataGridView , un TextBox e un ComboBox.
CODICE VISUAL STUDIO 2010 - VISUAL BASIC .NET - VB.NET: Public Class Form1
Private Sub DataGridView1_MouseWheel(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DataGridView1.MouseWheel Dim currentIndex As Integer = Me.DataGridView1.FirstDisplayedScrollingRowIndex Dim scrollLines As Integer = SystemInformation.MouseWheelScrollLines
Select Case e.Delta Case 120 'Scrolling up 'We cannot scroll up past row 0. Me.DataGridView1.FirstDisplayedScrollingRowIndex = Math.Max(0, currentIndex - scrollLines) Case -120 'Scrolling down Me.DataGridView1.FirstDisplayedScrollingRowIndex = currentIndex + scrollLines End Select
''Happening when the User used the mouse wheel 'If e.Delta > 0 Then ' SendKeys.Send("{UP}") 'If the wheel is going up 'Else ' SendKeys.Send("{DOWN}") 'If the wheel is going down 'End If End Sub
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load DataGridView1.ColumnCount = 3 DataGridView1.Columns(0).Name = "Product ID" DataGridView1.Columns(1).Name = "Product Name" DataGridView1.Columns(2).Name = "Product_Price"
For i As Integer = 1 To 100 Step 1 Dim row As String() = New String() {i, "Product " & i, "1000"} DataGridView1.Rows.Add(row) Next
For i As Integer = 1 To 5 Step 1 Dim row As String() = New String() {"5", "Product " & "5", "1005"} DataGridView1.Rows.Add(row) Next
CaricaComboBoxColonne(DataGridView1) cmbCollon.Text = cmbCollon.Items(0).ToString cmbCollon.DropDownStyle = ComboBoxStyle.DropDownList
End Sub ''' <summary> ''' FiltraTabella - Ottini i risultati nel nuovo DataGridView Passato ''' </summary> ''' <param name="Valore">Parola da cercare</param> ''' <param name="NomeColonna">Colonna da Cercare</param> ''' <param name="DataGridViewAnalizzer">DataGridView In - Che contiene il informazioni</param> ''' <param name="DataGridViewNew">DataGridView OutPut - Che conterra le informazioni trovate</param> ''' <returns></returns> ''' <remarks></remarks> Private Function FiltraTabella(ByVal Valore As String, ByVal NomeColonna As String, ByVal DataGridViewAnalizzer As DataGridView, ByVal DataGridViewNew As DataGridView) As DataGridView Try Dim ArrayIndexPosition As New List(Of Integer) ScriviStrutturaColonna(DataGridViewAnalizzer, DataGridViewNew) 'ScriviStrutturaColonna(DataGridViewAnalizzer, DataGridView2) Dim rowIndex As Integer = -1 For Each row As DataGridViewRow In DataGridViewAnalizzer.Rows If row.Cells(NomeColonna).Value Is Nothing Then Console.WriteLine("Fine Ciclo") Else If row.Cells(NomeColonna).Value.ToString().Equals(Valore) Then ArrayIndexPosition.Add(row.Index) Dim StringaValori As New List(Of String) StringaValori = CaricaArray(row) DataGridViewNew.Rows.Add(StringaValori.ToArray) ' rowIndex = row.Index 'Exit For End If End If Next Console.WriteLine("Grandezza = " & ArrayIndexPosition.Count) Console.WriteLine("Tutto ok") Return DataGridViewNew Catch ex As Exception 'Errore non trovato o altro Console.WriteLine(ex.Message.ToString) Return Nothing End Try End Function ''' <summary> ''' ScriviStrutturaColonna = Legge la struttura di oldDGW e la copia al nuovo newDGW ''' </summary> ''' <param name="oldDGW">Vecchio DataGridView </param> ''' <param name="newDGW">Nuovo DataGridView</param> ''' <returns></returns> ''' <remarks></remarks> Private Function ScriviStrutturaColonna(ByVal oldDGW As DataGridView, ByVal newDGW As DataGridView) As DataGridView For Each colonna As DataGridViewColumn In oldDGW.Columns Dim newCol As New DataGridViewColumn newCol.CellTemplate = colonna.CellTemplate newCol.DefaultCellStyle = colonna.DefaultCellStyle newCol.HeaderText = colonna.HeaderText newCol.Name = colonna.Name newCol.ReadOnly = colonna.ReadOnly newCol.Width = colonna.Width newCol.Visible = colonna.Visible newDGW.Columns.Add(newCol) Console.WriteLine(colonna.Name) Next Return newDGW End Function Private Sub CaricaComboBoxColonne(ByVal DataGridView As DataGridView) For Each colonna As DataGridViewColumn In DataGridView.Columns cmbCollon.Items.Add(colonna.Name.ToString) Console.WriteLine(colonna.Name) Next End Sub Private Function CaricaArray(ByVal row As DataGridViewRow) As List(Of String) Try Dim Stringa As New List(Of String) For Each cella As DataGridViewCell In row.Cells Stringa.Add(cella.Value.ToString) Next Console.WriteLine("Tutto OK Array Stringa") Return Stringa Catch ex As Exception Console.WriteLine(ex.Message.ToString) Return Nothing End Try End Function Private Sub PulisciDataGridViewRicerca(ByVal DataGridView As DataGridView) DataGridView.Rows.Clear() DataGridView.Columns.Clear() End Sub
Private Sub EffettuaRicerca() If search.Text <> "" And cmbCollon.Text <> "" Then PulisciDataGridViewRicerca(DataGridView2) DataGridView1.Visible = False DataGridView2.Visible = True FiltraTabella(search.Text, cmbCollon.Text, DataGridView1, DataGridView2) Else DataGridView1.Visible = True DataGridView2.Visible = False PulisciDataGridViewRicerca(DataGridView2) End If End Sub
Private Sub search_TextChanged(sender As System.Object, e As System.EventArgs) Handles search.TextChanged EffettuaRicerca() End Sub
Private Sub cmbCollon_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles cmbCollon.SelectedIndexChanged EffettuaRicerca() End Sub
End Class Potete scaricare il file d'esempio :
Password : "ImaginSystem" By ImaginSystems & Queen Gin
lunedì, 09 set 2013 Ore. 12.39
|
|