winform下DataGrid显示样式的小问题
1. 如何让页眉内容居中显示
2. 如何让页眉内容显示自己取的名字
3. 如何让数据项内容居中显示
4. 如何让数据项内容只读
5. 怎么去掉DataGird左边的那一个空列?(就是上面有个箭头和一个*号的那一列)
一有答案马上结帖
------解决方案--------------------你说的 "页眉 "我理解为 "列名 "不知对不对?
假定已存在一个数据源为DataTable的DataGird对象grdText
如何让列名显示为自己取的名字:
DataTable dt =(DataTable)grdText.DataSource;
dt.Columns[0].ColumnName= "自定义列名1 "; //第一列列名
dt.Columns[1].ColumnName= "自定义列名1 "; //第二列列名
dt.Columns[2].ColumnName= "自定义列名1 "; //第三列列名
………
如何让数据项内容只读:
grdText.ReadOnly=true;
如何去掉DataGird左边的那一个空列:
grdText.RowHeadersVisible =false;
至于内容的居中显示,没有使用过,Sorry!
抛砖引玉吧!
------解决方案--------------------using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
namespace prjDataGirdStyle
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.DataGrid grdTest;
private System.ComponentModel.Container components = null;
public Form1()
{
InitializeComponent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.grdTest = new System.Windows.Forms.DataGrid();
((
System.ComponentModel.ISupportInitialize)(this.grdTest)).BeginInit();
this.SuspendLayout();
//
// grdTest
//
this.grdTest.DataMember = " ";
this.grdTest.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.grdTest.Location = new System.Drawing.Point(16, 24);
this.grdTest.Name = "grdTest ";
this.grdTest.Size = new System.Drawing.Size(368, 224);
this.grdTest.TabIndex = 0;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(416, 283);
this.Controls.Add(this.grdTest);
this.Name = "Form1 ";
this.Text = "Form1 ";
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.grdTest)).EndInit();
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
//在这里对列名和列的对齐方式方式进行设置
private void Form1_Load(object sender, System.EventArgs e) {
SqlConnection cnn=new SqlConnection( "server=.;database=pubs;uid=sa;pwd=; ");
SqlDataAdapter da=new SqlDataAdapter( "Select * From Titles ",cnn);
DataTable dt=new DataTable( "titles ");
DataGridTableStyle dgts=new DataGridTableStyle();
dgts.MappingName= "titles ";
this.grdTest.TableStyles.Add(dgts);
DataGridColumnStyle dgcs1=new DataGridTextBoxColumn();
dgcs1.MappingName= "title_id ";
dgcs1.HeaderText= "编号 ";
dgcs1.Alignment=HorizontalAlignment.Center;