博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
GridView控件操作
阅读量:5153 次
发布时间:2019-06-13

本文共 4409 字,大约阅读时间需要 14 分钟。

1:           /// 
2:          /// 初始化GridView-绑定一个空表格
3:          /// 
4:          /// 输入参数:表头信息,已逗号分开字符串
5:          /// 
6:          public static void iniNullGridView(string mystr, System.Web.UI.WebControls.GridView GV,int AddColNum)
7:          {
8:              DataTable dt = new DataTable();
9:              string[] columns = mystr.Split(',');
10:              foreach (string column in columns)
11:              {
12:                  dt.Columns.Add(column.ToString());
13:              }
14:              dt.Rows.Add(dt.NewRow());
15:              GV.DataSource = dt;
16:              GV.DataBind();
17:              int mycount = dt.Columns.Count;
18:              GV.Rows[0].Cells.Clear();
19:              GV.Rows[0].Cells.Add(new System.Web.UI.WebControls.TableCell());
20:              GV.Rows[0].Cells[0].ColumnSpan = mycount + AddColNum;
21:              GV.Rows[0].Cells[0].Text = "没有记录";
22:              GV.Rows[0].Cells[0].Style.Add("text-align", "center");
23:          }
24:          /// 
25:          /// 绑定数据到GridView
26:          /// 
27:          /// 输入参数:DataSet
28:          /// 
29:          public static void BindDataToGridView(DataSet myds, System.Web.UI.WebControls.GridView GV)
30:          {
31:              GV.DataSource = myds.Tables[0].DefaultView;
32:              GV.DataBind();
33:          }
34:          /// 
35:          /// GridView分组(把具有相同行的单元格合并)
36:          /// 
37:          /// 
38:          /// 按第几列分组
39:          public static void GroupRowsInGridView(System.Web.UI.WebControls.GridView GridView1, int cellNum)
40:          {
41:              int i = 0, rowSpanNum = 1;
42:              while (i < GridView1.Rows.Count - 1)
43:              {
44:                  System.Web.UI.WebControls.GridViewRow gvr = GridView1.Rows[i];
45:                  for (++i; i < GridView1.Rows.Count; i++)
46:                  {
47:                      System.Web.UI.WebControls.GridViewRow gvrNext = GridView1.Rows[i];
48:                      if (gvr.Cells[cellNum].Text == gvrNext.Cells[cellNum].Text)
49:                      {
50:                          gvrNext.Cells[cellNum].Visible = false;
51:                          rowSpanNum++;
52:                      }
53:                      else
54:                      {
55:                          gvr.Cells[cellNum].RowSpan = rowSpanNum;
56:                          rowSpanNum = 1;
57:                          break;
58:                      }
59:                      if (i == GridView1.Rows.Count - 1)
60:                      {
61:                          gvr.Cells[cellNum].RowSpan = rowSpanNum;
62:                      }
63:                  }
64:              }
65:          }
66:          /// 
67:          /// 每隔rowNum行合并(固定行)
68:          /// 
69:          /// 
70:          /// 第几列生效
71:          /// 每隔多少行合并,这里是行变量
72:          public static void GroupRowsInGridView(System.Web.UI.WebControls.GridView GridView1, int cellNum, int rowNum)
73:          {
74:              int i = 0;
75:              while (i < GridView1.Rows.Count - 1)
76:              {
77:                  System.Web.UI.WebControls.GridViewRow[] gvr = new System.Web.UI.WebControls.GridViewRow[rowNum];
78:                  for (int j = 0; j <= rowNum - 1; j++)
79:                  {
80:                      gvr[j] = GridView1.Rows[i + j];
81:                      if (j != 0)
82:                          gvr[j].Cells[cellNum].Visible = false;
83:                  }
84:                  gvr[0].Cells[cellNum].RowSpan = rowNum;
85:                  i = i + rowNum;
86:              }
87:          }
88:          /// 
89:          /// 鼠标滑过后,GridView的颜色变化
90:          /// 
91:          /// 
92:          /// 
93:          public static void GroupColorInGridView(System.Web.UI.WebControls.GridView gv, System.Web.UI.WebControls.GridViewRowEventArgs e)
94:          {
95:              int i;
96:              for (i = -1; i < gv.Rows.Count; i++)
97:              {
98:                  if (e.Row.RowType == System.Web.UI.WebControls.DataControlRowType.DataRow)
99:                  {
100:                      e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#ffbbbb'");
101:                      e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
102:                  }
103:              }
104:          }

转载于:https://www.cnblogs.com/chenamo5776/archive/2011/11/13/2247127.html

你可能感兴趣的文章
dfs/bfs(转载)
查看>>
小生功能贴<一> --- 动态添加应用 具有长按删除功能
查看>>
【Apach shiro】spring 整合Apache shiro
查看>>
java获取当前上一周、上一月、上一年的时间
查看>>
图片代替滚动条 JS文件2
查看>>
HTML 学习笔记
查看>>
多项目上传文件解决方案之:Flash上传插件
查看>>
Linux相关学习笔记-文件系统
查看>>
《linux 内核全然剖析》sched.c sched.h 代码分析笔记
查看>>
Android学习之——优化篇(2)
查看>>
好的开源库总结
查看>>
Winform的Bitmap调色板的一个问题
查看>>
noframes,frame,iframe,frameset 区别
查看>>
Android统计绘图工具
查看>>
Isequal IsequalToString containsString hasPrefixd的区别
查看>>
【原】关于cuteftp连不上Linux虚拟机的问题
查看>>
大众点评cat系统的搭建笔记
查看>>
[svc]sort-uniq
查看>>
[svc]mysql备份恢复及常用命令
查看>>
mysql存储引擎之MyISAM 和 InnoDB的比较
查看>>