简体   繁体   English

将ListItemCollection绑定为DataGridView的数据源

[英]Bind ListItemCollection as a DataSource of a DataGridView

I am using SharePoint Server 2013. I am trying to show a list data in a DataGridView in a windows forms application(client application). 我正在使用SharePoint Server2013。我试图在Windows窗体应用程序(客户端应用程序)的DataGridView中显示列表数据。 I obtained the ListItemCollection object related to the specific list. 我获得了与特定列表相关的ListItemCollection对象。 How can I map that object to the datasource of the DataGridView ? 如何将该对象映射到DataGridView的数据源?

I can't find any specific way to obtain a DataTable object from the ListItemCollection object. 我找不到从ListItemCollection对象获取DataTable对象的任何特定方法。 Because the list I used to obtain data is selected by a drop down list. 因为我用来获取数据的列表是由下拉列表选择的。 So there is no predetermined columns for a Datatable object. 因此,没有针对Datatable对象的预定列。 Thanks in advance. 提前致谢。 :) :)

try this one - 试试这个-

        ListItemCollection items = GetListItemCollections(); //Get list item collection

        DataTable dt = new DataTable();

        foreach (var field in items[0].FieldValues.Keys)
        {
            dt.Columns.Add(field);
        }

        foreach (var item in items)
        {
            DataRow dr = dt.NewRow();

            foreach (var obj in item.FieldValues)
            {
                if (obj.Value != null)
                {
                    string type = obj.Value.GetType().FullName;

                    if (type == "Microsoft.SharePoint.Client.FieldLookupValue")
                    {
                        dr[obj.Key] = ((FieldLookupValue)obj.Value).LookupValue;
                    }
                    else if (type == "Microsoft.SharePoint.Client.FieldUserValue")
                    {
                        dr[obj.Key] = ((FieldUserValue)obj.Value).LookupValue;
                    }
                    else
                    {
                        dr[obj.Key] = obj.Value;
                    }
                }
                else
                {
                    dr[obj.Key] = null;
                }
            }

            dt.Rows.Add(dr);
        }

        ResetDataGridView(); //Clear contents of datagridview
        dataGridView1.DataSource = dt;

Hope this helps.. 希望这可以帮助..

Thanks 谢谢

    Below function will return you DataTable. 

    internal DataTable GetDataTableFromListItemCollection()
            {
                string strWhere = string.Empty;
                DataTable dtGetReqForm = new DataTable();
                using (var clientContext = new ClientContext("sharepoint host url"))
                {
                    try
                    {
                        Microsoft.SharePoint.Client.List spList = clientContext.Web.Lists.GetByTitle("ReqList");
                        clientContext.Load(spList);
                        clientContext.ExecuteQuery();

                        if (spList != null && spList.ItemCount > 0)
                        {
                            Microsoft.SharePoint.Client.CamlQuery camlQuery = new CamlQuery();
                            camlQuery.ViewXml =
                            @"<View>" +
                            "<Query> " +
                                "<Where>" +
                                    "<And>" +
                                            "<IsNotNull><FieldRef Name='ID' /></IsNotNull>" +
                                            "<Eq><FieldRef Name='ReqNo' /><Value Type='Text'>123</Value></Eq>" +
                                    "</And>" +
                                "</Where>" +
                            "</Query> " +
                            "<ViewFields>" +
                                "<FieldRef Name='ID' />" +
                            "</ViewFields>" +
                            "</View>";

                            SPClient.ListItemCollection listItems = spList.GetItems(camlQuery);
                            clientContext.Load(listItems);
                            clientContext.ExecuteQuery();

                            if (listItems != null && listItems.Count > 0)
                            {
                                foreach (var field in listItems[0].FieldValues.Keys)
                                {
                                    dtGetReqForm.Columns.Add(field);
                                }

                                foreach (var item in listItems)
                                {
                                    DataRow dr = dtGetReqForm.NewRow();

                                    foreach (var obj in item.FieldValues)
                                    {
                                        if (obj.Value != null)
                                        {
                                            string key = obj.Key;
                                            string type = obj.Value.GetType().FullName;

                                            if (type == "Microsoft.SharePoint.Client.FieldLookupValue")
                                            {
                                                dr[obj.Key] = ((FieldLookupValue)obj.Value).LookupValue;
                                            }
                                            else if (type == "Microsoft.SharePoint.Client.FieldUserValue")
                                            {
                                                dr[obj.Key] = ((FieldUserValue)obj.Value).LookupValue;
                                            }
                                            else if (type == "Microsoft.SharePoint.Client.FieldUserValue[]")
                                            {
                                                FieldUserValue[] multValue = (FieldUserValue[])obj.Value;
                                                foreach (FieldUserValue fieldUserValue in multValue)
                                                {
                                                    dr[obj.Key] += (fieldUserValue).LookupValue;
                                                }
                                            }
                                            else if (type == "System.DateTime")
                                            {
                                                if (obj.Value.ToString().Length > 0)
                                                {
                                                    var date = obj.Value.ToString().Split(' ');
                                                    if (date[0].Length > 0)
                                                    {
                                                        dr[obj.Key] = date[0];
                                                    }
                                                }
                                            }
                                            else
                                            {
                                                dr[obj.Key] = obj.Value;
                                            }
                                        }
                                        else
                                        {
                                            dr[obj.Key] = null;
                                        }
                                    }
                                    dtGetReqForm.Rows.Add(dr);
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                    }
                    finally
                    {
                        if (clientContext != null)
                            clientContext.Dispose();
                    }
                }
                return dtGetReqForm;

            }

//once you have the DataTable() with you you can set the DataSource 

//DataGridView1 is the id value

DataGridView1.DataSource = GetDataTableFromListItemCollection(); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM