如何删除DataGrid中记录asp.net,如果条件满足

编程入门 行业动态 更新时间:2024-10-22 05:05:40
本文介绍了如何删除DataGrid中记录asp,如果条件满足的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个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,如果条件满足

本文发布于:2023-10-24 14:01:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1524121.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:条件   DataGrid   asp   net

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!