.NET 4.x DataGridView1 like DataGridView2 - Visual Basic .NET

Узнай цену своей работы

Формулировка задачи:

Доброе время суток. Подскажите пожалуйста, как использовать условие отображения информация в DataGridView1 если в DataGridView2 имееются похожие записи?
Листинг программы
  1. For i = 0 To DataGridView1.Rows.Count - 1
  2. For ii = 0 To DataGridView2.Rows.Count - 1
  3. If DataGridView1.Rows(i).Cells(0).Value.ToString Like DataGridView2.Rows(ii).Cells(0).Value.ToString Then
  4. DataGridView1.Rows(i).Cells(0).Value = "ADMIN"
  5. End If
  6. Next
  7. Next

Решение задачи: «.NET 4.x DataGridView1 like DataGridView2»

textual
Листинг программы
  1. Public Class Form4
  2.     Private DT As DataTable
  3.     Private Sub Form3_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
  4.         DT = createTable()
  5.         DataGridView1.ColumnCount = 2
  6.         DataGridView1.RowCount = DT.Rows.Count
  7.         For i = 0 To DataGridView1.RowCount - 1
  8.             DataGridView1.Rows(i).Cells(0).Value = DT.Rows(i).Item(0)
  9.             DataGridView1.Rows(i).Cells(1).Value = DT.Rows(i).Item(1)
  10.         Next
  11.         DataGridView2.ColumnCount = 1
  12.         DataGridView2.RowCount = 20
  13.         Dim rnd As New Random
  14.         For i = 0 To DataGridView2.RowCount - 1
  15.             DataGridView2.Rows(i).Cells(0).Value = rnd.Next(1, 200)
  16.         Next
  17.     End Sub
  18.     Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
  19.         Dim vv As Integer
  20.         For i = 0 To DataGridView2.RowCount - 1
  21.             vv = DataGridView2.Rows(i).Cells(0).Value
  22.             For j = 0 To DataGridView1.RowCount - 1
  23.                 If DataGridView1.Rows(j).Cells(0).Value = vv.ToString Then
  24.                     DataGridView1.Rows(j).Cells(0).Value = "AD"
  25.                     DataGridView1.Rows(j).Cells(0).Style.ForeColor = Color.Red
  26.                 End If
  27.             Next
  28.         Next
  29.     End Sub
  30.     Private Function createTable() As DataTable
  31.         Dim tb As New DataTable
  32.         tb.Columns.Add("ID", System.Type.GetType("System.Int32"))
  33.         tb.Columns.Add("tstValue", System.Type.GetType("System.Int32"))
  34.         tb.PrimaryKey = {tb.Columns(0)}
  35.         Dim rr As DataRow
  36.         Dim rnd As New Random
  37.         For i = 1 To 50
  38.             rr = tb.NewRow
  39.             rr.Item(0) = i
  40.             rr.Item(1) = rnd.Next(10, 90)
  41.             tb.Rows.Add(rr)
  42.         Next
  43.         Return tb
  44.     End Function
  45. End Class

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

10   голосов , оценка 4.2 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут