我有一个DataGrid声明像这样ASCX文件:
I have a datagrid declare like this in ascx file:
<asp:datagrid id="dgCompanies" Width="100%" AllowSorting="True" DataKeyField="companyId" AutoGenerateColumns="False" AllowPaging="True" AllowCustomPaging="True" OnPageIndexChanged="dgCompanies_Paging" Runat="server" onprerender="dgCompanies_PreRender" >基本上,数据网格搜索结果一些如何像这样codebehind:
basically, datagrid is the search result some how like this in codebehind:
// retrieve the matching company records IDataReader rdr = Syntegra.Manufacturing.WMCCM.Companies.Companies.ListCompanies(dgCompanies.CurrentPageIndex, pageSize, CompanyList, CompanyNameStartsWith, ProcessSqlClause, SkillSqlClause, LocationClause, KeywordSqlClause, User, Status, SearchPortalId, false, sortColumn, sortDirection); // highlight the sorted column highlightSortColumn(); if (rdr.Read()) { // calculate page details _count = (int) rdr["companyCount"]; this.dgCompanies.VirtualItemCount = _count; // move onto the next resultset rdr.NextResult(); // bind the data to the datagrid dgCompanies.PageSize = pageSize; dgCompanies.DataSource = rdr; dgCompanies.DataBind();我将需要检查是否companyId不等于一些值,然后我需要从搜索结果中删除的记录,这意味着从dgCompanies DataGrid中删除它。我真的不知道我能做到这一点,任何人都可以请给我一些帮助吗?
I will need to check if companyId is not equal to some values, then I need to remove that record from the search result, meaning remove it from that dgCompanies datagrid. I really have no idea I could do that, could anyone please give me some help here?
推荐答案良好做法是根据标准要求来填充DataReader的。
Good practice is to populate datareader based on required criteria.
如果你想过滤来自DataReader的记录,你可以将其加载到数据表。
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 cnpanyId from companies cn INNER JOIN UserCompany uc ON ucpanyid = cnpanyid 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; } } }i.stack.imgur/rPy3A.png
更多推荐
如何删除DataGrid中记录asp.net,如果条件满足
发布评论