To work with SPGridview you need to register
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
Now we can start working with SPGridView. First will add SPGridView control on our aspx page.
<SharePoint:SPGridView runat="server" ID="SPGridView1"
AutoGenerateColums="false" PageSize="300" AllowPaging="true" AllowFiltering="true"
AllowSorting="true" AutoGenerateColumns="false"> </SharePoint:SPGridView>
<SharePoint:SPGridViewPager ID="SPGridViewPager1" runat="server" GridViewId="SPGridView1"></SharePoint:SPGridViewPager>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" />
You can see that I have added three control SPgridView, SPGridViewPager, and ObjectDatasource.
Mostly ppl don’t use objectdatasource in context of SPGridview but there may be a scenario that you need to use custom data source. Let me explain some part for you , suppose u need to get all items from Sharepoint list and then u alter some field and then need to show in spridview in this situation filtering and sorting will not work automatic and u will suffer lots, so in that situation we need to use this ObjectDataSource.
Now you need to write code on your vb file.
First we need to write code either on page load or CreateChildControls event. I am going to write on CreateChildControls so no need to handle postback.
Protected Overrides Sub CreateChildControls()
Dim obj As New Foobar()
ObjectDataSource1.TypeName = obj.GetType().AssemblyQualifiedName
BuildGridColumns()
ObjectDataSource1.SelectMethod = "getall"
Me.SPGridView1.DataSourceID = "ObjectDataSource1"
Me.SPGridView1.DataBind()
End Sub
In this i have create one object of Foobar class so we can give ObjectDataSource TypeName.
Next line for assigning TypeName of object. That means which type of data this ObjectDataSource going to store, since we are going to store Foobar type so I am assigning foobar assembly Qualified name.
Now next line is for creating columns in SPGRidview. So I have created one function that will create columns in SPGRidview.
Private Sub BuildGridColumns()
Me.SPGridView1.Columns.Clear()
Dim gridColumn3 As New SPBoundField With { _
.DataField = "SNo", _
.HeaderText = "S/No", _
.SortExpression = "SNo" _
}
Me.SPGridView1.Columns.Add(gridColumn3)
Dim gridColumn4a As New SPBoundField With { _
.DataField = "Title", _
.HeaderText = "File Ref No", _
.SortExpression = "Title" _
}
Me.SPGridView1.Columns.Add(gridColumn4a)
Me.SPGridView1.FilterDataFields = "SNo,Title"
Me.SPGridView1.FilteredDataSourcePropertyName = "FilterExpression"
Me.SPGridView1.FilteredDataSourcePropertyFormat = "{1} Like'{0}'"
End Sub
Now this function is creating two columns, and then adding to SPGridView.
After that assigning Filter Data Fields so that filtering can be done. For that simply assigning 3 properties FilterDataFields, FilteredDataSourcePropertyName, .FilteredDataSourcePropertyFormat.
Note:-Here no need to change anything in your code for filtering just need to give same value and it will work(Last 2 line).
Come back to CreateChildControls sub. Next line is SelectMethod=”getalll”. Fon now i am skiping this , will explain after explaing few more things
.
Next line is assigning DatasourceId of SPGirdview. For this putting objcetDatasource id as string.
After that we bind SPGridview to data source.
Now come to the “getall” this is a function of foobar class. Function will return datasource as Datatable. Now check our class Foobar.
Public Class Foobar
Private _SNo As String
Public Property SNo() As String
Get
Return _SNo
End Get
Set(ByVal value As String)
_SNo = value
End Set
End Property
Private _Title As String
Public Property Title() As String
Get
Return _Title
End Get
Set(ByVal value As String)
_Title = value
End Set
End Property
Public Function getall() As DataTable
Dim _site As SPSite = SPContext.Current.Site
Dim result As New System.Collections.Generic.List(Of Foobar)
Using _web As SPWeb = _site.OpenWeb("")
Dim _list As SPList = _web.Lists("")
Dim spList As SPListItemCollection = _list.Items
For Each _it In spList
Dim _new As New Foobar
_new.SNo = _it("ID")
_new.Title = _it("Title")
result.Add(_new)
Next
End Using
Dim datatable As DataTable = BuildDataTable(Of Foobar)(result)
Dim ListAsDataView As DataView = datatable.DefaultView
Return datatable
End Function
Public Function BuildDataTable(Of T)(ByVal lst As System.Collections.Generic.List(Of T)) As DataTable
'create DataTable Structure
Dim tbl As DataTable = CreateTable(Of T)()
Dim entType As Type = GetType(T)
Dim properties As PropertyDescriptorCollection = TypeDescriptor.GetProperties(entType)
'get the list item and add into the list
For Each item As T In lst
Dim row As DataRow = tbl.NewRow()
For Each prop As PropertyDescriptor In properties
row(prop.Name) = prop.GetValue(item)
Next
tbl.Rows.Add(row)
Next
Return tbl
End Function
Private Function CreateTable(Of T)() As DataTable
'T –> ClassName
Dim entType As Type = GetType(T)
'set the datatable name as class name
Dim tbl As New DataTable(entType.Name)
'get the property list
Dim properties As PropertyDescriptorCollection = TypeDescriptor.GetProperties(entType)
For Each prop As PropertyDescriptor In properties
'add property as column
tbl.Columns.Add(prop.Name, prop.PropertyType)
Next
Return tbl
End Function
If you will go through this class you will find there are 3 functions and two properties you can add as much as property you want. “getall” function is responsible for fetching data from SharePoint list and assigning to Foobar class.
After that inside “getall” we are calling BuildDataTable() function. In this function we are passing list collection(result) of Foobar and Type of list that is Foobar (Dim datatable As DataTable = BuildDataTable(Of Foobar)(result))
In this function you can see that we are calling one other function(CreateTable) that create columns for Table. Once table and columns has created we can assign value to each columns.
After that will have one table that will keep information of one list.
Why we are using table and why we are sending all data inform of table?
If you are not able to find this answer post here I will try to solve once again
Thanks and Regards,
Shailendra Kumar Singh
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
Now we can start working with SPGridView. First will add SPGridView control on our aspx page.
<SharePoint:SPGridView runat="server" ID="SPGridView1"
AutoGenerateColums="false" PageSize="300" AllowPaging="true" AllowFiltering="true"
AllowSorting="true" AutoGenerateColumns="false"> </SharePoint:SPGridView>
<SharePoint:SPGridViewPager ID="SPGridViewPager1" runat="server" GridViewId="SPGridView1"></SharePoint:SPGridViewPager>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" />
You can see that I have added three control SPgridView, SPGridViewPager, and ObjectDatasource.
Mostly ppl don’t use objectdatasource in context of SPGridview but there may be a scenario that you need to use custom data source. Let me explain some part for you , suppose u need to get all items from Sharepoint list and then u alter some field and then need to show in spridview in this situation filtering and sorting will not work automatic and u will suffer lots, so in that situation we need to use this ObjectDataSource.
Now you need to write code on your vb file.
First we need to write code either on page load or CreateChildControls event. I am going to write on CreateChildControls so no need to handle postback.
Protected Overrides Sub CreateChildControls()
Dim obj As New Foobar()
ObjectDataSource1.TypeName = obj.GetType().AssemblyQualifiedName
BuildGridColumns()
ObjectDataSource1.SelectMethod = "getall"
Me.SPGridView1.DataSourceID = "ObjectDataSource1"
Me.SPGridView1.DataBind()
End Sub
In this i have create one object of Foobar class so we can give ObjectDataSource TypeName.
Next line for assigning TypeName of object. That means which type of data this ObjectDataSource going to store, since we are going to store Foobar type so I am assigning foobar assembly Qualified name.
Now next line is for creating columns in SPGRidview. So I have created one function that will create columns in SPGRidview.
Private Sub BuildGridColumns()
Me.SPGridView1.Columns.Clear()
Dim gridColumn3 As New SPBoundField With { _
.DataField = "SNo", _
.HeaderText = "S/No", _
.SortExpression = "SNo" _
}
Me.SPGridView1.Columns.Add(gridColumn3)
Dim gridColumn4a As New SPBoundField With { _
.DataField = "Title", _
.HeaderText = "File Ref No", _
.SortExpression = "Title" _
}
Me.SPGridView1.Columns.Add(gridColumn4a)
Me.SPGridView1.FilterDataFields = "SNo,Title"
Me.SPGridView1.FilteredDataSourcePropertyName = "FilterExpression"
Me.SPGridView1.FilteredDataSourcePropertyFormat = "{1} Like'{0}'"
End Sub
Now this function is creating two columns, and then adding to SPGridView.
After that assigning Filter Data Fields so that filtering can be done. For that simply assigning 3 properties FilterDataFields, FilteredDataSourcePropertyName, .FilteredDataSourcePropertyFormat.
Note:-Here no need to change anything in your code for filtering just need to give same value and it will work(Last 2 line).
Come back to CreateChildControls sub. Next line is SelectMethod=”getalll”. Fon now i am skiping this , will explain after explaing few more things
.
Next line is assigning DatasourceId of SPGirdview. For this putting objcetDatasource id as string.
After that we bind SPGridview to data source.
Now come to the “getall” this is a function of foobar class. Function will return datasource as Datatable. Now check our class Foobar.
Public Class Foobar
Private _SNo As String
Public Property SNo() As String
Get
Return _SNo
End Get
Set(ByVal value As String)
_SNo = value
End Set
End Property
Private _Title As String
Public Property Title() As String
Get
Return _Title
End Get
Set(ByVal value As String)
_Title = value
End Set
End Property
Public Function getall() As DataTable
Dim _site As SPSite = SPContext.Current.Site
Dim result As New System.Collections.Generic.List(Of Foobar)
Using _web As SPWeb = _site.OpenWeb("")
Dim _list As SPList = _web.Lists("")
Dim spList As SPListItemCollection = _list.Items
For Each _it In spList
Dim _new As New Foobar
_new.SNo = _it("ID")
_new.Title = _it("Title")
result.Add(_new)
Next
End Using
Dim datatable As DataTable = BuildDataTable(Of Foobar)(result)
Dim ListAsDataView As DataView = datatable.DefaultView
Return datatable
End Function
Public Function BuildDataTable(Of T)(ByVal lst As System.Collections.Generic.List(Of T)) As DataTable
'create DataTable Structure
Dim tbl As DataTable = CreateTable(Of T)()
Dim entType As Type = GetType(T)
Dim properties As PropertyDescriptorCollection = TypeDescriptor.GetProperties(entType)
'get the list item and add into the list
For Each item As T In lst
Dim row As DataRow = tbl.NewRow()
For Each prop As PropertyDescriptor In properties
row(prop.Name) = prop.GetValue(item)
Next
tbl.Rows.Add(row)
Next
Return tbl
End Function
Private Function CreateTable(Of T)() As DataTable
'T –> ClassName
Dim entType As Type = GetType(T)
'set the datatable name as class name
Dim tbl As New DataTable(entType.Name)
'get the property list
Dim properties As PropertyDescriptorCollection = TypeDescriptor.GetProperties(entType)
For Each prop As PropertyDescriptor In properties
'add property as column
tbl.Columns.Add(prop.Name, prop.PropertyType)
Next
Return tbl
End Function
If you will go through this class you will find there are 3 functions and two properties you can add as much as property you want. “getall” function is responsible for fetching data from SharePoint list and assigning to Foobar class.
After that inside “getall” we are calling BuildDataTable() function. In this function we are passing list collection(result) of Foobar and Type of list that is Foobar (Dim datatable As DataTable = BuildDataTable(Of Foobar)(result))
In this function you can see that we are calling one other function(CreateTable) that create columns for Table. Once table and columns has created we can assign value to each columns.
After that will have one table that will keep information of one list.
Why we are using table and why we are sending all data inform of table?
If you are not able to find this answer post here I will try to solve once again
Thanks and Regards,
Shailendra Kumar Singh
No comments:
Post a Comment