在SharePoint Webpart中检索列表中的值到Gridview? [英] Retrieve the values from a list to Gridview in SharePoint Webpart?

查看:161
本文介绍了在SharePoint Webpart中检索列表中的值到Gridview?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为注册的列表,以下是列表中的列。

I have a List called Registration and the following are the columns of my list.

类型

员工姓名:人员或组


经理姓名:人员或组


名称:单行文本

程序名称:查找

状态:选择

先决条件:多行文本

Employee Name : Person or Group
Manager Name : Person or Group
Practice Name : Single line of text
Program Name : Lookup
Status : Choice
Prerequisite : Multiple lines of text

现在我创建了一个Web部件,它将所有这些值显示为网格视图
这里是我为 webpart.cs

And now i created a web part which will display all these values as a grid view here is the code which i have done for webpart.cs

protected void Page_Load(object sender, EventArgs e)
{
    gridViewManager.DataSource = GetData();
    gridViewManager.DataBind();
}

#region Try2
DataTable GetData()
{
    SPSite oSiteCollection = SPContext.Current.Site;
    SPWeb oWeb = oSiteCollection.OpenWeb();
    SPList oSPList = oWeb.Lists["Registration"];
    SPListItemCollection oSPListItemCollection = oSPList.Items;
    DataTable dt = new DataTable();            
    try
    {              
        dt.Columns.Add("Employee Name", typeof(String));
        dt.Columns.Add("Manager Name", typeof(String));
        dt.Columns.Add("Practice Name", typeof(String));
        dt.Columns.Add("Program Name", typeof(LookupField));
        //dt.Columns.Add("Program Name", typeof(String));
        dt.Columns.Add("Status", typeof(String));
        dt.Columns.Add("Prerequisite", typeof(String));               
        DataRow dataRow;                 
        foreach (SPListItem oSplistItem in oSPListItemCollection)
        {
            dataRow = dt.Rows.Add();
            dataRow["Employee Name"] = oSplistItem["Employee Name"].ToString();
            dataRow["Manager Name"] = oSplistItem["Manager Name"].ToString();
            dataRow["Practice Name"] = oSplistItem["Practice Name"].ToString();
            dataRow["Program Name"] = oSplistItem["Program Name"].ToString();
            dataRow["Status"] = oSplistItem["Status"].ToString();
            dataRow["Prerequisite"] = oSplistItem["Prerequisite"].ToString();
        }
        return dt;
    }
    catch (Exception ex)
    {
        System.Diagnostics.Debug.WriteLine("Managers Approval" + ex.Message.ToString());
        return dt;
    }

#endregion Try2
}

这里是用户控制代码的代码:

Here is the code for usercontrol code:

<SharePoint:SPGridView runat="server" ID="gridViewManager" AutoGenerateColumns="false">
    <Columns>
        <asp:BoundField DataField="Employee Name" HeaderText="Employee Name" />
        <asp:BoundField DataField="Manager Name" HeaderText="ManagerName" />
        <asp:BoundField DataField="Practice Name" HeaderText="Practice Name" />
        <asp:BoundField DataField="Program Name" HeaderText="Program Name" />
        <asp:BoundField DataField="Status" HeaderText="Current Status" />
        <asp:BoundField DataField="Prerequisite" HeaderText="Prerequisite" />
        <asp:TemplateField HeaderText="">
            <ItemTemplate>
                <asp:Button ID="BtnEdit" runat="server" Text="Take Action" />
                <asp:Button ID="Button1" runat="server" Text="View Details" />
            </ItemTemplate>
            <HeaderTemplate>
            </HeaderTemplate>
        </asp:TemplateField>
    </Columns>
</SharePoint:SPGridView>

现在我面临着这两行代码的问题

Now i am facing a proble with these two lines of code

dt.Columns.Add("Program Name", typeof(LookupField));
dt.Columns.Add("Prerequisite", typeof(String)); 

如果我不使用这个,那么这个webpart完美地工作。但我也想显示这些字段。我怎么做到这一点?

if i don't use this then this webpart works perfectly . but i wanted to display these fields too . how can i do this ?

推荐答案

你遇到的问题是空值。如果对象为null,则ToString()将失败。我假设所有其他字段都不会有任何空值(至少在您的查询中),但是您遇到问题的字段却没有。你有几个选择。你可以检查这个值是否为null,如果不是这个值,那么就把它放在一个空字符串中,对于许多已经是字符串的字段,你可以转换它们,而不是.ToString-ing它们。你可以使用处理空值的Convert.ToString(object)。我可以继续其他几种选择,但我认为你可以从这里拿走它。

The problem you're having is with null values. .ToString() will fail if the object is null. I assume that all of the other fields never have any null values (at least with your queries) but the fields that you are having problems with do. You have a few options. You can just check if the value is null and put in an empty string if it's not, for many of the fields that are already strings you can just cast them, rather than .ToString-ing them. You can use Convert.ToString(object) which handles nulls. I could go on with several other options, but I think you can take it from here.

这篇关于在SharePoint Webpart中检索列表中的值到Gridview?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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