C1FlexGrid 是 .NET 平台中性能最佳的表格控件,常用于数据显示和数据编辑操作,今天主要讲解如何在C1FlexGrid 表格控件中增加自定义的编辑和删除列。

首先,我们加载 C1FlexGrid 的数据源,代码如下:

    List<Item> list = new List<Item>();
 
    list.Add(new Item() { ID = 1, Code = "A10001", Name = "产品A1", Description = "产品A1" });
    list.Add(new Item() { ID = 2, Code = "A10002", Name = "产品A2", Description = "产品A2" });
    list.Add(new Item() { ID = 3, Code = "A10003", Name = "产品A3", Description = "产品A3" });
    list.Add(new Item() { ID = 4, Code = "A10004", Name = "产品A4", Description = "产品A4" });
    list.Add(new Item() { ID = 5, Code = "A10005", Name = "产品A5", Description = "产品A5" });
 
    c1FlexGrid1.DataSource = list;

 

然后,添加编辑列和删除列,并设置这两列中显示的图标,代码如下:

    c1FlexGrid1.Cols.Add(2);
    c1FlexGrid1.Cols[5].Width = 20;
    c1FlexGrid1.Cols[5].AllowEditing = false;
    c1FlexGrid1.Cols[6].Width = 20;
    c1FlexGrid1.Cols[6].AllowEditing = false;
 
    for (int row = 1; row < c1FlexGrid1.Rows.Count; row++)
    {
        c1FlexGrid1.SetCellImage(row, 5, Image.FromFile(@"Images\Edit.png"));
        c1FlexGrid1.SetCellImage(row, 6, Image.FromFile(@"Images\Delete.png"));
    }

 

最后在 C1Flexgrid 的 MouseClick 事件中判断当前点击的是那一列,并执行相应的操作,代码如下:

    void c1FlexGrid1_MouseClick(object sender, MouseEventArgs e)
    {
        switch (c1FlexGrid1.Col)
        {
            case 5:
                MessageBox.Show(string.Format("编辑产品:{0}",c1FlexGrid1[c1FlexGrid1.Row,2]));
                break;  
            case 6:
                MessageBox.Show(string.Format("删除产品:{0}", c1FlexGrid1[c1FlexGrid1.Row, 2]));
                break;
            default:
                break;
        }
    }

 

运行截图:

C1FlexGrid

 

源码下载:VS2010 + C1 2013v1