Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Add filtered rows to DataTable

Status
Not open for further replies.

ralphtrent

Programmer
Jun 2, 2003
958
US
Hello
I have a datatable filled with many rows. I used DataTable.Select to filter the rows. I then want to add only add those rows back to the datatable after I remove all other rows.

I have this code in place
Code:
DataTable dt = u.GetIsseDetails_ByUserCustomerId(sUser,iCustomerId);
DataRow[] sdr = dt.Select("iStatusId = " + iStatusId);
dt.Rows.Clear();
for(int i =0; i<sdr.Length; i++)
{
	dt.Rows.Add(sdr[i]);
}

I then go through each row and show the value.
Code:
foreach (DataColumn dc in dt.Columns)
{
	Console.Write(dc.ColumnName);
	Console.Write('\t');
}
Console.WriteLine();

foreach (DataRow dr in dt.Rows)
{
	foreach (DataColumn dc in dt.Columns)
	{
		Console.Write(Convert.ToString(dr[dc]));
		Console.Write('\t');
	}
	Console.WriteLine();
}

When I do that I get blank values, but the row counts = the number of rows I expect after I filter. Am I missing a step?

Thanks
 
Hi,

I think you're making it more complicated that it needs to be... why not just remove the unwanted rows and then you'll be left with only the wanted rows?

Code:
DataTable dt = u.GetIsseDetails_ByUserCustomerId(sUser, iCustomerId);

// Get Unwanted Rows
DataRows[] unwantedRows = dt.Select(string.Format("iStatusId = {0}", iStatusId)

// Remove Unwanted Rows
foreach (DataRow unwantedRow in unwantedRows)
{
  dt.Remove(row);
}
This should only remove the unwanted rows and then the DataTable (dt) will be left containing only the wanted rows.

Also it seems your values are blank because you've called the dt.Rows.Clear() before you tried to add the rows back to the DataTable.

Hope this is of some help... [smile]
 
After looking at it, that will not work. I am passing in values that I do want to keep. I am not able to do a "not equal" in this dt.select statement for some reason, I have tried '!=' and '<>' but they do not work. What i am trying to avoid is having to write SQL based on criteria passed it. I figured if I just get all the rows and then filter via C# code, I am better off. Am I really better off?

Thanks
 
I think it depends on what you're application does...

The company I work for take the approach that data being returned from a database might as well be filtered server side to make the client have to do less work.

If it was me, I would filter it server side by passing filter parameters to a SQL Stored Procedure then return the wanted results set to the client.

[smile]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top