找回密码
 立即注册

QQ登录

只需一步,快速开始

lit

注册会员

2

主题

7

帖子

44

积分

注册会员

积分
44

活字格认证

最新发帖
lit
注册会员   /  发表于:2012-7-11 15:41  /   查看:5450  /  回复:5
我用的是spread5 for winform.

我在设计器中将表格的某一列的celltype设为ComboBox类型,现在我从数据库得到一个datatable,在datatable中有一列是1或2,我想通过绑定数据源的方式来实现,当datatable中的该列是1时,在表格中显示“是”,当为2时,显示“否”

请问我该怎么做?
谢谢

5 个回复

倒序浏览
ZenosZeng讲师达人认证 悬赏达人认证
超级版主   /  发表于:2012-7-11 15:50:00
沙发
可以给这一列设置一个ComboBoxCellType,代码如下:
  1.      string[] cbstr;
  2.      cbstr = new String[] {"是", "否"};
  3.      string[] strval;
  4.      strval = new String[] {"1", "2"};
  5.      combo.Items = cbstr;
  6.      combo.ItemData = strval;
  7.      combo.EditorValue = FarPoint.Win.Spread.CellType.EditorValue.ItemData;
  8.      fpSpread1.Sheets[0].Columns[0].CellType = combo;
复制代码
回复 使用道具 举报
lit
注册会员   /  发表于:2012-7-11 16:20:00
板凳
谢谢dof,可以实现了,我还有一个问题就是:
按照你上面的方法可以实现绑定,但是combobox的下拉按钮还是显示出来了,
我现在想实现这样的功能:
1.当查询时(即绑定上数据源),只显示“是”或“否”,而不显示combobox的下拉按钮;
2.当双击该单元格时,使该单元格出现下来按钮,以供用户选择,然后再将“1”或“2”存入数据库。

先提前谢谢了
回复 使用道具 举报
ZenosZeng讲师达人认证 悬赏达人认证
超级版主   /  发表于:2012-7-11 17:10:00
地板
请先使用下面的代码进行测试,主要是设置Columns[0].Editor和Renderer属性为不同类型:
  1. namespace _5164_ComboBox
  2. {
  3.     public partial class Form1 : Form
  4.     {
  5.         public Form1()
  6.         {
  7.             InitializeComponent();
  8.         }

  9.         private void Form1_Load(object sender, EventArgs e)
  10.         {
  11.             FarPoint.Win.Spread.CellType.ComboBoxCellType combo = new FarPoint.Win.Spread.CellType.ComboBoxCellType();
  12.             string[] cbstr;
  13.             cbstr = new String[] { "是", "否" };
  14.             string[] strval;
  15.             strval = new String[] { "1", "2" };
  16.             combo.Items = cbstr;
  17.             combo.ItemData = strval;
  18.             combo.EditorValue = FarPoint.Win.Spread.CellType.EditorValue.ItemData;
  19.             fpSpread1.Sheets[0].Columns[0].Editor = combo;
  20.             fpSpread1.Sheets[0].Columns[0].Renderer  = new MyCellType();

  21.             fpSpread1.ActiveSheet.Cells[0, 0].Value = 1;
  22.             fpSpread1.ActiveSheet.Cells[1, 0].Value = 2;
  23.             fpSpread1.ActiveSheet.Cells[2, 0].Value = 1;
  24.             fpSpread1.ActiveSheet.Cells[3, 0].Value = 2;
  25.         }
  26.     }

  27.     public class MyCellType : FarPoint.Win.Spread.CellType.GeneralCellType
  28.     {
  29.         public override void PaintCell(Graphics g, Rectangle r, FarPoint.Win.Spread.Appearance appearance, object value, bool isSelected, bool isLocked, float zoomFactor)
  30.         {
  31.             if (value != null)
  32.             {
  33.                 if (value.ToString() == "1")
  34.                 {
  35.                     value = "是";
  36.                 }
  37.                 else if(value.ToString() == "2")
  38.                 {
  39.                     value = "否";
  40.                 }
  41.             }
  42.             base.PaintCell(g, r, appearance, value, isSelected, isLocked, zoomFactor);
  43.         }
  44.     }
  45. }
复制代码
回复 使用道具 举报
lit
注册会员   /  发表于:2012-7-12 10:39:00
5#
谢谢dof
回复 使用道具 举报
ZenosZeng讲师达人认证 悬赏达人认证
超级版主   /  发表于:2012-7-12 11:07:00
6#
不客气,这都是我们应该做的。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 立即注册
返回顶部