改变datagrid列名

原创 2006年05月22日 09:33:00

//表头类

                  public class TopHeaderColumn

                  {

                         public TopHeaderColumn()

                         {

                         this.columnCollection=new ArrayList();

                         }

                          private string caption;

                        //表头的显示文本

                          public string Caption

                          {

                            get {return caption;}

                            set {caption=value;}

                          }

                         private ArrayList columnCollection;

                        //用来记录属于表头管辖的各列的信息(通过往集合里添加object)

                         public ArrayList ColumnCollection

                         {

                          get {return this.columnCollection;}

                          set {this.columnCollection=value;}

                          }

                         private int width;

                         //表头的宽度

                         public int Width

                         {

                          get {return width;}

                          set {width=value;}

                          }

                         private Image image=null;

                         //表头的图标

                         public Image Image

                         {

                           get {return image;}

                           set {image=value;}

                          }

 

                    }

       另外,因为以后的代码需要DataGrid水平滚动条的位置,而DataGrid无法取得水平滚动条的位置,所以需要对DataGrid做一点修改。

                   public class myDataGrid:DataGrid

                   {

 

                       public ScrollBar HScrollBar

                       {

                          get {return this.HorizScrollBar;}

                       }

                   }

 

       好了,可以工作了。新建一个Window应用程序,加入一个myDataGrid、SqlConnection和ImageList,连接SQL数据库NorthWind。当然,还得加入上面的代码

                 

               namespace WindowsApplication1

              {

                       public class Form1 : System.Windows.Forms.Form

                       {

                          private System.Data.SqlClient.SqlConnection sqlConnection1;

                          private myDataGrid dataGrid1;

                          private ArrayList al;

                          private System.Windows.Forms.ImageList imageList1;

                          //在InitializeComponent()里加入这一句:this.dataGrid1 = new myDataGrid(),并根据你的需要设置其他的DataGrid属性。注意,CaptionVisible必须设为true,CaptionText=""。

                          private void Form1_Load(object sender, System.EventArgs e)

                          {

                             SqlDataAdapter da=new SqlDataAdapter("select lastname, firstname, Address,City from employees",this.sqlConnection1);

                             DataSet ds=new DataSet();

                             da.Fill(ds,"employees");

                             this.dataGrid1.DataSource=ds;

                             this.dataGrid1.DataMember="employees";

 

                             //设置DataGrid的各列

                             DataGridTextBoxColumn c1=new DataGridTextBoxColumn();

                             DataGridTextBoxColumn c2=new DataGridTextBoxColumn();

                             DataGridTextBoxColumn c3=new DataGridTextBoxColumn();

                             DataGridTextBoxColumn c4=new DataGridTextBoxColumn();

                             c1.MappingName="lastname";

                             c2.MappingName="firstname";

                             c3.MappingName="Address";

                             c4.MappingName="City";

                             c1.HeaderText="lastname";

                             c2.HeaderText="firstname";

                             c3.HeaderText="Address";

                             c4.HeaderText="City";

               c1.WidthChanged+=new EventHandler(this.abc);//列的宽变动时调整表头宽度

                             c2.WidthChanged+=new EventHandler(this.abc);

                             c3.WidthChanged+=new EventHandler(this.abc);

                             c4.WidthChanged+=new EventHandler(this.abc);

 

                             DataGridTableStyle dts=new DataGridTableStyle();

                             dts.GridColumnStyles.Add(c1);

                             dts.GridColumnStyles.Add(c2);

                             dts.GridColumnStyles.Add(c3);

                             dts.GridColumnStyles.Add(c4);

 

                             dts.MappingName="employees";   this.dataGrid1.TableStyles.Add(dts);

 

                              //建立自画的表头类并将它们加入集合al  

                             al=new ArrayList();

                             TopHeaderColumn tc1=new TopHeaderColumn();

                             tc1.Caption="Name";

                             tc1.Image=this.imageList1.Images[0];

                             tc1.ColumnCollection.Add(0);//记录它管辖的列的index

                             tc1.ColumnCollection.Add(1);

                             tc1.Width=c1.Width+c2.Width;

 

                            TopHeaderColumn tc2=new TopHeaderColumn();

                             tc2.Caption="Address";

                             tc2.ColumnCollection.Add(2);

                             tc2.ColumnCollection.Add(3);

                             tc2.Width=c3.Width+c4.Width;

  

                             al.Add(tc1);

                             al.Add(tc2);

                             this.dataGrid1.Paint += new System.Windows.Forms.PaintEventHandler(this.dataGrid1_Paint);

                          }

 

                         private void dataGrid1_Paint(object sender, System.Windows.Forms. PaintEventArgs e)

                        {

                           int x=0;

                           //计算出第一个表头的左边距

                           int left=this.dataGrid1.TableStyles[0].RowHeaderWidth-this.dataGrid1.HScrollBar.Value;

                           //遍历表头集合al,画出表头  

                           foreach (object o in this.al)

                           {

                          //计算出表头文字(文字居中)的左边距

                           x=left+(((TopHeaderColumn)o).Width-Convert.ToInt32(e.Graphics. MeasureString (((TopHeaderColumn)o).Caption, this.dataGrid1.CaptionFont). Width))/2;

                          //完成表头绘制 

                          if (((TopHeaderColumn)o).Image!=null)

                          e.Graphics.DrawImage(((TopHeaderColumn)o).Image,x-imageList1.Images[0].Size.Width,0);

 

                           e.Graphics.DrawString(((TopHeaderColumn)o).Caption,this.dataGrid1. CaptionFont,new SolidBrush(this.dataGrid1.CaptionForeColor),x,0);

                           if (x>0)

                           e.Graphics.DrawLine(new Pen(Color.Black,2),left-1,0,left-1,this.dataGrid1.Height);

                           //计算出下一个表头的左边距

                           left+=((TopHeaderColumn)o).Width;

                           }

                           if (x<this.dataGrid1.Width)

                           e.Graphics.DrawLine(new Pen(Color.Black,2),left-1,0,left-1,this.dataGrid1.Height); 

                         }

 

                     private void abc(object sender,EventArgs e)

                     {

                       //设置表头的宽度

                        foreach (object o in this.al)

                        {

                         ((TopHeaderColumn)o).Width=0;

                        foreach (int i in ((TopHeaderColumn)o).ColumnCollection)

                        {   ((TopHeaderColumn)o).Width+=this.dataGrid1.TableStyles[0].GridColumnStyles[i].Width;

                        }

                        }

                      }

 

 


最简便的方法 select name as 姓名 frome student

easyui之datagrid动态修改列

easyui datagrid 定义列的方式通常是: $('#grid').datagrid({columns:[[ {field:'f1',title:'字段1',width:160}...

一个简单的easyui动态初始化datagrid的列名的实例

在用easyui的时候,很多时候datagrid的列名是动态的而不是固定的,那么要动态加载datagrid的列名,有一种比较方便的方法是在easyui初始化datagrid之前,用ajax取到colu...

动态改变easyui datagrid 列的title

1.通过截图知道,要修改的title是span标签中的文字,所以我要想办法拿到span标签对象。 2.span标签不能直接取到,应为没有任何属性可以唯一标识这个标签。查看这个标签的父节点,父节点di...
  • A8_8A
  • A8_8A
  • 2016年05月19日 23:17
  • 5238

easyUI datagrid ajax实时刷新 动态添加列名

上一篇实现了datagrid加载时列名的动态添加,因项目需求,需要对此页面设置定时器,3秒刷新一次,用datagrid的reload方法刷新时会有闪屏的效果,网上搜的在本页面中添加css样式的方法虽然...

easyUI 1.3.2 dataGrid动态绑定列名和数据

修改easyUI 1.3.2源码使列名可以通过后台设定

datagrid动态加载列名和数据,附上源码

html代码 style="height:85%" > id="enterGroupListDG" class="easyui-datagrid" title="动态表单查询" fit="true"...

easyui datagrid动态绑定列名和数据

ashx文件内容 public void ProcessRequest(HttpContext context) {//示例用,各位可以根据自己需求写 var id = ...

EasyUI 1.3.1 datagrid动态绑定列名和数据

很多网友对【easyui datagrid动态绑定列名和数据 】存在很多疑惑,实验后不能成功,这是因为不同版本的EasyUI源码不同。我就最新的版本的EasyUI 1.3.1做了示例,并简化了后台代码...

jQuery EasyUI datagrid列名包含特殊字符会导致表格错位

首先申明:本文所述的Bug存在于1.3.3以及更高版本中,其它低版本,本人未测试,太老的版本不想去折腾了。 洒家在写前端的SQL执行工具时,表格用了 jQuery EasyUI datagrid...

LigerGrid动态改变加载字段列名

  • 2017年05月22日 09:13
  • 17KB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:改变datagrid列名
举报原因:
原因补充:

(最多只允许输入30个字)