Normally we bind SPgridView with datatable but
SPGridView becomes null on page load after postback ,so to maintain its data "ObjectDatasource"
is used.
You can also apply filtering by sending field names in "FilterDataFields"
property on which fields you want filter.
It will work as DataFormWebpart.
Add below code in page init function :
odsDataSource.ID = "odsDataSource";
odsDataSource.SelectMethod = "GetDataTable";
var assemblyQualifiedName = GetType().AssemblyQualifiedName;
if (assemblyQualifiedName != null) odsDataSource.TypeName = assemblyQualifiedName;
odsDataSource.ObjectCreating += odsDataSource_ObjectCreating;
odsDataSource.ObjectDisposing += odsDataSource_ObjectDisposing;
odsDataSource.Filtering += GridViewDocumentMaster_Filtering;
Controls.Add(odsDataSource);
GridViewDocumentMaster.PagerTemplate = null;
GridViewDocumentMaster.AllowFiltering = true;
GridViewDocumentMaster.FilterDataFields = ",Id,DocumentTitle,DocumentNumber,,,";
GridViewDocumentMaster.FilteredDataSourcePropertyName = "FilterExpression";
GridViewDocumentMaster.FilteredDataSourcePropertyFormat = "{1} = '{0}'";
GridViewDocumentMaster.EnableViewState = false;
Then Add below functions Change Gridview Name
/// <summary>
/// Handles the Filtering event of the SPGridViewMappedDocs control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.Web.UI.WebControls.ObjectDataSourceFilteringEventArgs"/> instance containing the event data.</param>
/// <remarks></remarks>
private void GridViewDocumentMaster_Filtering(object sender, ObjectDataSourceFilteringEventArgs e)
{
ViewState["FilterExpression"] = ((ObjectDataSourceView)sender).FilterExpression;
}
/// <summary>
/// Handles the ObjectCreating event of the odsDataSource control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.Web.UI.WebControls.ObjectDataSourceEventArgs"/> instance containing the event data.</param>
/// <remarks></remarks>
private void odsDataSource1_ObjectCreating(object sender, ObjectDataSourceEventArgs e)
{
e.ObjectInstance = this;
}
/// <summary>

Join the conversation! Your thoughts help the community grow.