Click here to Skip to main content
16,017,833 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have data in ds and i want to fill in data table
after that i want to take only particular row from that table so how i can do that??????

What I have tried:

C#
oBuyerBO.Ds.Tables.Add(dt);
        foreach (DataRow row in dt.Rows)
        {
            if (row["CompanyID"].ToString() != hidSelectedSupplier.ClientID)
            {
                    return row["CompanyName"];
            }
        }
Posted
Updated 31-May-16 20:27pm
v2

There's - at least - few ways to achieve that...
1) using Linq:
C#
//returns the list of rows which meet some criteria
var resultRow = dt.AsEnumerable()
                  .Where(x => x.Field<int>("CompanyID") != hidSelectedSupplier.ClientID)
                  .ToList();</int>

2) using Filter ans Select methods:
How to: Filter and Sort Directly in Data Tables[^]

Try!
 
Share this answer
 
Hi,

You should use Select() method of the DataTable object like shown below.
C#
string filterCondition = string.format(CultureInfo.CurrentCulture, "{0} = '{1}'", "CompanyName", "Test"); // assuming that you have a column called CompanyName in the DataTable and you are searching for the rows where CompanyName = "Test"

DataRow[] selectedRows = dt.Select(filterCondition);

foreach(DataRow rowItem in selectedRows)
{
    // Do your stuff
}


Please let me know if this is what you are looking for.

Thanks,
Debasis.
 
Share this answer
 
Comments
shukla parth 1-Jun-16 3:13am    
dt = oBuyerBO.Ds.Tables[0];
foreach (DataRow row in dt.Rows)
{
if (oCompanyBO.SelectedSupplierIDs.ToString() != row["CompanyID"].ToString())
{
lstSelectedSuppliers.Value= row["CompanyName"].ToString();
}
}
i am trying this
but this code do not check the if condition
i want to display the value which is not in the oCompanyBO.SelectedSupplierIDs
C#
dt = oBuyerBO.Ds.Tables[0];
           foreach (DataRow row in dt.Rows)
           {
               if (oCompanyBO.SelectedSupplierIDs.ToString() != row["CompanyID"].ToString())
               {
                       lstSelectedSuppliers.Value= row["CompanyName"].ToString();
               }
           }

i am trying this
but this code do not check the if condition
i want to display the value which is not in the oCompanyBO.SelectedSupplierIDs
...
 
Share this answer
 
Comments
Debasis.logica 1-Jun-16 3:57am    
I think oCompanyBO.SelectedSupplierIDs is a list of string (not a string variable). If that is the case, you cannot do a comparison like that. You need to do like

if(oCompanyBO.SelectedSupplierIDs.Contains(row["CompanyID"].ToString()))

instead.

Please let me know.

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