DataGrid顯示的時間爲什麼沒有“時、分、秒”?——WinForm中對DataGrid應用樣式

   SqlConnection conn = new SqlConnection( "server=(local);Database=prjmaster;uid=sa;pwd=123456" );
   SqlDataAdapter da = new SqlDataAdapter( "SELECT TOP 10 * FROM Bug", conn );
   DataSet ds = new DataSet();
   try
   {
    da.Fill( ds, "BugTable" );
   }
   catch( Exception ex )
   {
    MessageBox.Show( ex.Message );
   }
   dataGrid1.DataSource = ds.Tables["BugTable"];

   CurrencyManager myCurrencyManger =   //獲取對象列表管理器
    (CurrencyManager)this.BindingContext[ds];

   DataGridTableStyle myTableStyle = new DataGridTableStyle(); //創建表樣式
   myTableStyle.MappingName = "BugTable";

   PropertyDescriptor myDateDescriptor =  //獲取包含日期的列的PropertyDescriptor
    myCurrencyManger.GetItemProperties()["bugopendate"];
   
   // 'G' is for MM/dd/yyyy HH:mm:ss date format.
   DataGridColumnStyle myDateStyle =  //創建列樣式
    new DataGridTextBoxColumn(myDateDescriptor,"G");

   myDateStyle.MappingName = "bugopendate"; //數據庫中的列名
   myDateStyle.HeaderText = "Bug Open Date"; //要顯示在DataGird表頭上的文字
   myDateStyle.Width = 150;   //寬度

   /*如果你想顯示多個列,就要生成多個DataGridColumnStyle對象*/
   DataGridColumnStyle column2 = new DataGridTextBoxColumn();
   column2.MappingName = "bugassignto";
   column2.HeaderText = "Bug接收者";

   myTableStyle.GridColumnStyles.Add(myDateStyle);  //加入列樣式
   myTableStyle.GridColumnStyles.Add(column2);

   dataGrid1.TableStyles.Add(myTableStyle);  //將表樣式加入DataGrid

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章