Solved

row count of datatable

Posted on 2011-09-17
12
409 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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
 

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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

810 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