如何删除DataGrid中记录asp.net,如果条件满足 [英] How remove records from datagrid in asp.net if condition satisfy

查看:92
本文介绍了如何删除DataGrid中记录asp.net,如果条件满足的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个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 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

这篇关于如何删除DataGrid中记录asp.net,如果条件满足的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆