navbar help

rjhe22

Well-known member
Joined
May 22, 2008
Messages
88
Programming Experience
Beginner
hi
i am having a problem. i have a fourm with three rows in it and a nav bar. now what i want to happen is when i click on add on the nav bar it wont let me move on to the new row unless all three rows are filled in any one have any ideas
thats should be a datagrid with three rows
 
Loop through the grid rows before adding the new row. Maybe something like this

VB.NET:
Private Function AllowUpdate() as Boolean
  Dim returnValue as Boolean = True
  For Each row As DataGridViewRow in DataGridView1.Rows
    If row.Cells("ColumnName").Value = "" and row.Cells("AnotherColumn").Value = "" Then
      returnValue = False
    End If
  Next
  return returnValue
End Function
 
No, it doesn't have to be in a function..

VB.NET:
  Dim returnValue as Boolean = True
  For Each row As DataGridViewRow in DataGridView1.Rows
    If row.Cells("ColumnName").Value = "" and row.Cells("AnotherColumn").Value = "" Then
      returnValue = False
    End If
  Next
  If  returnValue then
    ' Add the row
  End If
 
sorry one last thing
with the add the row part what do u mean. i want to keep it generic like u have it

dont think that will work. im not sure if i explained myself well.
what i want to do is when i press the add button in the navbar i fill in the fields and then when i press add agin i want a msgbox to pop up if all the fields are not filled in
 
The way I understand it is you have 3 rows that must be filled in before you allow your user to add anymore rows...

You can use that function to make sure all fields are filled in... if you want to make sure there are at least 3 rows, use:
VB.NET:
If DataGridview1.rows.count >= 3 And AllowUpdate Then
 ' Add the row.
End If
To add a row,

Just do this:
VB.NET:
DataGridview1.rows.add()
 
The way I understand it is you have 3 rows that must be filled in before you allow your user to add anymore rows...
ya that exactly what i want.

if i dont want to name the Column and want to do it generically will that code work
 
if i dont want to name the Column and want to do it generically will that code work

Yes, you can also use row.columns(1), row.columns(2) etc. (I think its a zero based array)..

You can also loop through the columns:
VB.NET:
  Dim allowAdd as Boolean = True
  For Each row As DataGridViewRow in DataGridView1.Rows
    For Each col As DataGridViewColumn in row.Columns 
      if col.value = "" Then allowAdd = False
    Next
  Next
  If DataGridview1.rows.count < 3 Then allowAdd = False
  If  allowAdd then
    DataGridview1.rows.add()
  End If
 
sorry is there a way to do it were some fourm would have 2 fileds some one have 3 etc but it will cover all fourms
 
All you need to do is pass in a ref to the gridview and the number of rows....

The function needs to go into a module and it must be public.

VB.NET:
Public Function AddRow(ByRef Grid As DataGridView, ByVal MinimumRows As Integer) As Boolean
  Dim allowAdd as Boolean = True
  For Each row As DataGridViewRow in Grid.Rows
    For Each col As DataGridViewColumn in row.Columns 
      if col.value = "" Then allowAdd = False
    Next
  Next
  If Grid.rows.count < MinimumRows Then allowAdd = False
  If  allowAdd then
    Grid.rows.add()
  End If
  Return allowAdd
End Function

Call it like this:

VB.NET:
If AddRow(MyDataGrid,3) Then
  Debug.Print("Row Successfully Added")
Else
  Debug.Print("Can't Add Row")
End If
 
getting an error row.Columns.
aslo sorry for repeating myself just want to make sure this will work even if u dont no if the fourms have 2 or 3 or 4 rows etc

again thanks for all the help

fixed that one changed row to me._grdmain but get an error at this line If col.value
 
Sorry, I hadn't checked any of that code.... try this maybe:
VB.NET:
 For Each row As DataGridViewRow In Grid.Rows
            For Each cell As DataGridViewCell In row.Cells
                If cell.value = "" Then allowAdd = False
            Next
        Next
 
Back
Top