Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

row count of datatable

Posted on 2011-09-17
12
Medium Priority
?
419 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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 2000 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

[Webinar] Cloud Security

In this webinar you will learn:

-Why existing firewall and DMZ architectures are not suited for securing cloud applications
-How to make your enterprise “Cloud Ready”, and fix your aging DMZ architecture
-How to transform your enterprise and become a Cloud Enabler

Question has a verified solution.

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

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
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 …
Integration Management Part 2
Loops Section Overview

916 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