none
SPGridView Example for SharePoint 2010?

    Question

  • I have one list contains 5 columns and multiple rows.

    One of the column is of type Hyperlink.

    I want to show my Sample list into SPGridView and if someone click on the Hyperlink item, it should open in new window.

    Any help?


    Thanks & Regards, Naimish Pandya [MCTS, MCPD, MVP] http://naimishpandya.wordpress.com/
    Monday, October 03, 2011 4:11 PM

Answers

  • Here is an example.

    In the markup page define the SPGridView like this :-

     

    <SharePoint:SPGridView runat="server" ID="spgvTrainerTrainingLists" AutoGenerateColumns="false"
                                    DataKeyNames="TrainingId" OnRowDataBound="spgvTrainerTrainingLists_RowDataBound"
                                    DataSourceID="linqDsTrainingLists" PageSize="20" AllowPaging="true" AllowSorting="true"
                                    AllowFiltering="true" FilterDataFields=",,,Type,LastSession,NextSession" FilteredDataSourcePropertyName="Where"
                                    FilteredDataSourcePropertyFormat='{1} == "{0}"'>
                                    <Columns>
                                        <asp:TemplateField HeaderText="No.">
                                            <ItemTemplate>
                                                <%# Container.DataItemIndex + 1 %>
                                            </ItemTemplate>
                                        </asp:TemplateField>
                                        <SharePoint:SPBoundField HeaderText="Ref #" DataField="RefNo" SortExpression="RefNo" />
                                        <asp:TemplateField HeaderText="Training" SortExpression="Title">
                                            <ItemTemplate>
                                                <asp:HyperLink ID="hlTrainingDetail" runat="server" Text='<%# Eval("Title") %>' />
                                            </ItemTemplate>
                                        </asp:TemplateField>
                                        <SharePoint:SPBoundField HeaderText="Type" DataField="Type" SortExpression="Type" />
                                        <SharePoint:SPBoundField HeaderText="Last Session" SortExpression="LastSession" DataField="LastSession" />
                                        <SharePoint:SPBoundField HeaderText="Next Session" SortExpression="NextSession" DataField="NextSession" />
                                    </Columns>
                                    <EmptyDataTemplate>
                                        No training yet.</EmptyDataTemplate>
                                </SharePoint:SPGridView>
                                <SharePoint:SPGridViewPager ID="spgvPagerTrainingLists" runat="server" GridViewId="spgvTrainerTrainingLists" />
    

     

    Then in the code behind, in row databound set the value for the HyperLink as such:-

     

     protected void spgvTrainerTrainingLists_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
            {
                if (e.Row.RowType == System.Web.UI.WebControls.DataControlRowType.DataRow)
                {
                    HyperLink hl = (HyperLink)e.Row.FindControl("hlTrainingDetail");
                    string webUrl = SPContext.Current.Web.Url;
                    hl.NavigateUrl = webUrl + "/_layouts/TrainingAdministration/TrainingDetail.aspx?trainingId=" + ((GridView)sender).DataKeys[e.Row.RowIndex].Value.ToString();
                }
                else if (e.Row.RowType == DataControlRowType.Header)
                {
                    if (!string.IsNullOrEmpty(spgvTrainerTrainingLists.FilterFieldName))
                    {
                        for (int i = 0; i < spgvTrainerTrainingLists.Columns.Count; i++)
                        {
                            DataControlField field = spgvTrainerTrainingLists.Columns[i];
    
                            if (field.SortExpression == spgvTrainerTrainingLists.FilterFieldName)
                            {
                                Image filterIcon = new Image();
                                filterIcon.ImageUrl = "/_layouts/images/filter.gif";
                                filterIcon.Style[HtmlTextWriterStyle.MarginLeft] = "2px";
    
                                Literal headerText = new Literal();
                                headerText.Text = field.HeaderText;
    
                                PlaceHolder panel = new PlaceHolder();
                                panel.Controls.Add(headerText);
                                panel.Controls.Add(filterIcon);
    
                                e.Row.Cells[i].Controls[0].Controls.Add(panel);
    
                                break;
                            }
                        }
                    }
                }
            }
    

     

    If you wants OOTB functions (Sorting,Paging,Filtering) for the SPGridView you can choose either of these two options:-

    a.SPGridView + ObjectDataSource + DataTabel

    b.SPGridView + LinqDataSource + Generic class, List<T>

     

    Hope this helps

     


    agamand
    Tuesday, October 04, 2011 6:07 AM

All replies

  • Thanks but not much help.

    There isn't any book/article/blog which can explain SPGridView of SharePoint 2010 properly?? :(


    Thanks & Regards, Naimish Pandya [MCTS, MCPD, MVP] http://naimishpandya.wordpress.com/
    Tuesday, October 04, 2011 5:51 AM
  • Here is an example.

    In the markup page define the SPGridView like this :-

     

    <SharePoint:SPGridView runat="server" ID="spgvTrainerTrainingLists" AutoGenerateColumns="false"
                                    DataKeyNames="TrainingId" OnRowDataBound="spgvTrainerTrainingLists_RowDataBound"
                                    DataSourceID="linqDsTrainingLists" PageSize="20" AllowPaging="true" AllowSorting="true"
                                    AllowFiltering="true" FilterDataFields=",,,Type,LastSession,NextSession" FilteredDataSourcePropertyName="Where"
                                    FilteredDataSourcePropertyFormat='{1} == "{0}"'>
                                    <Columns>
                                        <asp:TemplateField HeaderText="No.">
                                            <ItemTemplate>
                                                <%# Container.DataItemIndex + 1 %>
                                            </ItemTemplate>
                                        </asp:TemplateField>
                                        <SharePoint:SPBoundField HeaderText="Ref #" DataField="RefNo" SortExpression="RefNo" />
                                        <asp:TemplateField HeaderText="Training" SortExpression="Title">
                                            <ItemTemplate>
                                                <asp:HyperLink ID="hlTrainingDetail" runat="server" Text='<%# Eval("Title") %>' />
                                            </ItemTemplate>
                                        </asp:TemplateField>
                                        <SharePoint:SPBoundField HeaderText="Type" DataField="Type" SortExpression="Type" />
                                        <SharePoint:SPBoundField HeaderText="Last Session" SortExpression="LastSession" DataField="LastSession" />
                                        <SharePoint:SPBoundField HeaderText="Next Session" SortExpression="NextSession" DataField="NextSession" />
                                    </Columns>
                                    <EmptyDataTemplate>
                                        No training yet.</EmptyDataTemplate>
                                </SharePoint:SPGridView>
                                <SharePoint:SPGridViewPager ID="spgvPagerTrainingLists" runat="server" GridViewId="spgvTrainerTrainingLists" />
    

     

    Then in the code behind, in row databound set the value for the HyperLink as such:-

     

     protected void spgvTrainerTrainingLists_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
            {
                if (e.Row.RowType == System.Web.UI.WebControls.DataControlRowType.DataRow)
                {
                    HyperLink hl = (HyperLink)e.Row.FindControl("hlTrainingDetail");
                    string webUrl = SPContext.Current.Web.Url;
                    hl.NavigateUrl = webUrl + "/_layouts/TrainingAdministration/TrainingDetail.aspx?trainingId=" + ((GridView)sender).DataKeys[e.Row.RowIndex].Value.ToString();
                }
                else if (e.Row.RowType == DataControlRowType.Header)
                {
                    if (!string.IsNullOrEmpty(spgvTrainerTrainingLists.FilterFieldName))
                    {
                        for (int i = 0; i < spgvTrainerTrainingLists.Columns.Count; i++)
                        {
                            DataControlField field = spgvTrainerTrainingLists.Columns[i];
    
                            if (field.SortExpression == spgvTrainerTrainingLists.FilterFieldName)
                            {
                                Image filterIcon = new Image();
                                filterIcon.ImageUrl = "/_layouts/images/filter.gif";
                                filterIcon.Style[HtmlTextWriterStyle.MarginLeft] = "2px";
    
                                Literal headerText = new Literal();
                                headerText.Text = field.HeaderText;
    
                                PlaceHolder panel = new PlaceHolder();
                                panel.Controls.Add(headerText);
                                panel.Controls.Add(filterIcon);
    
                                e.Row.Cells[i].Controls[0].Controls.Add(panel);
    
                                break;
                            }
                        }
                    }
                }
            }
    

     

    If you wants OOTB functions (Sorting,Paging,Filtering) for the SPGridView you can choose either of these two options:-

    a.SPGridView + ObjectDataSource + DataTabel

    b.SPGridView + LinqDataSource + Generic class, List<T>

     

    Hope this helps

     


    agamand
    Tuesday, October 04, 2011 6:07 AM
  • Hi,

    Go thru this thread

    http://social.msdn.microsoft.com/forums/en-US/sharepointdevelopment/thread/305d1663-e2e5-41ca-983c-b94200d1df96

    I think you might need javascript to acheive this kindof stuff

    With javascript, add the control.attribute.add("script1","window.open("Url.aspx?idofthecontrol="+e.Item.Cell[x].Text);

    Hope this gives you a reference, make sure you add this in the bounding of the grid.

    First go thru the thread

    Hope this helps :)


    "The Only Way To Get Smarter Is By Playing A Smarter Opponent"
    Tuesday, October 04, 2011 7:13 AM
  • Here is an example.

    In the markup page define the SPGridView like this :-

     

    <SharePoint:SPGridView runat="server" ID="spgvTrainerTrainingLists" AutoGenerateColumns="false"
                                    DataKeyNames="TrainingId" OnRowDataBound="spgvTrainerTrainingLists_RowDataBound"
                                    DataSourceID="linqDsTrainingLists" PageSize="20" AllowPaging="true" AllowSorting="true"
                                    AllowFiltering="true" FilterDataFields=",,,Type,LastSession,NextSession" FilteredDataSourcePropertyName="Where"
                                    FilteredDataSourcePropertyFormat='{1} == "{0}"'>
                                    <Columns>
                                        <asp:TemplateField HeaderText="No.">
                                            <ItemTemplate>
                                                <%# Container.DataItemIndex + 1 %>
                                            </ItemTemplate>
                                        </asp:TemplateField>
                                        <SharePoint:SPBoundField HeaderText="Ref #" DataField="RefNo" SortExpression="RefNo" />
                                        <asp:TemplateField HeaderText="Training" SortExpression="Title">
                                            <ItemTemplate>
                                                <asp:HyperLink ID="hlTrainingDetail" runat="server" Text='<%# Eval("Title") %>' />
                                            </ItemTemplate>
                                        </asp:TemplateField>
                                        <SharePoint:SPBoundField HeaderText="Type" DataField="Type" SortExpression="Type" />
                                        <SharePoint:SPBoundField HeaderText="Last Session" SortExpression="LastSession" DataField="LastSession" />
                                        <SharePoint:SPBoundField HeaderText="Next Session" SortExpression="NextSession" DataField="NextSession" />
                                    </Columns>
                                    <EmptyDataTemplate>
                                        No training yet.</EmptyDataTemplate>
                                </SharePoint:SPGridView>
                                <SharePoint:SPGridViewPager ID="spgvPagerTrainingLists" runat="server" GridViewId="spgvTrainerTrainingLists" />
    

     

    Then in the code behind, in row databound set the value for the HyperLink as such:-

     

     protected void spgvTrainerTrainingLists_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
            {
                if (e.Row.RowType == System.Web.UI.WebControls.DataControlRowType.DataRow)
                {
                    HyperLink hl = (HyperLink)e.Row.FindControl("hlTrainingDetail");
                    string webUrl = SPContext.Current.Web.Url;
                    hl.NavigateUrl = webUrl + "/_layouts/TrainingAdministration/TrainingDetail.aspx?trainingId=" + ((GridView)sender).DataKeys[e.Row.RowIndex].Value.ToString();
                }
                else if (e.Row.RowType == DataControlRowType.Header)
                {
                    if (!string.IsNullOrEmpty(spgvTrainerTrainingLists.FilterFieldName))
                    {
                        for (int i = 0; i < spgvTrainerTrainingLists.Columns.Count; i++)
                        {
                            DataControlField field = spgvTrainerTrainingLists.Columns[i];
    
                            if (field.SortExpression == spgvTrainerTrainingLists.FilterFieldName)
                            {
                                Image filterIcon = new Image();
                                filterIcon.ImageUrl = "/_layouts/images/filter.gif";
                                filterIcon.Style[HtmlTextWriterStyle.MarginLeft] = "2px";
    
                                Literal headerText = new Literal();
                                headerText.Text = field.HeaderText;
    
                                PlaceHolder panel = new PlaceHolder();
                                panel.Controls.Add(headerText);
                                panel.Controls.Add(filterIcon);
    
                                e.Row.Cells[i].Controls[0].Controls.Add(panel);
    
                                break;
                            }
                        }
                    }
                }
            }
    

     

    If you wants OOTB functions (Sorting,Paging,Filtering) for the SPGridView you can choose either of these two options:-

    a.SPGridView + ObjectDataSource + DataTabel

    b.SPGridView + LinqDataSource + Generic class, List<T>

     

    Hope this helps

     


    agamand

    When I am adding a web part on my SharePoint site, it's giving below Error :

     

     

    Error
    
    An unexpected error has occurred.
    
    Web Parts Maintenance Page: If you have permission, you can use this page to temporarily close Web Parts or remove personal settings. For more information, contact your site administrator.
    
    Troubleshoot issues with Microsoft SharePoint Foundation.
    
    Correlation ID: e94f1ea2-59df-43b1-bb5b-d1852e95b1b2

     

    May be it's because I don't have the following structure on my site?

     

    HyperLink hl = (HyperLink)e.Row.FindControl("hlTrainingDetail");
                    string webUrl = SPContext.Current.Web.Url;
                    hl.NavigateUrl = webUrl + "/_layouts/TrainingAdministration/TrainingDetail.aspx?trainingId=" + ((GridView)sender).DataKeys[e.Row.RowIndex].Value.ToString();

    Could you please provide some background to setup the above fields in my SharePoint site?

    Thanks

     

     



    Thanks & Regards, Naimish Pandya [MCTS, MCPD, MVP] http://naimishpandya.wordpress.com/
    Tuesday, October 04, 2011 11:28 AM
  • I used SPGridView in Custom Visual WebPart and Application pages. I have no experience using it anywhere else.
    agamand
    Wednesday, October 05, 2011 3:00 AM