Click here to Skip to main content
15,902,112 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have some data in data set.I want to check the data with a specific condition,if the condition is true then a text box should be visible otherwise text box should n't be visible.In dataset I have 20 records.

the condition is like:
C#
if ((dataset.Tables[0].Rows[i][0].ToString())=="FirstName"&&(dataset.Tables[0].Rows[i][3].ToString())=="block")
                     
{
     divFirstName.Visible = true;
     txtFName.Text = objUser.FirstName;
}

Note:
Here "firstname" and "block" are the values in columns.
if a particular row meets the condition,then we have to show text box.
we need to search that values in data set rows collection.

How to write for loop for this requirement?

Thanks in Advance
Posted
Updated 8-Sep-15 4:40am
v3
Comments
PIEBALDconsult 8-Sep-15 10:39am    
Please first look at the DefaultView property of the DataTable; it has a RowFilter property that will likely do what you want.
Something like: dataset.Tables[0].DefaultView.RowFilter = "column0='FirstName' AND column3='block'"
Then dataset.Tables[0].DefaultView will "contain" only those rows, with no additional coding by you.
See also: http://www.codeproject.com/Tips/850878/On-Why-to-Use-DataViews
Richard MacCutchan 8-Sep-15 10:51am    
That's a +5 solution.
ZurdoDev 8-Sep-15 11:04am    
Look at what PIEBALDconsult said and do that. However, your code is already written for a loop so if you don't realize that my guess is you copied the code somewhere and don't understand what it does. Asking us to turn it into a loop for you will not do you any good because you need to understand how code works.

1 solution

You can use foreach loop here as follows,
C#
foreach(DataRow objRow in dataset.Tables[0].Rows)
{
   if(objRow[0].ToString().Trim() == "FirstName" &&
      objRow[3].ToString().Trim() == "block")
   {
      divFirstName.Visible = true;
      txtFName.Text = objUser.FirstName;
   }
}
 
Share this answer
 
Comments
PIEBALDconsult 8-Sep-15 14:08pm    
Don't use foreach when for will work.
Don't call ToString on string fields -- just cast. And in this case you don't even need to do that.
VR Karthikeyan 8-Sep-15 23:25pm    
@PIEBALDconsult, Thank you for your information. May I know the reasons for above, it will helps me to understand C# better. Thanks again
PIEBALDconsult 9-Sep-15 0:32am    
http://www.codeproject.com/Tips/850878/On-Why-to-Use-DataViews

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900