(MVC) MVC (2017)

Шаблон кода для роботи з DataGridView.

Це моя старенька програма від 2015-го року, яку я використовую для парсінга задач з різноманітних фрілансерских майданчиків, щоб поєднати усі задачки у одну таблу у зручному вигляді, швиденько її пролистати, та автоматично відписатися у цікаві для мене задачки. Вона виводить набагато більше інформації, що показують самі сайти, наприклад на скрині результат парсінку Upwork'а, на якому ви ніколи не побачите (наприклад) країну замовника, а я її бачу, та можу наприклад не звертати уваги на індусів та малазійців, а відразу відписуватися у більш багаті країни. Але я не буду вам описувати усього мого парсеру, це не є задача цієї сторінки, тут я лише опишу свій загальний шаблон коду для роботи з формами з мережею DataGridView.

На самій формі нічого цікавого немає, лише сама мережа, та декілька кнопок та надписів на верхньому тулбарі.



Сама форма відбирає дані за допомогою Linq-to-SQL, тому щоб автоматично вирішувалося питання чистки кешу Linq-to-SQL, я зробив ось такий єкстеншен.


   1:  Module LinqToSqlExtension4
   2:      ''' <summary>
   3:      ''' Return Linq-to-SQL context
   4:      ''' </summary>
   5:      ''' <typeparam name="T"></typeparam>
   6:      ''' <param name="value"></param>
   7:      ''' <param name="ClearCache">True, if need clear cache</param>
   8:      ''' <returns>Linq-to-SQL context</returns>
   9:      <System.Runtime.CompilerServices.Extension()> _
  10:      Public Function GetContext(Of T As System.Data.Linq.DataContext)(ByRef value As T, ByVal ClearCache As Boolean) As T
  11:          If value IsNot Nothing And Not ClearCache Then
  12:              Return value
  13:          Else
  14:              'create new
  15:              Dim ObjConstructors() As Reflection.ConstructorInfo = GetType(T).GetConstructors
  16:              'шукаємо CTOR без параметрів
  17:              For Each One In ObjConstructors
  18:                  If One.GetParameters.Count = 0 Then
  19:                      value = ObjConstructors(0).Invoke(Nothing)
  20:                      Return value
  21:                  End If
  22:              Next
  23:          End If
  24:      End Function
  25:  End Module

Шаблон коду для роботи з DataGridView виглядає ось так.


   1:  Public Class CheckForm
   2:   
   8:   
   9:      Property Start_I As Integer
  10:      Property End_I As Integer
  11:      Property MinSumm As Integer = 100
  12:      Property ZeroPrice As Boolean = False
  13:   
  14:      Dim db1 As ParserDBDataContext
  15:   
  16:      Dim TypeOfSort As Integer = 0
  17:      Dim MaxBids As Integer = 20
  18:      Dim ProjectType As Integer = 0
  19:   
  20:      Dim Projects As System.Collections.Generic.List(Of Freelancer.AllProject)
  21:      Dim WherePredicate As Expressions.Expression(Of Func(Of AllProject, Boolean))
  22:      Dim OrderPredicate As Expressions.Expression(Of Func(Of AllProject, Boolean))
  23:   
  24:      Private Sub LoadForm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  25:          Me.Text = Me.Text & " (" & Start_I & "-" & End_I & ")"
  26:          db1.GetContext(True)
  27:          If ZeroPrice Then
  28:              MinSummToolStripTextBox1.Text = 0
  29:              WherePredicate = Function(x As AllProject) x.Checked Is Nothing And x.BidCount < MaxBids And x.Summ = 0 And x.i >= Start_I And x.i <= End_I
  30:          Else
  31:              MinSummToolStripTextBox1.Text = MinSumm
  32:              WherePredicate = Function(x As AllProject) x.Checked Is Nothing And x.BidCount < MaxBids And x.Summ >= MinSumm And x.i >= Start_I And x.i <= End_I
  33:          End If
  34:          Projects = (db1.AllProjects.Where(WherePredicate).OrderBy(Function(X As AllProject) X.ToMySkill).ThenBy(Function(X As AllProject) X.i)).ToList
  35:          'Projects = (From X In db1.AllProjects Select X Order By X.ToMySkill, X.i Where X.Checked Is Nothing And X.BidCount < MaxBids And X.Summ >= MinSumm And X.i >= Start_I And X.i <= End_I).ToList
  36:          RowCountToolStripLabel.Text = Projects.Count
  37:          MaxBidsToolStripTextBox1.Text = MaxBids
  38:          DataGridView1.AutoGenerateColumns = False
  39:          DataGridView1.Columns(1).DataPropertyName = "Summ"
  40:          DataGridView1.Columns(2).DataPropertyName = "TimeType"
  41:          DataGridView1.Columns(3).DataPropertyName = "HourLeft"
  42:          DataGridView1.Columns(4).DataPropertyName = "BidCount"
  43:          DataGridView1.Columns(5).DataPropertyName = "Country"
  44:          DataGridView1.Columns(6).DataPropertyName = "Category"
  45:          DataGridView1.Columns(7).DataPropertyName = "Title"
  46:          DataGridView1.Columns(8).DataPropertyName = "TXT"
  47:          DataGridView1.DataSource = Projects
  48:          ProjectTypeToolStripComboBox.SelectedIndex = 0
  49:      End Sub
  50:   
  51:      Private Sub DataGridView1_DataBindingComplete(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewBindingCompleteEventArgs) Handles DataGridView1.DataBindingComplete
  52:   
  53:          For i As Integer = 0 To Projects.Count - 1
  54:              DataGridView1.Rows(i).Cells(0).ToolTipText = Projects(i).i
  55:              DataGridView1.Rows(i).Cells(1).ToolTipText = Projects(i).Category
  56:              DataGridView1.Rows(i).Cells(2).ToolTipText = Projects(i).Category
  57:              DataGridView1.Rows(i).Cells(3).ToolTipText = Projects(i).Category
  58:              DataGridView1.Rows(i).Cells(4).ToolTipText = Projects(i).Category
  59:              DataGridView1.Rows(i).Cells(5).ToolTipText = Projects(i).Country
  60:              DataGridView1.Rows(i).Cells(6).ToolTipText = DataGridView1.Rows(i).Cells(5).Value
  61:              '
  62:              Dim Style1 = New DataGridViewCellStyle()
  63:              Dim Blue As Integer = CInt(Math.Max(0, DataGridView1.Rows(i).Cells(3).Value)) * 10
  64:              If Blue > 255 Then Blue = 255
  65:              Style1.BackColor = Color.FromArgb(255, 255, Blue)
  66:              DataGridView1.Rows(i).Cells(3).Style = Style1
  67:              '
  68:              Dim Style2 = New DataGridViewCellStyle()
  69:              Dim Red As Integer = 350 - CInt(Math.Max(0, DataGridView1.Rows(i).Cells(1).Value))
  70:              If Red > 255 Then
  71:                  Red = 255
  72:              ElseIf Red < 0 Then
  73:                  Red = 0
  74:              End If
  75:              Style2.BackColor = Color.FromArgb(Red, 255, 255)
  76:              DataGridView1.Rows(i).Cells(1).Style = Style2
  77:              '
  78:              Dim Style3 = New DataGridViewCellStyle()
  79:              Dim Green As Integer = CInt(DataGridView1.Rows(i).Cells(4).Value) * 20
  80:              If Green > 255 Then Green = 255
  81:              Style3.BackColor = Color.FromArgb(255, Green, 255)
  82:              DataGridView1.Rows(i).Cells(4).Style = Style3
  83:              '
  84:              Dim Style4 = New DataGridViewCellStyle()
  85:              Dim Style41 = New DataGridViewCellStyle()
  86:              Style41.BackColor = Color.FromArgb(0, 255, 0)
  87:              Dim Style42 = New DataGridViewCellStyle()
  88:              Style42.BackColor = Color.FromArgb(0, 0, 255)
  89:              Dim Style43 = New DataGridViewCellStyle()
  90:              Style43.BackColor = Color.FromArgb(0, 128, 128)
  91:              If Projects(i).ProjectType = 2 Then
  92:                  DataGridView1.Rows(i).Cells(0).Style = Style41
  93:              ElseIf Projects(i).ProjectType = 1 Then
  94:                  DataGridView1.Rows(i).Cells(0).Style = Style42
  95:              ElseIf Projects(i).ProjectType = 3 Then
  96:                  DataGridView1.Rows(i).Cells(0).Style = Style43
  97:              End If
  98:              '
  99:              If SearchToolStripTextBox.Text <> "" Then
 100:                  Dim Style5 = New DataGridViewCellStyle()
 101:                  Style5.BackColor = Color.FromArgb(255, 128, 128)
 102:                  If DataGridView1.Rows(i).Cells(7).Value IsNot Nothing And DataGridView1.Rows(i).Cells(8).Value IsNot Nothing Then
 103:                      If DataGridView1.Rows(i).Cells(7).Value.ToString.ToLower.Contains(SearchToolStripTextBox.Text.ToLower) Or DataGridView1.Rows(i).Cells(8).Value.ToString.ToLower.ToLower.Contains(SearchToolStripTextBox.Text.ToLower) Then
 104:                          DataGridView1.Rows(i).Cells(8).Style = Style5
 105:                      End If
 106:                  End If
 107:              End If
 108:          Next
 109:      End Sub
 110:   
 111:   
 112:      Private Sub DataGridView1_CellContentDoubleClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentDoubleClick
 113:          If e.RowIndex >= 0 And e.ColumnIndex >= 6 Then
 114:              Process.Start("C:\Program Files\Mozilla Firefox\Firefox.exe", Projects(e.RowIndex).URL)
 115:          ElseIf e.RowIndex = -1 And e.ColumnIndex <= 6 Then
 116:              Rebind(e.ColumnIndex, False)
 117:          Else
 118:              ParsePage(e.RowIndex)
 119:          End If
 120:      End Sub
 121:   
 122:      Private Sub DelToolStripButton1_Click(sender As System.Object, e As System.EventArgs) Handles DelToolStripButton1.Click
 123:          For i As Integer = 0 To DataGridView1.Rows.Count - 1
 124:              Dim Check1 As Boolean = Convert.ToBoolean(CType(DataGridView1.Rows(i).Cells(0), DataGridViewCheckBoxCell).Value)
 125:              If Check1 Then
 126:                  Debug.Print(i)
 127:                  Dim DelNum As Integer = CInt(DataGridView1.Rows(i).Cells(0).ToolTipText)
 128:                  Dim DelOne = (From X In db1.AllProjects Select X Where X.i = DelNum).ToList
 129:                  If DelOne.Count > 0 Then
 130:                      DelOne(0).Checked = 1
 131:                      db1.SubmitChanges()
 132:                  End If
 133:              End If
 134:          Next
 135:          db1.SubmitChanges()
 136:          Rebind(TypeOfSort, True)
 137:      End Sub
 138:   
 139:      Function ParseRestTime(One As String) As String
 140:          Dim Pos3 As Integer = One(8).ToString.IndexOf(">")
 141:          Dim Pos4 As Integer = One(8).ToString.IndexOf("<", Pos3)
 142:          Return One.Substring(Pos3, Pos4 - Pos3)
 143:      End Function
 144:   
 145:      Private Sub MaxBidsToolStripTextBox1_TextChanged(sender As Object, e As System.EventArgs) Handles MaxBidsToolStripTextBox1.TextChanged
 146:          If Integer.TryParse(MaxBidsToolStripTextBox1.Text, MaxBids) And Integer.TryParse(MinSummToolStripTextBox1.Text, MinSumm) Then
 147:              If DataGridView1.DataSource IsNot Nothing Then
 148:                  Rebind(TypeOfSort, False)
 149:              End If
 150:          End If
 151:      End Sub
 152:   
 153:      Private Sub MinSummToolStripTextBox1_TextChanged(sender As Object, e As System.EventArgs) Handles MinSummToolStripTextBox1.TextChanged
 154:          If Integer.TryParse(MaxBidsToolStripTextBox1.Text, MaxBids) And Integer.TryParse(MinSummToolStripTextBox1.Text, MinSumm) Then
 155:              If DataGridView1.DataSource IsNot Nothing Then
 156:                  Rebind(TypeOfSort, False)
 157:              End If
 158:          End If
 159:      End Sub
 160:   
 167:   
 168:      Private Sub RefreshToolStripButton_Click(sender As System.Object, e As System.EventArgs) Handles RefreshToolStripButton.Click
 169:          Rebind(TypeOfSort, False)
 170:      End Sub
 171:   
 172:      Private Sub ProjectTypeToolStripComboBox_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles ProjectTypeToolStripComboBox.SelectedIndexChanged
 173:          ProjectType = ProjectTypeToolStripComboBox.SelectedIndex
 174:      End Sub
 175:   
 176:      Private Sub DataGridView1_CellContentClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
 177:          Dim GridView = DirectCast(sender, DataGridView)
 178:          If TypeOf GridView.Columns(e.ColumnIndex) Is DataGridViewButtonColumn AndAlso e.RowIndex >= 0 Then
 179:              Dim CurCell = CType(GridView.Rows(e.RowIndex).Cells(e.ColumnIndex), DataGridViewButtonCell)
 180:              Dim Style4 = New DataGridViewCellStyle()
 181:              Style4.BackColor = Color.FromArgb(60, 160, 100)
 182:              CurCell.Style = Style4
 183:              Dim DB_I As Integer = CInt(CType(GridView.Rows(e.RowIndex).Cells(0), DataGridViewCheckBoxCell).ToolTipText)
 184:              Dim OneDbRow As Freelancer.AllProject = (From Z In Projects Select Z Where Z.i = DB_I).First
 185:              Dim X As New CheckFormDetails
 186:              X.ShowTxt = OneDbRow.TXT
 187:              X.Title = OneDbRow.Title
 188:              X.URL = OneDbRow.URL
 189:              X.Show()
 190:          End If
 191:      End Sub
 192:   
 193:   
 194:      Sub Rebind(ByVal ColumnIndex As Integer, ByVal FullReload As Boolean)
 195:          Dim CurCursor = Me.Cursor
 196:          Me.Cursor = Cursors.WaitCursor
 197:          If FullReload Then
 198:              If ProjectType = 0 Then
 199:                  If ZeroPrice Then
 200:                      WherePredicate = Function(x As AllProject) x.Checked Is Nothing And x.BidCount < MaxBids And x.Summ = 0 And x.i >= Start_I And x.i <= End_I
 201:                  Else
 202:                      WherePredicate = Function(x As AllProject) x.Checked Is Nothing And x.BidCount < MaxBids And x.Summ >= MinSumm And x.i >= Start_I And x.i <= End_I
 203:                  End If
 204:                  Select Case ColumnIndex
 205:                      Case 0
 206:                          Projects = (db1.AllProjects.Where(WherePredicate).OrderBy(Function(X As AllProject) X.ToMySkill).ThenBy(Function(X As AllProject) X.i)).ToList
 207:                          TypeOfSort = 0
 208:                      Case 1
 209:                          OrderPredicate = Function(X As AllProject) X.Summ
 210:                          TypeOfSort = 1
 211:                      Case 2
 212:                          OrderPredicate = Function(X As AllProject) X.TimeType
 213:                          TypeOfSort = 2
 214:                      Case 3
 215:                          OrderPredicate = Function(X As AllProject) X.HourLeft
 216:                          TypeOfSort = 3
 217:                      Case 4
 218:                          OrderPredicate = Function(X As AllProject) X.BidCount
 219:                          TypeOfSort = 4
 220:                      Case 5
 221:                          OrderPredicate = Function(X As AllProject) X.Checked
 222:                          TypeOfSort = 5
 223:                      Case 6
 224:                          OrderPredicate = Function(X As AllProject) X.Category
 225:                          TypeOfSort = 6
 226:                  End Select
 227:                  Projects = (db1.AllProjects.Where(WherePredicate).OrderBy(OrderPredicate)).ToList
 228:              Else
 229:                  If ZeroPrice Then
 230:                      WherePredicate = Function(x As AllProject) x.Checked Is Nothing And x.BidCount < MaxBids And x.Summ = 0 And x.i >= Start_I And x.i <= End_I And x.ProjectType = ProjectType
 231:                  Else
 232:                      WherePredicate = Function(x As AllProject) x.Checked Is Nothing And x.BidCount < MaxBids And x.Summ >= MinSumm And x.i >= Start_I And x.i <= End_I And x.ProjectType = ProjectType
 233:                  End If
 234:                  Select Case ColumnIndex
 235:                      Case 0
 236:                          Projects = (db1.AllProjects.Where(WherePredicate).OrderBy(Function(X As AllProject) X.ToMySkill).ThenBy(Function(X As AllProject) X.i)).ToList
 237:                          TypeOfSort = 0
 238:                      Case 1
 239:                          OrderPredicate = Function(X As AllProject) X.Summ
 240:                          TypeOfSort = 1
 241:                      Case 2
 242:                          OrderPredicate = Function(X As AllProject) X.TimeType
 243:                          TypeOfSort = 2
 244:                      Case 3
 245:                          OrderPredicate = Function(X As AllProject) X.HourLeft
 246:                          TypeOfSort = 3
 247:                      Case 4
 248:                          OrderPredicate = Function(X As AllProject) X.BidCount
 249:                          TypeOfSort = 4
 250:                      Case 5
 251:                          OrderPredicate = Function(X As AllProject) X.Checked
 252:                          TypeOfSort = 5
 253:                      Case 6
 254:                          OrderPredicate = Function(X As AllProject) X.Category
 255:                          TypeOfSort = 6
 256:                  End Select
 257:                  Projects = (db1.AllProjects.Where(WherePredicate).OrderBy(OrderPredicate)).ToList
 258:              End If
 259:          Else
 260:              If ProjectType = 0 Then
 261:                  Select Case ColumnIndex
 262:                      Case 0
 263:                          Projects = (From X In Projects Select X Order By X.ToMySkill, X.i Where X.Checked Is Nothing And X.BidCount < MaxBids And X.Summ >= MinSumm).ToList
 264:                          TypeOfSort = 0
 265:                      Case 1
 266:                          Projects = (From X In Projects Select X Order By X.Summ Where X.Checked Is Nothing And X.BidCount < MaxBids And X.Summ >= MinSumm).ToList
 267:                          TypeOfSort = 1
 268:                      Case 2
 269:                          Projects = (From X In Projects Select X Order By X.TimeType Where X.Checked Is Nothing And X.BidCount < MaxBids And X.Summ >= MinSumm).ToList
 270:                          TypeOfSort = 2
 271:                      Case 3
 272:                          Projects = (From X In Projects Select X Order By X.HourLeft Where X.Checked Is Nothing And X.BidCount < MaxBids And X.Summ >= MinSumm).ToList
 273:                          TypeOfSort = 3
 274:                      Case 4
 275:                          Projects = (From X In Projects Select X Order By X.BidCount Where X.Checked Is Nothing And X.BidCount < MaxBids And X.Summ >= MinSumm).ToList
 276:                          TypeOfSort = 4
 277:                      Case 5
 278:                          Projects = (From X In Projects Select X Order By X.Country Where X.Checked Is Nothing And X.BidCount < MaxBids And X.Summ >= MinSumm).ToList
 279:                          TypeOfSort = 5
 280:                      Case 6
 281:                          Projects = (From X In Projects Select X Order By X.Category Where X.Checked Is Nothing And X.BidCount < MaxBids And X.Summ >= MinSumm).ToList
 282:                          TypeOfSort = 6
 283:                  End Select
 284:              Else
 285:                  Select Case ColumnIndex
 286:                      Case 0
 287:                          Projects = (From X In Projects Select X Order By X.ToMySkill, X.i Where X.Checked Is Nothing And X.BidCount < MaxBids And X.Summ >= MinSumm And X.ProjectType = ProjectType).ToList
 288:                          TypeOfSort = 0
 289:                      Case 1
 290:                          Projects = (From X In Projects Select X Order By X.Summ Where X.Checked Is Nothing And X.BidCount < MaxBids And X.Summ >= MinSumm And X.ProjectType = ProjectType).ToList
 291:                          TypeOfSort = 1
 292:                      Case 2
 293:                          Projects = (From X In Projects Select X Order By X.TimeType Where X.Checked Is Nothing And X.BidCount < MaxBids And X.Summ >= MinSumm And X.ProjectType = ProjectType).ToList
 294:                          TypeOfSort = 2
 295:                      Case 3
 296:                          Projects = (From X In Projects Select X Order By X.HourLeft Where X.Checked Is Nothing And X.BidCount < MaxBids And X.Summ >= MinSumm And X.ProjectType = ProjectType).ToList
 297:                          TypeOfSort = 3
 298:                      Case 4
 299:                          Projects = (From X In Projects Select X Order By X.BidCount Where X.Checked Is Nothing And X.BidCount < MaxBids And X.Summ >= MinSumm And X.ProjectType = ProjectType).ToList
 300:                          TypeOfSort = 4
 301:                      Case 5
 302:                          Projects = (From X In Projects Select X Order By X.Country Where X.Checked Is Nothing And X.BidCount < MaxBids And X.Summ >= MinSumm And X.ProjectType = ProjectType).ToList
 303:                          TypeOfSort = 5
 304:                      Case 6
 305:                          Projects = (From X In Projects Select X Order By X.Category Where X.Checked Is Nothing And X.BidCount < MaxBids And X.Summ >= MinSumm And X.ProjectType = ProjectType).ToList
 306:                          TypeOfSort = 6
 307:                  End Select
 308:              End If
 309:          End If
 310:   
 311:          DataGridView1.DataSource = Projects
 312:          RowCountToolStripLabel.Text = Projects.Count
 313:          Me.Cursor = CurCursor
 314:      End Sub
 315:   
 316:      Private Sub CheckAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckAll.Click
 317:          For Each One In Projects
 318:              One.Checked = 1
 319:          Next
 320:          db1.SubmitChanges()
 321:          Me.Close()
 322:      End Sub
 323:  End Class

Сама форма працює на Expression-функції, яка відбирає дані з бази по параметрам, які задаються в момент виклика форми. Спочатку цей код був зроблений на статичних linq, але як раз у цьму році я трохи зробив модифікацію цьєї форми та додав у код WherePredicate. Бо без цього виклик форми був невдалий. А виглядає виклик ось так.



 120:   
 121:      Dim ZeroPrice As Boolean = False
 122:      Private Sub CheckProjectToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckProjectToolStripButton.Click
 123:          Dim ShowCount As Integer = CInt(ToRowI.Text) - CInt(FromRowI.Text)
 124:          If CInt(ShowCount <= 1000) Then
 125:              GoTo RunShow
 126:          End If
 127:          Dim Res As MsgBoxResult = MsgBox("Are you sure?", MsgBoxStyle.OkCancel, "Display " & ShowCount & " rows")
 128:          If Res = MsgBoxResult.Ok Then
 129:  RunShow:
 130:              Dim CheckForm_Instance As New CheckForm
 131:              CheckForm_Instance.ZeroPrice = ZeroPrice
 132:              CheckForm_Instance.Start_I = CInt(FromRowI.Text)
 133:              CheckForm_Instance.End_I = CInt(ToRowI.Text)
 134:              CheckForm_Instance.MinSumm = CInt(MinSummToolStripTextBox1.Text)
 135:              CheckForm_Instance.ShowDialog()
 136:              RefreshCounterToolStripButton1_Click(Nothing, Nothing)
 137:          End If
 138:      End Sub
 139:   
 497:      Private Sub FromMinProceToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles FromMinProceToolStripMenuItem.Click
 498:          ZeroPrice = False
 499:      End Sub
 500:   
 501:      Private Sub ZeroPriceToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ZeroPriceToolStripMenuItem.Click
 502:          ZeroPrice = True
 503:      End Sub



This is another simple program with DataGridView, it can organize your URL, order it and set color mark various URL, story login/password and comment for various sites. Structure of grid form controls please see below.



And this is full code of grid form.


   1:  Imports System.Windows.Forms
   2:   
   3:  Public Class StartForm
   4:   
   5:      Dim FF As New Firefox
   6:      Public db1 As New Url_DB
   7:      Dim Rows As List(Of Site) = db1.Sites.OrderBy(Function(X) X.Pos).ToList
   8:      Dim EditFormInstance As New EditForm
   9:   
  10:      Public Enum nColumn
  11:          FF = 0
  12:          Color = 1
  13:          Down = 2
  14:          Up = 3
  15:          URL = 4
  16:          Login = 5
  17:          Comment = 6
  18:          Edit = 7
  19:          Del = 8
  20:      End Enum
  21:   
  22:      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
  23:          DataGridView1.AutoGenerateColumns = False
  24:          DataGridView1.DataSource = Rows
  25:      End Sub
  26:   
  27:      Private Sub DataGridView1_CellFormatting(sender As Object, e As DataGridViewCellFormattingEventArgs)
  28:          Dim senderGrid = DirectCast(sender, DataGridView)
  29:          Dim ColorStyle = New DataGridViewCellStyle()
  30:              ColorStyle.BackColor = ColorRestore(Rows(e.RowIndex).Color)
  31:              Dim CurCell As DataGridViewCell = senderGrid(e.ColumnIndex, e.RowIndex)
  32:          If e.ColumnIndex = nColumn.URL Then
  33:              CurCell.ToolTipText = senderGrid(nColumn.Comment, e.RowIndex).Value
  34:              CurCell.Style = ColorStyle
  35:          ElseIf e.ColumnIndex = nColumn.Login Then
  36:              CurCell.ToolTipText = senderGrid(nColumn.Comment, e.RowIndex).Value
  37:              CurCell.Style = ColorStyle
  38:          ElseIf e.ColumnIndex = nColumn.Comment Then
  39:              CurCell.ToolTipText = senderGrid(nColumn.Comment, e.RowIndex).Value
  40:              CurCell.Style = ColorStyle
  41:          ElseIf e.ColumnIndex = nColumn.Color Then
  42:              CurCell.ToolTipText = Rows(e.RowIndex).Color
  43:          ElseIf e.ColumnIndex = nColumn.ff Then
  44:              CurCell.ToolTipText = Rows(e.RowIndex).I
  45:          ElseIf e.ColumnIndex = nColumn.Up Or e.ColumnIndex = nColumn.Down Then
  46:              CurCell.ToolTipText = Rows(e.RowIndex).Pos
  47:          ElseIf e.ColumnIndex = nColumn.Edit Or e.ColumnIndex = nColumn.Del Then
  48:              CurCell.ToolTipText = Rows(e.RowIndex).URL
  49:          End If
  50:   
  51:      End Sub
  52:   
  53:      Private Sub BtAdd_Click(sender As Object, e As EventArgs) Handles BtAdd.Click
  54:          EditFormInstance.EditMode = False
  55:          EditFormInstance.ShowDialog()
  56:      End Sub
  57:   
  58:      Private Sub ButtonGo_Click(sender As Object, e As EventArgs) Handles ButtonGo.Click
  59:          For Each OneRow As DataGridViewRow In DataGridView1.Rows
  60:              Dim Check1 As DataGridViewCheckBoxCell = OneRow.Cells(nColumn.FF)
  61:              If Check1.Value Then
  62:                  FF.OpenFirefoxInNewTab(OneRow.Cells(nColumn.URL).Value)
  63:              End If
  64:          Next
  65:      End Sub
  66:   
  67:      Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs)
  68:          If e.RowIndex >= 0 Then
  69:              Dim senderGrid = DirectCast(sender, DataGridView)
  70:              Dim CurRow = Rows(e.RowIndex)
  71:              If TypeOf senderGrid.Columns(e.ColumnIndex) Is DataGridViewCheckBoxColumn AndAlso e.ColumnIndex = nColumn.FF Then
  72:                  Dim Check1 As DataGridViewCheckBoxCell = senderGrid(e.ColumnIndex, e.RowIndex)
  73:                  Check1.Value = Not Check1.Value
  74:              ElseIf TypeOf senderGrid.Columns(e.ColumnIndex) Is DataGridViewImageColumn AndAlso e.ColumnIndex = nColumn.Color Then
  75:                  ColorClick(CurRow)
  76:              ElseIf TypeOf senderGrid.Columns(e.ColumnIndex) Is DataGridViewImageColumn AndAlso e.ColumnIndex = nColumn.Up Then
  77:                  UpClick(CurRow)
  78:              ElseIf TypeOf senderGrid.Columns(e.ColumnIndex) Is DataGridViewImageColumn AndAlso e.ColumnIndex = nColumn.Down Then
  79:                  DownClick(CurRow)
  80:              ElseIf TypeOf senderGrid.Columns(e.ColumnIndex) Is DataGridViewImageColumn AndAlso e.ColumnIndex = nColumn.Edit Then
  81:                  EditClick(CurRow)
  82:              ElseIf TypeOf senderGrid.Columns(e.ColumnIndex) Is DataGridViewImageColumn AndAlso e.ColumnIndex = nColumn.Del Then
  83:                  DelClick(CurRow)
  84:              ElseIf TypeOf senderGrid.Columns(e.ColumnIndex) Is DataGridViewLinkColumn AndAlso e.ColumnIndex = nColumn.URL Then
  85:                  UrlClick(CurRow)
  86:              ElseIf TypeOf senderGrid.Columns(e.ColumnIndex) Is DataGridViewTextBoxColumn AndAlso e.ColumnIndex = nColumn.Login Then
  87:                  LoginClick(CurRow)
  88:              ElseIf TypeOf senderGrid.Columns(e.ColumnIndex) Is DataGridViewTextBoxColumn AndAlso e.ColumnIndex = nColumn.Comment Then
  89:                  CommentClick(CurRow)
  90:              End If
  91:          End If
  92:      End Sub
  93:   
  94:      Sub ColorClick(CurRow As Site)
  95:          Dim ColorForm As New ColorDialog
  96:          If ColorForm.ShowDialog = DialogResult.OK Then
  97:              CurRow.Color = String.Format("{0:X2}", ColorForm.Color.R) & String.Format("{0:X2}", ColorForm.Color.G) & String.Format("{0:X2}", ColorForm.Color.B)
  98:              db1.SaveChanges()
  99:              DataGridView1.Refresh()
 100:          End If
 101:      End Sub
 102:   
 103:      Function ColorRestore(RGB As String) As Color
 104:          If RGB <> "" Then
 105:              Dim R As Integer = Convert.ToUInt32(Mid(RGB, 1, 2), 16)
 106:              Dim G As Integer = Convert.ToUInt32(Mid(RGB, 3, 2), 16)
 107:              Dim B As Integer = Convert.ToUInt32(Mid(RGB, 5, 2), 16)
 108:              Return Color.FromArgb(R, G, B)
 109:          Else
 110:              Return Color.White
 111:          End If
 112:   
 113:      End Function
 114:   
 115:   
 116:      Sub UpClick(CurRow As Site)
 117:          If CurRow.Pos Is Nothing Then CurRow.Pos = CurRow.I
 118:          Dim R As Site = Rows.Where(Function(X) X.I = CurRow.I).FirstOrDefault
 119:          R.Pos = CurRow.Pos - 1
 120:          db1.SaveChanges()
 121:          Rows = db1.Sites.OrderBy(Function(X) X.Pos).ToList
 122:          DataGridView1.DataSource = Rows
 123:          DataGridView1.Refresh()
 124:      End Sub
 125:   
 126:      Sub DownClick(CurRow As Site)
 127:          If CurRow.Pos Is Nothing Then CurRow.Pos = CurRow.I
 128:          Dim R As Site = Rows.Where(Function(X) X.I = CurRow.I).FirstOrDefault
 129:          R.Pos = CurRow.Pos + 1
 130:          db1.SaveChanges()
 131:          Rows = db1.Sites.OrderBy(Function(X) X.Pos).ToList
 132:          DataGridView1.DataSource = Rows
 133:          DataGridView1.Refresh()
 134:      End Sub
 135:   
 136:      Sub EditClick(CurRow As Site)
 137:          EditFormInstance.EditMode = True
 138:          EditFormInstance.CurEditRow = CurRow
 139:          EditFormInstance.URLTextBox.Text = CurRow.URL
 140:          EditFormInstance.LoginTextBox.Text = CurRow.Login
 141:          EditFormInstance.PassTextBox.Text = CurRow.Pass
 142:          EditFormInstance.CommentRichTextBox.Text = CurRow.Comment
 143:          EditFormInstance.ShowDialog()
 144:          DataGridView1.Refresh()
 145:      End Sub
 146:   
 147:      Sub DelClick(CurRow As Site)
 148:          Dim R As Site = Rows.Where(Function(X) X.I = CurRow.I).FirstOrDefault
 149:          db1.Sites.Remove(R)
 150:          db1.SaveChanges()
 151:          Rows = db1.Sites.OrderBy(Function(X) X.Pos).ToList
 152:          DataGridView1.DataSource = Rows
 153:          DataGridView1.Refresh()
 154:      End Sub
 155:   
 156:      Sub UrlClick(CurRow As Site)
 157:          FF.OpenFirefoxInNewTab(CurRow.URL)
 158:      End Sub
 159:   
 160:      Sub LoginClick(CurRow As Site)
 161:          EditClick(CurRow)
 162:      End Sub
 163:   
 164:      Sub CommentClick(CurRow As Site)
 165:          EditClick(CurRow)
 166:      End Sub
 167:   
 168:  End Class


Comments ( )
<00>  <01>  <02>  <03>  <04>  <05>  <06>  <07>  <08>  <09>  <10>  <11>  <12>  <13>  <14>  <15>  <16>  <17>  <18>  <19
Link to this page: http://www.vb-net.com/DataGridView/index.htm
<SITEMAP>  <MVC>  <ASP>  <NET>  <DATA>  <KIOSK>  <FLEX>  <SQL>  <NOTES>  <LINUX>  <MONO>  <FREEWARE>  <DOCS>  <ENG>  <MAIL ME>  <ABOUT ME>  < THANKS ME>