随笔 - 8, 文章 - 25, 评论 - 26, 阅读 - 27116
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

Spread基本知识(一)

Posted on   醉春风  阅读(1624)  评论(0编辑  收藏  举报
1 取得当前行号、列号。
 

int row=e.Row;
int count=e.Count;
或者:
int rowindex = fpSpread1.ActiveSheet.ActiveRowIndex;
int columnindex = fpSpread1.ActiveSheet.ActiveColumnIndex;

2 单击一行变颜色。 

private void spdResult_CellClick(object sender, FarPoint.Win.Spread.CellClickEventArgs e)
  {
   //单击Spread列头时,什么也不处理

   if(!e.ColumnHeader)
   {
    if(spdResult.Sheets[0].Rows.Count!=0)
    {
     for(int i=0;i<spdResult.Sheets[0].Rows.Count;i++)
     {
      spdResult.Sheets[0].Rows[i].BackColor=System.Drawing.Color.White;
     }
     int row=e.Row;
     spdResult.Sheets[0].Rows[row].BackColor=System.Drawing.Color.FromArgb(((System.Byte)(192)), ((System.Byte)(255)), ((System.Byte)(255)));
    }
   }
  }

3 将Spread的单元格内容付值给一控件的Text

txtItemCD.Text = spdResult.Sheets[0].Cells[row,count].Text;

4 给Spread的指定单元格付值。

spdResult.Sheets[0].Cells[row,count].Text = txtItemCD.Text;

5 通过上下光标键改变选中行颜色

private void spdResult_LeaveCell(object sender, FarPoint.Win.Spread.LeaveCellEventArgs e)
  {
   //首先检查spread行数是否为0
   if(spdResult.Sheets[0].Rows.Count==0)
   {
    return;
   }
   else
   {
    for(int i=0;i<spdResult.Sheets[0].Rows.Count;i++)
    {
     spdResult.Sheets[0].Rows[i].BackColor=System.Drawing.Color.White;
    }
    int row=e.NewRow;
    spdResult.Sheets[0].Rows[row].BackColor=System.Drawing.Color.FromArgb(((System.Byte)(192)), ((System.Byte)(255)), ((System.Byte)(255)));
   }
  }

6 下拉列表加载数据(ComBobox) 
・ 列表追加(适合于数据量少的情况)

FarPoint.Win.Spread.CellType.ComboBoxCellType cb4 = new FarPoint.Win.Spread.CellType.ComboBoxCellType();
   cb4.ListWidth = 96;
   cb4.Editable = true;
   cb4.MaxDrop = 10;
   cb4.MaxLength = 1;
   string[] priceTagList = new string[]{" 0 无"," 1 有"};
   cb4.Items = priceTagList;
   this.spdSetList.ActiveSheet.Columns[4].CellType = cb4;

・ 从数据库追加

FarPoint.Win.Spread.CellType.ComboBoxCellType cb12 = new FarPoint.Win.Spread.CellType.ComboBoxCellType();
   cb12.ListWidth = 150;
   cb12.Editable = true;
   cb12.MaxDrop = 10;
   cb12.MaxLength = 8;

//dsEmployee:数据集Dataset,已经加载好数据的Dataset
   string[] employeeList = DataSetToArray(dsEmployee, 8);
   cb12.Items = employeeList;
   this.spdSetList.ActiveSheet.Columns[12].CellType = cb12;

private string[] DataSetToArray(DataSet ds, int BlankNum)
  {
   int i = 0;
   int NumLength = 0;
   string[] returnArray = new string[ds.Tables[0].Rows.Count];

   DataRow foundRows = ds.Tables[0].Rows[ds.Tables[0].Rows.Count -1];
   NumLength = foundRows[0].ToString().Length;

   foreach(DataRow dr in ds.Tables[0].Rows)
   {
    returnArray[i] = dr[0].ToString().PadLeft(BlankNum, ' ') + " " + dr[1].ToString();
    i++;
   }
   return returnArray;
  }

编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」
· Manus的开源复刻OpenManus初探
· 写一个简单的SQL生成工具
点击右上角即可分享
微信分享提示