Solved

row count of datatable

Posted on 2011-09-17
12
407 Views
Last Modified: 2012-05-12
Hi Expert
Using the following code
 
 dt2 = DS2.Tables("codes")
 dt2.DefaultView.RowFilter = "a3 ='" & TextBox3.Text & "'"
 grid3.DataSource = dt2

IN grid3 I got few rows showing.
I would like to loop through those rows and check
 using  if col3='pads' and col5 is empty
                store col4 to veriable feex
            endif

please help me write the code using visual studio 2008

Thanks
0
Comment
Question by:b001
  • 6
  • 4
  • 2
12 Comments
 
LVL 3

Expert Comment

by:nixkuroi
ID: 36554266
If you're familiar with Linq, you can probably do something like this:

grid3.Rows.ToList().ForEach(f=>
                                 {if (f.col3==pads && string.IsNullOrWhiteSpace(f.col5))
                                      storeColToVariableFeex(col4);
                                 });
0
 
LVL 3

Expert Comment

by:chandrasekar1
ID: 36554293
or can go with simple .net code

Dim intcount As Integer = 0
For Each Row As DataGridViewRow In grid3.Rows
If grid3.Rows(intcount).Cells.Item(“col3”).Value = "pads" Then
'Do Something
intcount +=1
End If
Next Row
0
 
LVL 3

Expert Comment

by:nixkuroi
ID: 36554358
chandrasekar1:

Are you sure you need the intcount if you're doing a for each?  Shouldn't you just be able to use Row?
0
 
LVL 3

Expert Comment

by:chandrasekar1
ID: 36554633
thanks nix,

please try below code

 For Each gvRow As GridViewRow In grid3.Rows
            If CType(gvRow.FindControl("col3labelname"), Label).Text = "pads" And CType(gvRow.FindControl("col5labelname"), Label).Text = "" Then
                'Do something
            End If
        Next
0
 

Author Comment

by:b001
ID: 36555954
hi chandrasekar1:
I have an error

Type  'GridViewRow'  is not defined

Thanks
0
 
LVL 3

Expert Comment

by:chandrasekar1
ID: 36556408
Sorry, one basic question, which application u r using vb.net windows or web? , the above code works for web app..
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Comment

by:b001
ID: 36556413
vb.net windows
0
 
LVL 3

Expert Comment

by:chandrasekar1
ID: 36556476
For Each dgvr As DataGridViewRow In grid3.Rows
            If dgvr.Cells("col3name").Value = "pads" And dgvr.Cells("col5name").Value = "" Then
                'Do something
                dgvr.Cells("col4name").Value = "feex"
            End If
        Next
0
 

Author Comment

by:b001
ID: 36556533
Hi chandrasekar1:
It Works
Sometime
dgvr.Cells("col3name").Value   in  NULL

Where ever it NULL it Crashes.

How can I overcome that problem.

Thanks
0
 
LVL 3

Expert Comment

by:chandrasekar1
ID: 36556552
can u please share ur code here (both binding and looping), let me check, its doesn't throws error for me  even its null
0
 

Author Comment

by:b001
ID: 36556606
Hi    
  Table column name   a12  is datatype  money
                                  a21  is                varchar(255)

   dt2 = DS2.Tables("codes")
     dt2.DefaultView.RowFilter = "a3 ='" & TextBox3.Text & "'"
        grid3.DataSource = dt2
        grid3.Columns("Prefix").Visible = False
        grid3.Columns("a3").Visible = True
        grid3.Columns("a21").Visible = True
        grid3.Columns("a3").DisplayIndex = 0
        grid3.Columns("a1").DisplayIndex = 1
        grid3.Columns("a12").DisplayIndex = 2
        grid3.Columns("a21").DisplayIndex = 2
        grid3.Columns("a22").DisplayIndex = 2
   
For Each dgvr As DataGridViewRow In grid3.Rows
           If dgvr.Cells("a12").Value = 0 And dgvr.Cells("a21").Value = "" Then
                MsgBox("Zero")
            End If
   next


Thanks
0
 
LVL 3

Accepted Solution

by:
chandrasekar1 earned 500 total points
ID: 36556678
I have updated the if condition to handle null
For Each dgvr As DataGridViewRow In grid3.Rows
           If Convert.ToDouble(dgvr.Cells("a12").Value) = 0 And dgvr.Cells("a21").Value = "" Then
                MsgBox("Zero")
            End If
   next

FYI, this Convert.ToDouble() will handle all the float or double digits and null, except characters, if characters also came, you to write a special case to check it.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This video discusses moving either the default database or any database to a new volume.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now