Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I'm working with a gridview, so i can show data from my three lists. I get it to show the title, but the date doesn't appear! Check the if statement.

            using (SPSite site = new SPSite("http://knowitintranet:9797/sites/News"))
            {
                SPWeb web = SPContext.Current.Web;

                if (DropDownListNewsFeed.SelectedItem.Value == "All")
                {
                    SPSiteDataQuery query = new SPSiteDataQuery();

                    query.Lists = "<Lists>" +
                  "<List ID=" + web.Lists.TryGetList("Staff News").ID.ToString() + " />" +
                  "<List ID=" + web.Lists.TryGetList("Company News").ID.ToString() + " />" +
                  "<List ID=" + web.Lists.TryGetList("Management News").ID.ToString() + " />" +
                  "</Lists>";


                    query.ViewFields = "<FieldRef Name=\"Title\" />" +
                        "<FieldRef Name=\"Date\" />";
                    query.Query = "<OrderBy><FieldRef Name='Modified' Ascending='FALSE'></FieldRef></OrderBy>";
                    query.Webs = "<Webs Scope=\"SiteCollection\" />";

                    query.RowLimit = 10;

                    DataTable dt = web.GetSiteData(query);
                    DataView dv = new DataView(dt);

                    GridViewNewsFeed.AutoGenerateColumns = false;
                    GridViewNewsFeed.DataSource = dv;
                    GridViewNewsFeed.DataBind();

                    GridViewNewsFeed.AllowSorting = true;
                    GridViewNewsFeed.HeaderStyle.Font.Bold = true;
                }
                else
                {
                    SPList list = web.Lists.TryGetList(DropDownListNewsFeed.SelectedItem.Value);

                    SPQuery query = new SPQuery();
                    query.Query = "<OrderBy><FieldRef Name='Modified' Ascending='FALSE' /></OrderBy>";
                    query.RowLimit = 10;

                    SPListItemCollection items = list.GetItems(query);

                    RepeaterNewsFeed.DataSource = items;
                    RepeaterNewsFeed.DataBind();
                }
            }

My asp code:

     <asp:GridView ID="GridViewNewsFeed" runat="server" BackColor="White" 
        BorderColor="White" BorderStyle="Ridge" BorderWidth="2px" CellPadding="3" 
        CellSpacing="1" EnableModelValidation="True" GridLines="None">

        <FooterStyle BackColor="#C6C3C6" ForeColor="Black" />
        <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#E7E7FF" />
        <PagerStyle BackColor="#C6C3C6" ForeColor="Black" HorizontalAlign="Right" />
        <RowStyle BackColor="#DEDFDE" ForeColor="Black" />
        <SelectedRowStyle BackColor="#9471DE" Font-Bold="True" ForeColor="White" />

        <Columns>
            <asp:BoundField DataField="Title" HeaderText="Title" ReadOnly="False"/>
            <asp:BoundField DataField="Date" HeaderText="Date" DataFormatString="{0:d}" ReadOnly="True"/>
        </Columns>
    </asp:GridView>

Here is a image of what my problem is.

I was googling around, and i found this site. But still don't getting it right.. Any suggestions?

share|improve this question
    
Do you get the date field in the datatable? Also, are you sure that the internal name of the field is Date. –  Nadeem Yousuf Jan 30 '13 at 14:26
add comment

1 Answer 1

It seems that the internal name of the Date field is something different. Try finding that and replace the Date in following line with the correct internal name:

"<FieldRef Name=\"Date\" />";

Also, use Nullable=\"TRUE\ setting the Nullable attribute to TRUE causes an empty value to be returned for lists that do not include the Date column. The default is to skip a list that does not include the column.

share|improve this answer
    
Hm, isn't that the "column name" in this case it's Date. Or is it maybe Modified i should have there? –  Kristian Salov Jan 30 '13 at 14:38
    
For a minute, change Date with Modified and see if the datatable is populated. The Date field may have a display name of Date but can have a different internal name. –  Nadeem Yousuf Jan 30 '13 at 14:42
    
with query.ViewFields = "<FieldRef Name=\"Title\" /><FieldRef Name=\"Modified\" Nullable=\"TRUE\"/>"; i don't even get out title, but the exception is the same.. –  Kristian Salov Jan 30 '13 at 14:49
    
Did you change the DataField to Modified in the markup? –  Nadeem Yousuf Jan 30 '13 at 15:10
    
I've tried <asp:BoundField DataField="Modified" HtmlEncode="False" DataFormatString = "{0:D}" HeaderText="Date" ReadOnly="True" /> but no output. Same exception. –  Kristian Salov Jan 30 '13 at 15:25
show 4 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.