网上SourceGrid相关的资料很少,使用过程中做了下记录,以便日后查用

1:初始化

this.grid = new SourceGrid.Grid();
this.grid.Size = new System.Drawing.Size(552, 224);
this.grid.Location = new System.Drawing.Point(0, 0);
this.tabPage1.Controls.Add(this.grid);

2:设置行列

方法1: grid.Redim(20,12);
方法2: grid.ColumnsCount = 3;
grid.RowsCount = 3;
方法3: 在属性中设置 ColumnsCount和RowsCount的值 grid.Rows.Insert(3);

3:固定行列

grid.FixedRows = 2;
grid.FixedColumns = 3;

4:标题列赋值及标题样式(标题是否自带排序)

 grid[0, 0] = new SourceGrid.Cells.ColumnHeader("序号");
grid[0, 1] = new SourceGrid.Cells.ColumnHeader("区块名称"); 对标题样式进行修改
grid1[0, 0] = new MyHeader("序号"); private class MyHeader : SourceGrid.Cells.ColumnHeader
{
public MyHeader(object value) : base(value)
{
//1 Header Row
SourceGrid.Cells.Views.ColumnHeader view = new SourceGrid.Cells.Views.ColumnHeader();
view.Font = new Font(FontFamily.GenericSansSerif, 10, FontStyle.Bold);
view.TextAlignment = DevAge.Drawing.ContentAlignment.MiddleCenter;
View = view;
      //自带排序true;不实现排序false
AutomaticSortEnabled = false;
}
}

5:内容列赋值

 字符串类型: grid[1, 3] = new SourceGrid.Cells.Cell("Hello " + r.ToString(), typeof(string));
复选框类型: grid[1, 5] = new SourceGrid.Cells.CheckBox(null, true);
按钮类型: grid[62, 2] = new SourceGrid.Cells.Button("删除");
时间类型: grid[1, 4] = new SourceGrid.Cells.Cell(DateTime.Today, typeof(DateTime));
grid[1, 2] = new SourceGrid.Cells.Cell(12121); 获取:var aa=grid[1, 4].Value.ToString();
重新赋值: grid[r + i, 4].Value = "2"; 注意:单元格格式用上面形式,赋值用Value可以避免多次赋值无效的问题

6:单元格宽高设置

单元格根据内容自动变化:
(不包含标题列,只对内容有效,我感觉不好用,还是根据自己需求设置宽度)
grid.AutoSizeCells(); grid.Columns[0].Width = 100;(要在AutoSizeCells()方法后使用,否则无效)
grid.Rows[0].Height = 30;

7:删除某行某列

grid.Rows.Remove(3);//删除下标为3的行
grid.Columns.Remove(3);

8:设置单元格文本/图片居中及背景色(斑马线即隔行换色)

/// <summary>
/// 设置表头字体及样式
/// </summary>
public class MyHeader : SourceGrid.Cells.ColumnHeader
{
  public MyHeader(object value) : base(value)
  {
  //1 Header Row
  SourceGrid.Cells.Views.ColumnHeader view = new SourceGrid.Cells.Views.ColumnHeader();
  view.Font = new Font(FontFamily.GenericSansSerif, 9, FontStyle.Bold);
  view.TextAlignment = DevAge.Drawing.ContentAlignment.MiddleCenter;
  View = view;

  AutomaticSortEnabled = false;
  }
}

//使用grid[0, 0] = new MyHeader("序号");

 
SourceGrid.Cells.Views.Cell transparentView = new SourceGrid.Cells.Views.Cell();
transparentView.BackColor = Color.White;
 //字体
//transparentView.Font = new Font(FontFamily.GenericSansSerif, 9, FontStyle.Bold);
transparentView.TextAlignment = DevAge.Drawing.ContentAlignment.MiddleCenter;
transparentView.ImageAlignment = DevAge.Drawing.ContentAlignment.MiddleCenter;
SourceGrid.Cells.Views.Cell semiTransparentView = new SourceGrid.Cells.Views.Cell();
semiTransparentView.BackColor = Color.LightCyan;
semiTransparentView.TextAlignment = DevAge.Drawing.ContentAlignment.MiddleCenter;
semiTransparentView.ImageAlignment = DevAge.Drawing.ContentAlignment.MiddleCenter;
CheckBoxBackColorAlternate viewCheckBox = new CheckBoxBackColorAlternate(Color.LightCyan, Color.White);

隔行显示

for (int r = 1; r < grid.RowsCount; r++)
  {
   for (int j = 0; j < grid.ColumnsCount; j++)
   {
    if ((r - 1) % 2 == 0) grid[r, j].View = transparentView;
    else grid[r, j].View = semiTransparentView;
   }
  }

9:按钮button的相关设置

SourceGrid.Cells.Views.Cell buttonView = new SourceGrid.Cells.Views.Button();
buttonView.TextAlignment = DevAge.Drawing.ContentAlignment.MiddleCenter;
buttonView.Padding = new DevAge.Drawing.Padding(2, 30, 2, 5);
使用:
grid[1, 20] = new SourceGrid.Cells.Button("删除");

grid[1,20].View=buttonView;

10:复选框checkBox的相关设置

        //使用:
grid[r, 16] = new SourceGrid.Cells.CheckBox(null, true);
     CheckBoxBackColorAlternate viewCheckBox = new CheckBoxBackColorAlternate(Color.LightCyan, Color.White);
grid[1, 3].View = viewCheckBox;
/// <summary>
/// 复选框CheckBox的相关设置
/// </summary>
public class CheckBoxBackColorAlternate : SourceGrid.Cells.Views.CheckBox
{
public CheckBoxBackColorAlternate(Color firstColor, Color secondColor)
{
FirstBackground = new DevAge.Drawing.VisualElements.BackgroundSolid(firstColor);
SecondBackground = new DevAge.Drawing.VisualElements.BackgroundSolid(secondColor);
} private DevAge.Drawing.VisualElements.IVisualElement mFirstBackground;
public DevAge.Drawing.VisualElements.IVisualElement FirstBackground
{
get { return mFirstBackground; }
set { mFirstBackground = value; }
} private DevAge.Drawing.VisualElements.IVisualElement mSecondBackground;
public DevAge.Drawing.VisualElements.IVisualElement SecondBackground
{
get { return mSecondBackground; }
set { mSecondBackground = value; }
} protected override void PrepareView(SourceGrid.CellContext context)
{
base.PrepareView(context); if (Math.IEEERemainder(context.Position.Row, 2) == 0)
Background = FirstBackground;
else
Background = SecondBackground;
}
}

注意:SourceGrid中不知道为什么没有单选按钮Radio,我为了实现效果直接用了CheckBox复选框,在单元格的点击事件中实现单选效果

 private void clickEvent_Click(object sender, EventArgs e)
{
SourceGrid.CellContext context = (SourceGrid.CellContext)sender;
if (context.Position.Column == 1)//选择事件
{
for (int r = 1; r < grid.RowsCount; r++)
{
if (r == context.Position.Row)
{
grid[r, 1].Value = true;
}
else
{
grid[r, 1].Value = false;
}
}
}
}

11:复制粘贴

1.grid.ClipboardMode = SourceGrid.ClipboardMode.All;
2.也可在属性中设置
若要实现单元格不可编辑但是可以复制功能的话需要注意:(不能使用14中的单元格不可操作代码)
上述设置实现后,在赋值地方
grid[1, 3] = new SourceGrid.Cells.Cell("Hello " + r.ToString(), typeof(string));改为
grid[1, 3] = new SourceGrid.Cells.Cell("Hello " + r.ToString());即不要带赋值类型就可实现不可编辑

12:单元格添加事件(点击事件/修改编辑后触发的事件)(点击/鼠标进入单元格显示手型,离开现实默认样式)

SourceGrid.Cells.Controllers.CustomEvents clickEvent = new SourceGrid.Cells.Controllers.CustomEvents();
clickEvent.Click += new EventHandler(clickEvent_Click);//点击(单击)
clickEvent.DoubleClick += new EventHandler(clickEvent_DoubleClick);//双击
clickEvent.EditEnded += new EventHandler(editEvent_EditEnded);//编辑后
//鼠标进入和离开
clickEvent.MouseEnter += new EventHandler(mouseEvent_MouseEnter);
clickEvent.MouseLeave += new EventHandler(mouseEvent_MouseLeave);
//绑定事件
grid[r, 8].AddController(clickEvent);
//单击事件
private void clickEvent_Click(object sender, EventArgs e)
{
SourceGrid.CellContext context = (SourceGrid.CellContext)sender;
MessageBox.Show(this, context.Position.ToString());
}
//双击事件
private void clickEvent_DoubleClick(object sender, EventArgs e)

  {
    SourceGrid.CellContext context = (SourceGrid.CellContext)sender;
    MessageBox.Show(this, context.Position.ToString());
    //要执行的操作

  }

/// <summary>
/// 进入单元格鼠标显示手型
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void mouseEvent_MouseEnter(object sender, EventArgs e)
{
  SourceGrid.CellContext context = (SourceGrid.CellContext)sender;
  if (context.Position.Column == 8)
  {
    this.Cursor = Cursors.Hand;
  }
}
/// <summary>
/// 离开单元格鼠标显示默认状态
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void mouseEvent_MouseLeave(object sender, EventArgs e)
{
  SourceGrid.CellContext context = (SourceGrid.CellContext)sender;
  if (context.Position.Column == 8)
  {
    this.Cursor = Cursors.Default;
  }
}
        /// <summary>
/// 单元格编辑结束
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void editEvent_EditEnded(object sender, EventArgs e)
{
SourceGrid.CellContext context = (SourceGrid.CellContext)sender;
//进行保存等相关操作
}

13:隐藏/显示行/列

grid1.Columns[0].Visible=true;
grid1.Rows[1].Visible = false;

14:使单元格不可操作(不可编辑)

grid[1, 2].AddController(SourceGrid.Cells.Controllers.Unselectable.Default);

15:ComboBox下拉框的使用

SourceGrid.Cells.Editors.ComboBox cbKFJD = new SourceGrid.Cells.Editors.ComboBox(typeof(string));
cbKFJD.StandardValues = new string[] { "老区","新区" };
cbKFJD.EditableMode = SourceGrid.EditableMode.Focus | SourceGrid.EditableMode.SingleClick | SourceGrid.EditableMode.AnyKey;
grid[1, 2] = new SourceGrid.Cells.Cell("老区", cbKFJD);

16:报错索引超出界限的特殊性情况处理:

在代码中设置了足够的行数和列数但是在colspan或rowspan的时候依然报错可能原因是在属性设置中Columnscout或Rowscount设置了值但是值不够大,

解决办法:1.属性设置Columnscout和Rowscount不要设值默认值0(推荐)

    2.属性设置Columnscout或Rowscount设置一个足够大的值就行

17:进入页面不显示焦点:grid.Selection.FocusStyle = FocusStyle.None;

18:以Excel格式导出当前SourceGrid表格

     /// <summary>
/// 导出计算报表
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void materialButton2_Click(object sender, EventArgs e)
{
try
{
SaveFileDialog sfd = new SaveFileDialog();
string tradeTime = DateTime.Now.ToString("yyyyMMddHHmmss", DateTimeFormatInfo.InvariantInfo);
sfd.FileName = "计算报表" + tradeTime;
if (sfd.ShowDialog() != DialogResult.OK) return;
//保存
var exportFileName = System.IO.Path.Combine(sfd.FileName.Substring(0, sfd.FileName.Length - 18), sfd.FileName.Substring(sfd.FileName.Length - 18) + ".csv"); using (var writer = new System.IO.StreamWriter(exportFileName, false, System.Text.Encoding.Default))
{
var csv = new SourceGrid.Exporter.CsvExporter();
csv.Export(grid, writer);
writer.Close();
MessageBox.Show("导出成功!");
} DevAge.Shell.Utilities.OpenFile(exportFileName);
}
catch (Exception ex)
{
MessageBox.Show("导出失败!" + ex.Message);
}

c# 表格控件SourceGrid使用总结的更多相关文章

  1. 如何在web中实现类似excel的表格控件

    Execl功能非常强大,内置的很多函数或公式可以大大提高对数据的加工处理能力.那么在web中有没有类似的控件呢?经过一番搜寻,发现handsontable具备了基本的excel功能支持公式,同时能对数 ...

  2. 最好的Angular2表格控件

    现在市面上有大量的JavaScript数据表格控件,包括开源的第三方的和自产自销的.可以说Wijmo的Flexgrid是目前适应Angular 2的最好的表格控件. Angular 2数据表格基本要求 ...

  3. C# DatrgridView表格控件的一些用法

    public class useDatrgrivView { string conn = null; string sqlComm = null; DataSet das = null; DataGr ...

  4. Silverlight项目笔记5:Oracle归档模式引起的异常&&表格控件绑定按钮

    1.Oracle归档模式产生日志文件引起数据库异常 连接数据库失败,提示监听错误,各种检查监听配置文件,删除再添加监听,无果. sqlplus下重启数据库数据库依然无果,期间碰到多个错误提示: ORA ...

  5. Android入门之GridView(表格控件)

    GridView是一个表格控件,可以在每个单元格中显示自定义的View或者字符串.在这里我们要实现一个图标下方有文字的效果. 1.首先我们应自定义布局文件image_text.xml.代码如下: &l ...

  6. 深入浅出ExtJS 第三章 表格控件

    3.1 表格的特性简介 >.Ext中的表格功能:包括排序/缓存/拖动/隐藏某一列/自动显示行号/列汇总/单元格编辑等实用功能; >.表格由类Ext.grid.GridPanel定义,继承自 ...

  7. ExtJS4.2学习(10)分组表格控件--GroupingGrid(转)

    鸣谢网址:http://www.shuyangyang.com.cn/jishuliangongfang/qianduanjishu/2013-11-17/179.html ------------- ...

  8. ExtJS4.2学习(九)属性表格控件PropertyGrid(转)

    鸣谢网址:http://www.shuyangyang.com.cn/jishuliangongfang/qianduanjishu/2013-11-15/178.html ------------- ...

  9. Ext入门学习系列(五)表格控件(3)

    上节学习了Ext中如何绑定服务器端传递的数据.分别用asp.net和asp.net MVC.PHP.XML为例.本节主要介绍绑定之后的分页功能. 一.Ext的表格控件如何绑定? 分页是Ext自带的一个 ...

随机推荐

  1. C++智能指针使用说明

    导读 STL提供四种智能指针:auto_ptr.unique_ptr.shared_ptr和weak_ptr.其中auto_ptr是C++98提供的解决方案,C++11以后均已摒弃.所有代码在gcc ...

  2. 「CTSC2006」歌唱王国

    概率生成函数\(g(x)=\sum_{i\geq 0}t_ix^i\),\(t_i\)表示结果为\(i\)的概率 令\(f(x)\)表示i位表示串结束时长度为i的概率,\(G(x)\)表示i位表示串长 ...

  3. Java中命名Dao、Bean、conn等包的含义(不定期补充)

    感谢大佬:https://blog.csdn.net/j904538808/article/details/78904732 (1)DAO是Data Access Object数据访问接口.数据访问: ...

  4. Java--面向对象设计

    [转载自本科老师上课课件] 问题一: 在一个软件的功能模块中,需要一种图像处理的功能.该图像处理的策略(如何处理)与图像的内容是相关的.如:卫星的运行图片,使用策略A处理方式,如果是卫星内云图片,则需 ...

  5. 人工智能与智能系统3-> 机器人学3 | 移动机器人平台

    机器人学的基本工具已经了解完毕,现在开始了解移动机器人,这部分包括机器人平台.导航.定位. 所谓机器人平台就是指机器人的物理结构及其驱动方式.本文将学习两种典型移动机器人平台(四旋翼和轮式车)的运动与 ...

  6. mysql表查询、多表查询(增强查询的使用)子查询、合并查询,外连接,mysql5种约束,自增长

    一.查询加强 1.在mysql中,日期类型可以直接比较,需要注意格式 2.%:表示0到多个字符, _:表示单个字符 exp:显示第二个字符为大写O的所有员工的姓名和工资 select  name fr ...

  7. Ubuntu18配置静态IP地址

    1. 记住网卡名称 ifconfig 2. 记住网关地址 netstat -rn 3. 配置静态IP 注意:Ubuntu18固定IP的方式跟Ubuntu18之前版本的的配置方式不同, Ubuntu18 ...

  8. PCI Verilog IP

    1      PCI IP设计 虽然PCI已经逐渐淘汰,但是还是有不少应用需要这样的接口通讯. 设计目的是为了提供基于源码的PCI IP,这样硬件就不必受限于某一个FPGA型号,也方便ASIC迁移.由 ...

  9. 《PHP程序员面试笔试宝典》——如何回答系统设计题?

    如何巧妙地回答面试官的问题? 本文摘自<PHP程序员面试笔试宝典> 应届生在面试时,偶尔也会遇到一些系统设计题,而这些题目往往只是测试求职者的知识面,或者测试求职者对系统架构方面的了解,一 ...

  10. MySQL架构原理之体系架构

    MySQL是最流行的关系型数据库软件之一,由于其体量小.速度快.开源免费.简单易用.维护成本低等,在季军架构中易于扩展.高可用等优势,深受开发者和企业的欢迎,在互联网行业广泛使用. 其系统架构如下: ...