2011-12-15 144 views
1

我使用Visual Studio創建了一個Web部件,以在GridView中顯示列表的選定列。但問題是,無論何時我將語言環境更改爲英國 - 英國(這是默認英語 - 美國),不幸的是它和網站對它沒有影響,儘管日期格式應該被更改。Sharepoint區域設置

我嘗試下面的代碼更改地區和日期格式:

protected void btnClick_Event(object sender, EventArgs e) 
    { 
     using (SPSite osite = new SPSite(SPContext.Current.Web.Url)) 
     { 
      using (SPWeb oweb = osite.OpenWeb()) 
      { 
       if (DropDownList1.SelectedValue == "English-UK") 
       { 
        oweb.AllowUnsafeUpdates = true; 
        oweb.Locale = new System.Globalization.CultureInfo("en-GB"); 
        oweb.Update(); 
       } 
       else if(DropDownList1.SelectedValue == "English-US") 
       { 
        oweb.Locale = new System.Globalization.CultureInfo("en-US"); 
        oweb.Update(); 
       } 

       lblmsg.Text = "Region changed successfully";   
      } 
     } 
    } 

的WebPart代碼如下:

using System; 
using System.ComponentModel; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Web.UI.WebControls.WebParts; 
using Microsoft.SharePoint; 
using Microsoft.SharePoint.WebControls; 
using System.Data; 

namespace SharePointProject100.CustomGridWebpart 
{ 
    [ToolboxItemAttribute(false)] 
    public class CustomGridWebpart : WebPart 
    { 

    SPGridView grdview = new SPGridView(); 
    protected override void CreateChildControls() 
    { 
     grdview.ID = "grdview"; 
     grdview.AutoGenerateColumns = false; 

     this.Controls.Add(grdview); 
     using (SPSite osite = new SPSite(SPContext.Current.Web.Url)) 
     { 
      using (SPWeb web = osite.OpenWeb()) 
      { 
       SPList mylist = web.Lists["EmployeeDetails"]; 
       BindToGrid(mylist, grdview); 
      } 
     } 
    } 

    private void BindToGrid(SPList myList, SPGridView gridView) 
    { 
     // get all the listitem 
     SPListItemCollection results = myList.Items; 

     // create the datatable object 
     DataTable table = new DataTable(); 
     table.Columns.Add("Employee Name", typeof(string)); 
     table.Columns.Add("DOB", typeof(string)); 
     table.Columns.Add("JoiningDate", typeof(string)); 
     table.Columns.Add("MembershipExpiryDate", typeof(string)); 

     // Create rows for each splistitem 
     DataRow row; 
     foreach (SPListItem result in results) 
     { 
      row = table.Rows.Add(); 
      row["Employee Name"] = Convert.ToString(result["Employee Name"]); 
      row["DOB"] = Convert.ToString(result["DOB"]); 
      row["JoiningDate"] = Convert.ToString(result["JoiningDate"]); 
      row["MembershipExpiryDate"] = Convert.ToString(result["MembershipExpiryDate"]); 
     } 

     // create the bound fields 
     SPBoundField boundField; 
     boundField = new SPBoundField(); 
     boundField.HeaderText = "Employee Name"; 
     boundField.DataField = "Employee Name"; 
     boundField.ItemStyle.HorizontalAlign = HorizontalAlign.Center; 
     boundField.ItemStyle.Wrap = false; 
     gridView.Columns.Add(boundField); 

     boundField = new SPBoundField(); 
     boundField.HeaderText = "DOB"; 
     boundField.DataField = "DOB"; 
     boundField.ItemStyle.HorizontalAlign = HorizontalAlign.Center; 
     boundField.ItemStyle.Wrap = false; 
     gridView.Columns.Add(boundField); 

     boundField = new SPBoundField(); 
     boundField.HeaderText = "JoiningDate"; 
     boundField.DataField = "JoiningDate"; 
     boundField.ItemStyle.HorizontalAlign = HorizontalAlign.Center; 
     boundField.ItemStyle.Wrap = false; 
     gridView.Columns.Add(boundField); 

     boundField = new SPBoundField(); 
     boundField.HeaderText = "MembershipExpiryDate"; 
     boundField.DataField = "MembershipExpiryDate"; 
     boundField.ItemStyle.HorizontalAlign = HorizontalAlign.Center; 
     boundField.ItemStyle.Wrap = false; 
     gridView.Columns.Add(boundField); 

     gridView.AutoGenerateColumns = false; 
     gridView.DataSource = table.DefaultView; 
     gridView.DataBind(); 
    } 
} 

}

+0

要更改區域設置,請按照以下步驟操作:站點操作 - >站點設置 - >站點管理 - >區域設置 – 2011-12-15 11:11:27

回答

1

設置的LocaleID在web的RegionalSettings是應該將其標記爲「髒」(查看ILSpy中的反彙編代碼),但是我沒有任何運氣可以讓它基於這種改變來「更新」其他程序。