Solved

row count of datatable

Posted on 2011-09-17
12
414 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

617 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