I have a datagrid declare like this in ascx file:
Good practice is to populate datareader based on required criteria.
In case you want to filter records from datareader, you can load it into datatable
.
IDataReader rdr = Syntegra.Manufacturing.WMCCM.Companies.Companies.ListCompanies(dgCompanies.CurrentPageIndex, pageSize, CompanyList, CompanyNameStartsWith, ProcessSqlClause, SkillSqlClause, LocationClause, KeywordSqlClause, User, Status, SearchPortalId, false, sortColumn, sortDirection);
DataTable dt = new DataTable();
dt.Load(rdr);
rdr.Close();
for(int i = dt.Rows.Count-1; i >= 0; i--)
{
DataRow dr = dt.Rows[i];
int iCompanyId = Convert.ToInt(dr["CompanyId"]);
if (IsCompanyHavingChoiceYes(iCompanyId))
dr.Delete();
}
dt.AcceptChanges();
dgCompanies.PageSize = pageSize;
dgCompanies.DataSource = dt; //provide datatable as datasource
dgCompanies.DataBind();
Method to check from database:
private bool IsCompanyHavingChoiceYes(int iCompanyId)
{
string commandText = @"SELECT cn.companyId from companies cn
INNER JOIN UserCompany uc ON uc.companyid = cn.companyid
INNER JOIN Users u ON u.userId = uc.userId
INNER JOIN Choice c ON c.Email = u.Email And c.Choice = @Choice
WHERE cn.CompanyId = @CompanyId;";
string connectionString = ConfigurationSettings.AppSettings["connectionString"];
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlCommand command = new SqlCommand(commandText, connection);
command.Parameters.AddWithValue("@CompanyId", iCompanyId);
command.Parameters.AddWithValue("@Choice", "YES");
try
{
connection.Open();
using (SqlDataReader reader = command.ExecuteReader())
{
return reader.HasRows;
}
return false;
}
catch (Exception ex)
{
return false;
}
}
}
http://i.stack.imgur.com/rPy3A.png