专注

导航

GridView控件操作

   1:           /// <summary>
   2:          /// 初始化GridView-绑定一个空表格
   3:          /// </summary>
   4:          /// <param name="mystr">输入参数:表头信息,已逗号分开字符串</param>
   5:          /// <param name="GV"></param>
   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:          /// <summary>
  25:          /// 绑定数据到GridView
  26:          /// </summary>
  27:          /// <param name="myds">输入参数:DataSet</param>
  28:          /// <param name="GV"></param>
  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:          /// <summary>
  35:          /// GridView分组(把具有相同行的单元格合并)
  36:          /// </summary>
  37:          /// <param name="GridView1"></param>
  38:          /// <param name="cellNum">按第几列分组</param>
  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:          /// <summary>
  67:          /// 每隔rowNum行合并(固定行)
  68:          /// </summary>
  69:          /// <param name="GridView1"></param>
  70:          /// <param name="cellNum">第几列生效</param>
  71:          /// <param name="rowNum">每隔多少行合并,这里是行变量</param>
  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:          /// <summary>
  89:          /// 鼠标滑过后,GridView的颜色变化
  90:          /// </summary>
  91:          /// <param name="gv"></param>
  92:          /// <param name="e"></param>
  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:          }

posted on 2011-11-13 11:57  陈啊M  阅读(226)  评论(0编辑  收藏  举报