|
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
|