日期:2014-05-18 浏览次数:21100 次
//横向显示表格 private void RotateDGV(DataTable dt , DataGridView DgvData) { DgvData.Rows.Clear(); DgvData.Columns.Clear(); //添加列到DgvData DataGridViewTextBoxColumn column = new DataGridViewTextBoxColumn(); column.HeaderText = column.Name = dt.Columns[0].ColumnName; column.FillWeight = 10; DgvData.Columns.Add(column); DataGridViewTextBoxColumn[] columns = new DataGridViewTextBoxColumn[dt.Rows.Count]; for (int dtrow = 0; dtrow < dt.Rows.Count; dtrow++) { column = new DataGridViewTextBoxColumn(); object o = dt.Rows[dtrow][0]; if (o.GetType().Name == "DateTime") { column.HeaderText = column.Name = ((DateTime)o).ToString("yyyy-MM"); } else column.HeaderText = o.ToString(); columns[dtrow] = column; column.FillWeight = 10; } DgvData.Columns.AddRange(columns); ///在运行中强行中断, 光标总是停留在这一行. //添加数据 DgvData.Rows.Add(dt.Columns.Count - 1); for (int dtcol = 1; dtcol < dt.Columns.Count; dtcol++) { DgvData[0, dtcol - 1].Value = dt.Columns[dtcol].ColumnName; string colType = DgvData[0, dtcol - 1].Value.GetType().Name; DgvData[0, dtcol - 1].Style.Alignment = colType == "String" ? DataGridViewContentAlignment.MiddleLeft : DataGridViewContentAlignment.MiddleCenter; for (int dtrow = 0; dtrow < dt.Rows.Count; dtrow++) { DgvData[dtrow + 1, dtcol - 1].Value = dt.Rows[dtrow][dtcol]; } } }