468,145 Members | 1,386 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,145 developers. It's quick & easy.

Inputes in datagridview

Hi all i have 2 forms which every form has a datagrid.the 1st form is a datagrid with 4 columns (from,to,grade & points) where i enter the range of scores from students and save them. The second form has a datagrid with 4 columns(subjects,scores,grade & points) where subjects, grade & points are readonly. How can i code the second form when i enter the scores of the subjects to display the grades and points as per the first form automatically?
Aug 31 '18 #1
1 2739
SioSio
238 128KB
Make Datagridview of Form1 Public so that it can be referenced from Form2
Expand|Select|Wrap|Line Numbers
  1. Public Partial Class Form1
  2.     Public DataGridView1 As System.Windows.Forms.DataGridView
  3.     Public Sub New()
  4.         ' The Me.InitializeComponent call is required for Windows Forms designer support.
  5.         Me.InitializeComponent()
  6.  
  7.         'dataGridView1
  8.         DataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize
  9.         DataGridView1.Location = New System.Drawing.Point(126, 228)
  10.         DataGridView1.Name = "DataGridView1"
  11.         DataGridView1.RowTemplate.Height = 30
  12.         DataGridView1.Size = New System.Drawing.Size(896, 540)
  13.  
  14.         Dim textColumn1 As New DataGridViewTextBoxColumn()
  15.         Dim textColumn2 As New DataGridViewTextBoxColumn()
  16.         Dim textColumn3 As New DataGridViewTextBoxColumn()
  17.         Dim textColumn4 As New DataGridViewTextBoxColumn()
  18.         textColumn1.DataPropertyName = "From"
  19.         textColumn1.Name = "From"
  20.         textColumn1.HeaderText = "From"
  21.          DataGridView1.Columns.Add(textColumn1)
  22.         textColumn2.DataPropertyName = "To"
  23.         textColumn2.Name = "To"
  24.          DataGridView1.Columns.Add(textColumn2)
  25.         textColumn3.DataPropertyName = "Grade"
  26.         textColumn3.Name = "Grade"
  27.          DataGridView1.Columns.Add(textColumn3)
  28.         textColumn4.DataPropertyName = "Points"
  29.         textColumn4.Name = "Points"
  30.          DataGridView1.Columns.Add(textColumn4)
  31.  
  32.         DataGridView1.Rows.Add(0, 49, "F", 0)
  33.         DataGridView1.Rows.Add(50, 59, "E", 1)
  34.         DataGridView1.Rows.Add(60, 69, "D", 2)
  35.         DataGridView1.Rows.Add(70, 79, "C", 3)
  36.         DataGridView1.Rows.Add(80, 89, "B", 4)
  37.         DataGridView1.Rows.Add(90, 100, "A", 5)    
  38.  
  39.         DataGridView1.Columns(0).ReadOnly = True
  40.         DataGridView1.Columns(1).ReadOnly = True
  41.         DataGridView1.Columns(2).ReadOnly = True
  42.         DataGridView1.Columns(3).ReadOnly = True
  43.     End Sub
  44. End Class
  45.  
  46. Public Partial Class Form2
  47.     Public Sub New()
  48.         ' The Me.InitializeComponent call is required for Windows Forms designer support.
  49.         Me.InitializeComponent()
  50.  
  51.         DataGridView1.ColumnCount = 4
  52.         DataGridView1.AllowUserToAddRows = False
  53.         DataGridView1.Columns(0).HeaderText = "Subject"
  54.         DataGridView1.Columns(1).HeaderText = "Score"
  55.         DataGridView1.Columns(2).HeaderText = "Grade"
  56.         DataGridView1.Columns(3).HeaderText = "Points"
  57.         DataGridView1.Rows.Add("Sub1", "", "", 0)
  58.         DataGridView1.Rows.Add("Sub2", "", "", 0)
  59.         DataGridView1.Rows.Add("Sub3", "", "", 0)
  60.         DataGridView1.Rows.Add("Sub4", "", "", 0)
  61.         DataGridView1.Rows.Add("Aub4", "", "", 0)
  62.  
  63.         DataGridView1.Columns(0).ReadOnly = True
  64.         DataGridView1.Columns(1).ReadOnly = False
  65.         DataGridView1.Columns(2).ReadOnly = True
  66.         DataGridView1.Columns(3).ReadOnly = True
  67.     End Sub
  68.  
  69. 'CellValidated Event
  70. Private Sub DataGridView1_CellValidated(ByVal sender As Object, _
  71.     ByVal e As DataGridViewCellEventArgs)
  72.     Dim i As Integer
  73.     Try
  74.         Dim dgv As DataGridView = DirectCast(sender, DataGridView)
  75.         Dim columnIndex As Integer = e.ColumnIndex
  76.         Dim HDNameas As string = dgv.Columns(e.ColumnIndex).HeaderText
  77.         Dim Row As DataGridViewRow
  78.         Dim Score As String = Trim(dgv.CurrentCell.Value.ToString)
  79.         If HDNameas = "Score" AndAlso Score IsNot Nothing AndAlso (Score.Length <> 0) then 
  80.             For Each Row In My.Forms.Form1.datagridview1.Rows()
  81.                 Dim Fromdata As Integer = 0
  82.                 Dim Todata As Integer = 0
  83.                 Dim Points As Integer = 0
  84.                 Dim Grade As String = ""
  85.                 If TypeOf Row.Cells(0).Value Is Integer Then
  86.                     Fromdata = CInt(Row.Cells(0).Value)
  87.                 End If
  88.                 If TypeOf Row.Cells(1).Value Is Integer Then
  89.                      Todata = CInt(Row.Cells(1).Value)
  90.                 End If
  91.                 If TypeOf Row.Cells(2).Value Is String Then
  92.                      Grade = Row.Cells(2).Value.ToString
  93.                 End If
  94.                 If TypeOf Row.Cells(3).Value Is Integer Then
  95.                     Points = CInt(Row.Cells(3).Value)
  96.                 End If
  97.                 If (Grade IsNot Nothing) AndAlso (Grade.Length <> 0) Then
  98.                     Dim SSS As Integer = dgv.CurrentCell.RowIndex
  99.                     If CInt(dgv.CurrentCell.Value.ToString) >= Fromdata AndAlso CInt(dgv.CurrentCell.Value.ToString) <= Todata Then
  100.                         dgv(2, dgv.CurrentCell.RowIndex).Value = Grade
  101.                         dgv(3, dgv.CurrentCell.RowIndex).Value = Points
  102.                                             End If
  103.                 End If
  104.             Next
  105.         End if
  106.         Catch ex As Exception
  107.             MsgBox("Error " & ex.Message , MsgBoxStyle.Critical)
  108.         End Try    
  109.     End Sub
  110. End Class
  111.  
Dec 20 '19 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

reply views Thread by DraguVaso | last post: by
10 posts views Thread by Henok Girma | last post: by
7 posts views Thread by Mitchell S. Honnert | last post: by
7 posts views Thread by =?Utf-8?B?TG9zdEluTUQ=?= | last post: by
reply views Thread by jeastman - Hotmail | last post: by
3 posts views Thread by Andrus | last post: by
30 posts views Thread by didacticone | last post: by
1 post views Thread by gcdp | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.