2017-07-16 105 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