According to the project of the PC purchase process described in the previous article, an order generation function is required in the project, the admin can easily select some of the applications for approval to generate orders. The interface operation is simple and clear, which is roughly as follows:
Click checkbox to automatically calculate the total value of the current order. click the button to generate the order.
This comes to mind the use of spgridview, a ready-made control, which has been used before. In fact, it is no big difference with gridview. Here is a brief introduction:
First create a webpart
In createchildcontrols (), you can set the data source and attributes of spgridview, and add fields.
However, the checkbox column must be generated by a custom template class. below is our custom checkbox template class, which implements the itemplate interface:
Code
Class Checkboxtemplatefield: itemplate
{
String ID;
Public Eventhandler oncheck = Null ;
Public Checkboxtemplatefield ( String Chbid, eventhandler checkevent)
{
ID = Chbid;
Oncheck = Checkevent;
}
Public Void Instantiatein (system. Web. UI. Control container)
{
Checkbox CHB = New Checkbox ();
CHB. autopostback = True ;
CHB. ID = ID;
CHB. checkedchanged + = Oncheck;
Container. Controls. Add (CHB );
}
}
In this way, you can call this template class in spgridview and provide the processing method for the oncheck event:
Code
Protected Override Void Createchildcontrols ()
{
If ( ! _ Error)
{
Try
{
Splist sourcelist = Spcontext. Current. Web. Lists [ " Purchase Request " ];
Datasource = New Spdatasource ();
This . Controls. Add (datasource );
Datasource. List = Sourcelist;
Gridview = New Spgridview ();
Gridview. autogeneratecolumns = False ;
Templatefield chbfield = New Templatefield ();
Chbfield. headertext = "" ;
Eventhandler oncheck = New Eventhandler (oncheck );
Chbfield. itemtemplate = New Checkboxtemplatefield ( " CHB " , Oncheck );
Gridview. Columns. Add (chbfield );
After we provide field binding, We need to specify a column as the group column. Here we specify the "team" column:
Code
Spboundfield createdfield = Createnewboundfield ( " Created " , " Created " , 0 );
Gridview. Columns. Add (createdfield );
spboundfield applicantfield = createnewboundfield ( " created by " , " created by " , 0 );
gridview. columns. add (applicantfield);
spboundfield mtfield = createnewboundfield ( " Machine Type " , " Machine Type " , 0 );
gridview. columns. add (mtfield);
spboundfield compfield = createnewboundfield ( " component type " , " component type " , 0 );
gridview. columns. add (compfield);
spboundfield purnumfield = createnewboundfield ( " purchase number " , " purchase number " , 0 );
gridview. columns. add (purnumfield);
spboundfield purreasonfield = createnewboundfield ( " purchase reason " , " purchase reason " , 0 );
gridview. columns. add (purreasonfield);
spboundfield mgrappfield = createnewboundfield ( " Manager approval " , " Manager approval " , 0 );
gridview. columns. add (mgrappfield);
spboundfield drtappfield = createnewboundfield ( " Director approval " , " Director approval " , 0 );
gridview. columns. add (drtappfield);
Spboundfield pricefield=Createnewboundfield ("Total price","Total price",0);
Gridview. Columns. Add (pricefield );
Gridview. allowgrouping= True ;
Gridview. allowgroupcollapse = True ;
Gridview. groupfield = " Team " ;
Gridview. groupfielddisplayname = " Team " ;
However, the field "team" is of the lookup type in the data source list. If not processed, the displayed result will be team: 23; # SharePoint test, that is, even the lookupid is displayed. here we need to handle the data binding, so we have added the gridview. rowdatabound + = new gridviewroweventhandler (gridview_rowdatabound ),CodeAs follows:
Code
Void Gridview_rowdatabound ( Object Sender, gridviewroweventargs E)
{
If (E. Row. rowtype = Datacontrolrowtype. datarow)
{
If (Sender As Spgridview). allowgrouping)
{
Spgridviewrow gridviewrow = E. Row As Spgridviewrow;
If (Gridviewrow ! = Null && Gridviewrow. headertext ! = Null )
{
Gridviewrow. headertext = " Team: " + New Spfieldlookupvalue (databinder. getpropertyvalue (E. Row. dataitem, (sender As Spgridview). groupfield). tostring (). lookupvalue;
}
}
}
}
In this way, make sure that when we perform groupby in spgridview, only the lookupvalue is displayed, not the lookupid; # lookupvalue format.
In addition, we can set the datakeynames of spgridview to save some information that we may need for each row, for example
Gridview. datakeynames = new string [] {"ID", "team", "created by", "total price", "Machine Type", "component type", "purchase number ", "shipped order "};
You can use the data as follows:
Gridview. datakeys [row. rowindex]. Values ["Machine Type"]. tostring ();
We also encountered a problem when using spgridview for webpart development, that is, when we access the session of SharePoint, an exception is reported on the page. However, in the Code, check the web for exceptions. in the config file, we found that the session module is also added to the httpmodule and the <page> node also enables the session. So far, we have not been able to identify the cause. Later, we had to use context. cache to replace. I hope you can read the official website for any reason. Please do not give me any further advice. Thank you ~
Spgridview is not complex to use. The key is to clarify the business logic in the project and select the appropriate solution to solve the problem.