如何过滤数据集绑定的gridview中的数据? [英] how do I filter data from gridview which is binded by dataset?

查看:50
本文介绍了如何过滤数据集绑定的gridview中的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想过滤数据集中的数据,并在gridview中显示数据。

我试过了



  public  DataSet Ds 
{
get
{
object temp = ViewState [ Ds ];
return temp == null null :temp as DataSet;
}
set
{
ViewState [ Ds] = value ;
}
}

public void gridCustBind( int pageIndex)
{
try
{
// 如果数据库连接关闭,则打开数据库连接...
if (connection.State == ConnectionState.Closed)
connection.Open();
command = new SqlCommand();
command.CommandText = sp_Get_CustInfoSerach;
command.CommandType = CommandType.StoredProcedure;
command.Connection = connection;
command.Parameters.AddWithValue( @ PageIndex,pageIndex);
command.Parameters.AddWithValue( @ PageSize INT .Parse(ddlPaging.SelectedValue));
command.Parameters.Add( @ RecordCount,SqlDbType.Int, 4 );
command.Parameters [ @ RecordCount]。Direction = ParameterDirection.Output;

// SqlParameter outParam = command.Parameters.Add(@ bal,SqlDbType。 Float);
// outParam.Direction = ParameterDirection.Output;

command.Parameters.Add( @ bal,SqlDbType.Int, 4 );
command.Parameters [ @ bal]。Direction = ParameterDirection.Output;

SqlDataAdapter daAcc = new SqlDataAdapter(command);

daAcc.Fill(Ds);
}
catch (例外情况)
{
lblMessageCustSerach.Text = ex.Message;
lblMessageCustSerach.Visible = true ;
}
最后 // 关闭数据库连接,如果它已打开....
{
if (connection.State == ConnectionState.Open)
connection.Close();

}
}

受保护 void Page_Load( object sender,EventArgs e)
{
if (! IsPostBack == true
{

gridCustBind( 1 ) ;
if (gridCustomer.Rows.Count > 0
{
gridCustomer.DataSource = Ds;
gridCustomer.DataBind();

}

}
}



如果我喜欢这样,那么抛出错误:值不能是null。

参数名称:dataSet

daAcc.Fill(Ds); //在这一行

解决方案

1.在视图状态中缓存数据集是不行的。视图状态旨在用于缓存页面中的少量数据,例如来自页面控件的最后一个用户输入。



2.解决您的问题在我的下一篇文章(包括完整的源代码)中:高级ASPX GridView分页和数据实体 [ ^ ]

i want to filter the data in dataset and that data display in gridview.
I have tried with

public DataSet Ds
            {
                get
                {
                    object temp = ViewState["Ds"];
                    return temp == null ? null : temp as DataSet;
                }
                set
                {
                    ViewState["Ds"] = value;
                }
            }
    
            public void gridCustBind(int pageIndex)
            {
                try
                {
                    //open the db connection if it is closed...  
                    if (connection.State == ConnectionState.Closed)
                        connection.Open();
                    command = new SqlCommand();
                    command.CommandText = "sp_Get_CustInfoSerach";
                    command.CommandType = CommandType.StoredProcedure;
                    command.Connection = connection;
                    command.Parameters.AddWithValue("@PageIndex", pageIndex);
                    command.Parameters.AddWithValue("@PageSize", int.Parse(ddlPaging.SelectedValue));
                    command.Parameters.Add("@RecordCount", SqlDbType.Int, 4);
                    command.Parameters["@RecordCount"].Direction = ParameterDirection.Output;
    
                    //SqlParameter outParam = command.Parameters.Add("@bal", SqlDbType.Float);
                    //outParam.Direction = ParameterDirection.Output;
    
                    command.Parameters.Add("@bal", SqlDbType.Int, 4);
                    command.Parameters["@bal"].Direction = ParameterDirection.Output;
    
                    SqlDataAdapter daAcc = new SqlDataAdapter(command);
                   
                    daAcc.Fill(Ds);
                   }
                catch (Exception ex)
                {
                    lblMessageCustSerach.Text = ex.Message;
                    lblMessageCustSerach.Visible = true;
                }  
                        finally //Close db Connection if it is open....  
                {
                    if (connection.State == ConnectionState.Open)
                        connection.Close();
    
                }
            }        
    
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack == true)
                {
                   
                    gridCustBind(1);
                    if (gridCustomer.Rows.Count > 0)
                    {
                        gridCustomer.DataSource = Ds;
                        gridCustomer.DataBind();
                        
                    }
    
                }
            }


If I do like this then throwing error: Value cannot be null.
Parameter name: dataSet
daAcc.Fill(Ds); //at this line

解决方案

1.It is not OK to cache the data set inside your view state. The view state is designed to be used for caching small amount of data from the page, like last user inputs from the page controls.

2.A solution to your problem is in my next article (including full source code): Advanced ASPX GridView Pagination and Data Entities[^]


这篇关于如何过滤数据集绑定的gridview中的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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