Solved

row count of datatable

Posted on 2011-09-17
12
412 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
Independent Software Vendors: 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

Independent Software Vendors: 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!

Question has a verified solution.

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

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

738 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