Bind the query list in Sharepoint to the gridview

Source: Internet
Author: User

# Region data source binding
/// <Summary>
/// Data source binding
/// </Summary>
/// <Param name = "strquery"> </param>
Private void binddata (string strquery)
{
Using (spsite site = new spsite (comment. weburl) // http: // MOSS: 8000 name of your own website set
{
Foreach (spweb in site. allwebs)
{
Splist list = web. Lists ["task"];
Spquery query = new spquery ();
Query. viewfields = "<fieldref name = 'id'/> <fieldref name = 'title'/> <fieldref name = 'doccicon '/> <fieldref name = 'linktitle'/> <fieldref name = 'assignedto '/> <fieldref name = 'status'/> <fieldref name = 'priority'/> <fieldref name = 'duedate'/> "; // name of the field in the list
Query. query = strquery;
This. gridviewlist. datasource = List. getitems (query). getdatatable ();
This. gridviewlist. databind ();
}
}
}
# Endregion

// Obtain the query Conditions

Effect:

Method 1:

Private string getwhere ()
{
// Query the scheduled end time range
String strquery = "";
String stringbookingbegin = this.txt bookingtimeb. Text. Trim ();
String txtbookingtimee = this.txt bookingtimee. Text. Trim ();
If (stringbookingbegin. length> 0 & txtbookingtimee. Length <= 0)
{
Strquery + = "<Leq> <fieldref name = 'bookingendtime'/> <value includetimevalue = 'true' type = 'datetime'>" + stringbookingbegin + "</value> <Leq> ";
}
If (stringbookingbegin. Length <= 0 & txtbookingtimee. length> 0)
{
Strquery + = "<geq> <fieldref name = 'bookingendtime'/> <value includetimevalue = 'true' type = 'datetime'>" + txtbookingtimee + "</value> </ geq> ";
}
If (this.txt bookingtimee. Text. Trim (). length> 0 & this.txt bookingtimeb. Text. Trim (). length> 0)
{
Strquery + = string. format (@ "<and> <geq> <fieldref name = 'bookingendtime'/> <value type = 'datetime'> {0} </value> </geq>
<Leq> <fieldref name = 'bookingendtime'/> <value type = 'datetime'> {1} </value> </Leq> </and> ", stringbookingbegin, txtbookingtimee );
}
If (this. ddlmrname_query.selectedindex> 0)
{
Strquery + = "<EQ> <fieldref name = 'title'/> <value type = 'text'>" + this. ddlmrname_query.selecteditem.text.trim () + "</value> </EQ> ";
}
If (! String. isnullorempty (strquery. Trim ()))
{
Strquery = "<where> <and>" + strquery + "<EQ> <fieldref name = 'state'/> <value type = 'text'> valid </value> </EQ> </and> </where> ";
}
Else
{
Strquery = "<where> <EQ> <fieldref name = 'state'/> <value type = 'text'> valid </value> </EQ> </where>";
}
Return strquery;
}

Method 2: LINQ to SharePoint

# Region Query statistics
/// <Summary>
/// Query statistics button
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "E"> </param>
Protected void btnsearch_click (Object sender, eventargs E)
{
Mrbmodeldatacontext A = new mrbmodeldatacontext (comment. weburl );
Entitylist <meetingroombookingitem> somelist = A. getlist <meetingroombookingitem> ("list name"); //. Where <A. meetingroombooking> (getcon ())
VaR mrblists = from m in somelist
Where getcondition (m)
Group M by new {M. Title, M. region, M. bookingperson} into demgrp
Select
New
{
Mrbname = demgrp. Key. title,
Booingperson = demgrp. Key. bookingperson,
Bookingcount = demgrp. Count (),
Info = demgrp
};
Gridview1.datasource = mrblists;
Gridview1.databind ();

}
# Endregion

# Region Query Conditions
/// <Summary>
/// Query Conditions
/// </Summary>
/// <Param name = "MRL"> </param>
/// <Returns> </returns>
Private bool getcondition (meetingroombookingitem MRL)
{
// Query the scheduled start time period
Bool boolresult = true;

String stringbookingbegin = this.txt bookingtimeb. Text. Trim ();
String txtbookingtimee = this.txt bookingtimee. Text. Trim ();
If (stringbookingbegin. length> 0)
{
Boolresult & = MRL. bookingbegintime> = convert. todatetime (stringbookingbegin );
}

If (txtbookingtimee. length> 0)
{
Boolresult & = MRL. bookingbegintime <= convert. todatetime (txtbookingtimee );
}
If (this. ddlmrname_query.selectedindex> 0)
{
Boolresult & = MRL. Title. Equals (this. ddlmrname_query.selecteditem.text.trim ());
} If (this. ddlregion. selectedindex> 0)
{
Boolresult & = MRL. region. Equals (this. ddlregion. selectedvalue. Trim ());
}
Return boolresult;
}
# Endregion

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.