您现在的位置:首页 >> 软件开发 >> 内容

C#在DataTable中建立表格并加载到dataGridView的方法

时间:2023-12-12 23:13:44 点击:

  核心提示:在DataTable中建立表格并加载到dataGridView的方法 //***************字段行*************************** DataTable tblData...

在DataTable中建立表格并加载到dataGridView的方法


  //***************字段行***************************
  DataTable tblDatas = new DataTable();
  DataColumn dc = null;
  tblDatas.Columns.Add("列名1", Type.GetType("System.String"));
  tblDatas.Columns.Add("列名1", Type.GetType("System.String"));
  tblDatas.Columns.Add("列名1", Type.GetType("System.String"));
  //***************字段行***************************


  //***************内容行***************************


  ***第一行内容*********************
  newRow = tblDatas.NewRow();
  newRow["列名1"] = "第一行列1内容";
  newRow["列名2"] = "第一行列2内容";
  newRow["列名3"] = "第一行列3内容";
  tblDatas.Rows.Add(newRow);
           
  ***第二行内容*********************
  newRow = tblDatas.NewRow();
  newRow["列名1"] = "第二行列1内容";
  newRow["列名2"] = "第二行列2内容";
  newRow["列名3"] = "第二行列3内容";
  tblDatas.Rows.Add(newRow);


  ***第三行内容*********************
  newRow = tblDatas.NewRow();
  newRow["列名1"] = "第三行列1内容";
  newRow["列名2"] = "第三行列2内容";
  newRow["列名3"] = "第三行列3内容";
  tblDatas.Rows.Add(newRow); 


 //***************内容行***************************


  dataGridView1.DataSource = tblDatas;


  //行头高度设置
   dataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
   dataGridView1.ColumnHeadersDefaultCellStyle.WrapMode = DataGridViewTriState.True;
   dataGridView1.ColumnHeadersHeight = 50;//字段行高度设置



    for (int 0 = 1; i < 3; i++)
    {
          this.dataGridView1.Rows[i].Height = 40; //行高设置
     }


    this.dataGridView1.RowsDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;//内容居中显示


  //行头居中显示
  DataGridViewCellStyle headerStyle = new DataGridViewCellStyle();
  headerStyle.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter;
  this.dataGridView1.ColumnHeadersDefaultCellStyle = headerStyle;


  dataGridView1.RowHeadersVisible = false;//最左边列不显示
  dataGridView1.EnableHeadersVisualStyles = false;


  dataGridView1.RowsDefaultCellStyle.Font = new Font("Tahoma", 12);//行字体设置
  ataGridView1.ColumnHeadersDefaultCellStyle.Font = new Font("Tahoma", 12, FontStyle.Bold);//行头字体设置


  dataGridView1.ColumnHeadersDefaultCellStyle.BackColor = Color.Yellow;//字段行背景颜色设置


  for (int n = 0; n < 3; n = n + 2)
  {
     dataGridView1.Rows[n].DefaultCellStyle.BackColor = Color.Gainsboro;//行背景颜色设置
  }


  dataGridView1.AllowUserToAddRows = false;//去掉最后一行空白行


  dataGridView1.ScrollBars = ScrollBars.None; //去除滚动条


  //列宽设置 
  this.dataGridView1.Columns[0].Width = 110;
  this.dataGridView1.Columns[1].Width = 110;
  this.dataGridView1.Columns[2].Width = 100;

作者:站长 来源:原创
相关文章
  • 没有相关文章
共有评论 0相关评论
发表我的评论
  • 大名:
  • 内容:
  • 陈工笔记(www.dui580.com) © 2024 版权所有 All Rights Reserved.
  • 站长:陈工 微信号:chengongbiji QQ:24498854
  • Powered by 陈工