2017-07-16 110 views
-1

我会尽量让移动平均线在VB i want to check the cells and set the value to text box移动平均法VB

但结果是 all the text box has the same value

如何使我的第一校验值(penjualan /补栏)便被输入到第一个文本框和第二个检查(penjualan/bulan)到第二个文本框。 这里是我的代码

Private Sub DataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick 
     If e.ColumnIndex = 5 Then 
      tb1.Text = DataGridView1.CurrentRow.Cells(3).Value 
      tb2.Text = DataGridView1.CurrentRow.Cells(3).Value 
      tb3.Text = DataGridView1.CurrentRow.Cells(3).Value 
     End If 
    End Sub 

谢谢。

+0

请阅读[问]并参加[旅游]。所有与你的问题有关的事实都需要在这个问题上;并且需要一些自己解决的努力 – Plutonix

回答

0

您设置每次将cellClicked-event所有3个文本框都提升为相同的值CurrentRow.Cells(3).Value。 另一个问题是您的代码会始终设置文本框中的文本。它不检查复选框是否被选中。每次单击此列中的任何单元格时,它都会更新,将3个框中的文本添加到当前选定行的值中。

在这里你有一个解决方案。它不完美,但应该工作,但你应该尝试理解和优化它。

Public Class Form1 

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load 
     InitializeDgv() 
    End Sub 

    Private Sub InitializeDgv() 
     Dim row as String() = New String(){"2016",240} 
     DataGridView1.Rows.Add(row) 
     row = New String(){"2017",223} 
     DataGridView1.Rows.Add(row) 
     row = New String(){"2015",54} 
     DataGridView1.Rows.Add(row) 
    End Sub 

    Private Sub DataGridView1_CellValueChanged(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellValueChanged 
     if e.ColumnIndex=2 

      Dim checkedRows=(From dgv as DataGridViewRow in DataGridView1.Rows where dgv.Cells(2).Value=True select dgv).ToList() 
      Dim controlsList As new List(of TextBox) 
      controlsList.Add(TextBox1) 
      controlsList.Add(TextBox2) 
      controlsList.Add(TextBox3) 
      for Each item in controlsList 
       item.Text=String.Empty 
      Next 
      for i=0 to checkedRows.Count-1 
       controlsList(i).Text=checkedRows.Item(i).Cells(0).Value 
      Next 
     End If 
    End Sub 
End Class