2017-10-11 7 views
0

ユーザーは、datagridviewでユニットが空のqtyを入力できません。2番目のセルが空の場合にセルを読み取り専用にする方法

明らかにするには、ユニットの列が空の場合は、セルreadonly = trueを作成します。

colUOM4は、この列のセルが空の場合、olNewQty2セルが読み込み専用になる列の名前です。

それが唯一のいずれかに反応し、あなたがそれを実行したときの状態を設定しないので、私はループを使用していないお勧めします私はこのコードを試みたが、それは

Public Sub UnitEmpty() 
    For i As Integer = 0 To dgvCount.RowCount - 1 
     If dgvCount.Rows(i).Cells("colUOM4").Value Is Nothing Then 
      MessageBox.Show("Its Worked!") 
      dgvCount.Rows(i).Cells("colNewQty2").ReadOnly = True 
     Else 
      MessageBox.Show("Nothing happened!") 

      Exit For 
     End If 
    Next 
End Sub 

this is my datagridview and the name for it is dgvCount

答えて

1

を動作しませんでした変更。行とセルレベルで作業することをおすすめします。つまり、行が追加されたときのデフォルトの状態を設定し、特定のセルが変更されたときに反応することをおすすめします。

Private Sub DataGridView1_RowsAdded(sender As Object, e As DataGridViewRowsAddedEventArgs) Handles DataGridView1.RowsAdded 
    For i = e.RowIndex To e.RowIndex + e.RowCount - 1 
     'Make the first cell in each new row read-only by default. 
     DataGridView1(0, i).ReadOnly = True 
    Next 
End Sub 

Private Sub DataGridView1_CellValueChanged(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellValueChanged 
    'Check whether the change is in the second column. 
    If e.RowIndex >= 0 AndAlso e.ColumnIndex = 1 Then 
     Dim row = DataGridView1.Rows(e.RowIndex) 

     'Make the first cell in the row read-only if and only if the second cell is empty. 
     row.Cells(0).ReadOnly = (row.Cells(1).Value Is Nothing) 
    End If 
End Sub 
+0

私はすぐにこれを試してみます。 –

+0

それは動作しませんでした..それはまだ2番目の列のセルが空でも、他の列を編集することができます –

+0

私はあなたが何か間違っていたと思われるので、私のために働いたテストプロジェクトから直接そのコードをコピーしました。私は空のグリッドから始め、新しい行を直接入力しました。その場合、あなたのために機能しますか?それがうまくいかなかったとき、あなたは何をしましたか? – jmcilhinney

関連する問題