1、后台动态生成链接列 传递一个参数。(HyperLinkField 不支持多参数传值)
代码
HyperLinkField hlf = new HyperLinkField();
hlf.HeaderText = "诊断";
////string[] canshu = new string[4] { "stationid", "datetime", "devicetypename", "objid" };
////hlf.DataNavigateUrlFields = canshu;
////hlf.DataNavigateUrlFormatString = "~/diagnose/DiagnoseGeneral.aspx?stationid={0}&datetime={1}&devicetypename={2}&objid={3}";
hlf.DataNavigateUrlFormatString = "~/diagnose/DiagnoseGeneral.aspx?stationid={0}";
hlf.Text = "诊断";
gv.Columns.Add(hlf);
hlf.HeaderText = "诊断";
////string[] canshu = new string[4] { "stationid", "datetime", "devicetypename", "objid" };
////hlf.DataNavigateUrlFields = canshu;
////hlf.DataNavigateUrlFormatString = "~/diagnose/DiagnoseGeneral.aspx?stationid={0}&datetime={1}&devicetypename={2}&objid={3}";
hlf.DataNavigateUrlFormatString = "~/diagnose/DiagnoseGeneral.aspx?stationid={0}";
hlf.Text = "诊断";
gv.Columns.Add(hlf);
2、后台动态生成模板列 传递多个个参数。
(1)写代码生成模板列(选自孟子E章)
模板列类代码
1 public class GridViewTemplate : ITemplate
2 {
3 private DataControlRowType templateType;
4 private string columnName;
5
6 public GridViewTemplate(DataControlRowType type, string colname)
7 {
8 templateType = type;
9 columnName = colname;
10 }
11 public void InstantiateIn(System.Web.UI.Control container)
12 {
13 switch (templateType)
14 {
15 case DataControlRowType.Header:
16 Literal lc = new Literal();
17 lc.Text = columnName;
18 container.Controls.Add(lc);
19 break;
20 case DataControlRowType.DataRow:
21 HyperLink link = new HyperLink();
22 link.ID = "linkId";
23 link.Text = "诊断";
24 container.Controls.Add(link);
25 break;
26 default:
27 break;
28 }
29 }
30 }
2 {
3 private DataControlRowType templateType;
4 private string columnName;
5
6 public GridViewTemplate(DataControlRowType type, string colname)
7 {
8 templateType = type;
9 columnName = colname;
10 }
11 public void InstantiateIn(System.Web.UI.Control container)
12 {
13 switch (templateType)
14 {
15 case DataControlRowType.Header:
16 Literal lc = new Literal();
17 lc.Text = columnName;
18 container.Controls.Add(lc);
19 break;
20 case DataControlRowType.DataRow:
21 HyperLink link = new HyperLink();
22 link.ID = "linkId";
23 link.Text = "诊断";
24 container.Controls.Add(link);
25 break;
26 default:
27 break;
28 }
29 }
30 }
(2)调用模板列类生成模板列
代码
1 private void TreeViewBindMethod(GridView gv)
2 {
3 TemplateField tf = new TemplateField();
4 tf.HeaderTemplate = new GridViewTemplate(DataControlRowType.Header,"诊断");
5 tf.ItemTemplate = new GridViewTemplate(DataControlRowType.DataRow, "诊断");
6 gv.Columns.Add(tf);
7 }
2 {
3 TemplateField tf = new TemplateField();
4 tf.HeaderTemplate = new GridViewTemplate(DataControlRowType.Header,"诊断");
5 tf.ItemTemplate = new GridViewTemplate(DataControlRowType.DataRow, "诊断");
6 gv.Columns.Add(tf);
7 }
(3)传递多参数
多参数传递代码
1 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
2 {
3 //首先判断是否是数据行
4 if (e.Row.RowType == DataControlRowType.DataRow)
5 {
6 //生成链接列
7 DataRowView gv = (DataRowView)e.Row.DataItem;
8 HyperLink link = (HyperLink)e.Row.FindControl("linkId");
9 string url = "../diagnose/DiagnoseGeneral.aspx";
10 url += "?stationid=" + gv.Row["stationid"].ToString().Trim() + "&datetime=" + gv.Row["datetime"].ToString().Trim() + "&devicetypename=" + gv.Row["devicetypename"].ToString().Trim() + "&objid=" + gv.Row["objid"].ToString().Trim();
11 link.NavigateUrl = url;
12 }
2 {
3 //首先判断是否是数据行
4 if (e.Row.RowType == DataControlRowType.DataRow)
5 {
6 //生成链接列
7 DataRowView gv = (DataRowView)e.Row.DataItem;
8 HyperLink link = (HyperLink)e.Row.FindControl("linkId");
9 string url = "../diagnose/DiagnoseGeneral.aspx";
10 url += "?stationid=" + gv.Row["stationid"].ToString().Trim() + "&datetime=" + gv.Row["datetime"].ToString().Trim() + "&devicetypename=" + gv.Row["devicetypename"].ToString().Trim() + "&objid=" + gv.Row["objid"].ToString().Trim();
11 link.NavigateUrl = url;
12 }
3、改变gridview-cell的值。
代码
1 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
2 {
3 //首先判断是否是数据行
4 if (e.Row.RowType == DataControlRowType.DataRow)
5 {
6 //修改Cell中的值
7 foreach (TableCell cell in e.Row.Cells)
8 {
9 if (cell.Text.Trim() == "-12121")
10 {
11 cell.Text = "-";
12 }
13 }
14 }
15 }
2 {
3 //首先判断是否是数据行
4 if (e.Row.RowType == DataControlRowType.DataRow)
5 {
6 //修改Cell中的值
7 foreach (TableCell cell in e.Row.Cells)
8 {
9 if (cell.Text.Trim() == "-12121")
10 {
11 cell.Text = "-";
12 }
13 }
14 }
15 }
4、鼠标滑过的行,颜色改变
代码
1 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
2 {
3 //首先判断是否是数据行
4 if (e.Row.RowType == DataControlRowType.DataRow)
5 {
6 //鼠标点上时背景颜色
7 e.Row.Attributes.Add("onmouseover", "color=this.style.backgroundColor;this.style.backgroundColor='DodgerBlue'");
8 //当鼠标移开时还原背景色
9 e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=color");
10 }
11 }
2 {
3 //首先判断是否是数据行
4 if (e.Row.RowType == DataControlRowType.DataRow)
5 {
6 //鼠标点上时背景颜色
7 e.Row.Attributes.Add("onmouseover", "color=this.style.backgroundColor;this.style.backgroundColor='DodgerBlue'");
8 //当鼠标移开时还原背景色
9 e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=color");
10 }
11 }
5、改变某列的Cell颜色--设置比较值----大于 小于 等于 某个值。
代码
1 /// <summary>
2 /// 设定比较值 把数据大于或小于或等于某个值的字段改变颜色
3 /// 2010 05 03
4 /// </summary>
5 /// <param name="dt"></param>
6 /// <param name=""></param>
7 private void ChangeColor(DataTable dt)
8 {
9 for (int rowNum = 0; rowNum <= GridView1.Rows.Count - 1; rowNum++)
10 {
11 DataRowView drv = dt.DefaultView[rowNum];
12 double valueOfH2 = Convert.ToDouble(drv["H2"]);
13 if (valueOfH2 > 23.0)
14 {
15 GridView1.Rows[rowNum].Cells[3].BackColor = System.Drawing.Color.Green;
16 }
17 }
18 }
2 /// 设定比较值 把数据大于或小于或等于某个值的字段改变颜色
3 /// 2010 05 03
4 /// </summary>
5 /// <param name="dt"></param>
6 /// <param name=""></param>
7 private void ChangeColor(DataTable dt)
8 {
9 for (int rowNum = 0; rowNum <= GridView1.Rows.Count - 1; rowNum++)
10 {
11 DataRowView drv = dt.DefaultView[rowNum];
12 double valueOfH2 = Convert.ToDouble(drv["H2"]);
13 if (valueOfH2 > 23.0)
14 {
15 GridView1.Rows[rowNum].Cells[3].BackColor = System.Drawing.Color.Green;
16 }
17 }
18 }
6、后台代码生成绑定列。
代码
1 /// <summary>
2 /// 生成Gridview的绑定列
3 /// </summary>
4 /// <param name="gv"></param>
5 private void TreeViewBindMethod(GridView gv)
6 {
7 gv.Columns.Clear();
8 BoundField fieldStationName = new BoundField();
9 fieldStationName.DataField = "STATIONID";
10 fieldStationName.HeaderText = "变电站名称";
11 fieldStationName.Visible = false;
12 gv.Columns.Add(fieldStationName);
13 BoundField fieldDeviceName = new BoundField();
14 fieldDeviceName.DataField = "devicetypename";
15 fieldDeviceName.HeaderText = "设备名称";
16 fieldDeviceName.Visible = false;
17 gv.Columns.Add(fieldDeviceName);
18 BoundField fieldObjName = new BoundField();
19 fieldObjName.DataField = "objid";
20 fieldObjName.HeaderText = "主设备名称";
21 fieldObjName.Visible = false;
22 gv.Columns.Add(fieldObjName);
23 BoundField fieldDateTime = new BoundField();
24 fieldDateTime.DataField = "datetime";
25 fieldDateTime.HeaderText = "时间";
26 gv.Columns.Add(fieldDateTime);
27 if (dtDga != null && dtDga.Rows.Count > 0)
28 {
29 for (int i = 0; i < dtDga.Rows.Count; i++)
30 {
31 BoundField field = new BoundField();
32 field.DataField = dtDga.Rows[i][1].ToString().Trim();
33 field.HeaderText = dtDga.Rows[i][1].ToString().Trim();
34 gv.Columns.Add(field);
35 }
36 }
37 TemplateField tf = new TemplateField();
38 tf.HeaderTemplate = new GridViewTemplate(DataControlRowType.Header,"诊断");
39 tf.ItemTemplate = new GridViewTemplate(DataControlRowType.DataRow, "诊断");
40 gv.Columns.Add(tf);
41 }
2 /// 生成Gridview的绑定列
3 /// </summary>
4 /// <param name="gv"></param>
5 private void TreeViewBindMethod(GridView gv)
6 {
7 gv.Columns.Clear();
8 BoundField fieldStationName = new BoundField();
9 fieldStationName.DataField = "STATIONID";
10 fieldStationName.HeaderText = "变电站名称";
11 fieldStationName.Visible = false;
12 gv.Columns.Add(fieldStationName);
13 BoundField fieldDeviceName = new BoundField();
14 fieldDeviceName.DataField = "devicetypename";
15 fieldDeviceName.HeaderText = "设备名称";
16 fieldDeviceName.Visible = false;
17 gv.Columns.Add(fieldDeviceName);
18 BoundField fieldObjName = new BoundField();
19 fieldObjName.DataField = "objid";
20 fieldObjName.HeaderText = "主设备名称";
21 fieldObjName.Visible = false;
22 gv.Columns.Add(fieldObjName);
23 BoundField fieldDateTime = new BoundField();
24 fieldDateTime.DataField = "datetime";
25 fieldDateTime.HeaderText = "时间";
26 gv.Columns.Add(fieldDateTime);
27 if (dtDga != null && dtDga.Rows.Count > 0)
28 {
29 for (int i = 0; i < dtDga.Rows.Count; i++)
30 {
31 BoundField field = new BoundField();
32 field.DataField = dtDga.Rows[i][1].ToString().Trim();
33 field.HeaderText = dtDga.Rows[i][1].ToString().Trim();
34 gv.Columns.Add(field);
35 }
36 }
37 TemplateField tf = new TemplateField();
38 tf.HeaderTemplate = new GridViewTemplate(DataControlRowType.Header,"诊断");
39 tf.ItemTemplate = new GridViewTemplate(DataControlRowType.DataRow, "诊断");
40 gv.Columns.Add(tf);
41 }
7、合并GridView中某列相同信息的行
代码
1 ///<summary>
2 /// 合并GridView中某列相同信息的行(单元格)
3 /// </summary>
4 /// <param name="GridView1">GridView</param>
5 /// <param name="cellNum">第几列</param>
6 public static void GroupRows(GridView GridView1, int cellNum)
7 {
8 int i = 0, rowNum = 1;
9 while (i < GridView1.Rows.Count - 1)
10 {
11 GridViewRow gvr = GridView1.Rows[i];
12
13 for (++i; i < GridView1.Rows.Count; i++)
14 {
15 GridViewRow gvrNext = GridView1.Rows[i];
16 if (gvr.Cells[cellNum].Text == gvrNext.Cells[cellNum].Text)
17 {
18 gvrNext.Cells[cellNum].Visible = false;
19 rowNum++;
20 }
21 else
22 {
23 gvr.Cells[cellNum].RowSpan = rowNum;
24 rowNum = 1;
25 break;
26 }
27
28 if (i == GridView1.Rows.Count - 1)
29 {
30 gvr.Cells[cellNum].RowSpan = rowNum;
31 }
32 }
33 }
34 }
2 /// 合并GridView中某列相同信息的行(单元格)
3 /// </summary>
4 /// <param name="GridView1">GridView</param>
5 /// <param name="cellNum">第几列</param>
6 public static void GroupRows(GridView GridView1, int cellNum)
7 {
8 int i = 0, rowNum = 1;
9 while (i < GridView1.Rows.Count - 1)
10 {
11 GridViewRow gvr = GridView1.Rows[i];
12
13 for (++i; i < GridView1.Rows.Count; i++)
14 {
15 GridViewRow gvrNext = GridView1.Rows[i];
16 if (gvr.Cells[cellNum].Text == gvrNext.Cells[cellNum].Text)
17 {
18 gvrNext.Cells[cellNum].Visible = false;
19 rowNum++;
20 }
21 else
22 {
23 gvr.Cells[cellNum].RowSpan = rowNum;
24 rowNum = 1;
25 break;
26 }
27
28 if (i == GridView1.Rows.Count - 1)
29 {
30 gvr.Cells[cellNum].RowSpan = rowNum;
31 }
32 }
33 }
34 }
8、前台使用HyperLinkField传递多参数
<asp:HyperLinkField DataNavigateUrlFields="BDZID,SBID" DataNavigateUrlFormatString="Default.aspx?bdzid={0}&sbbm={1}"
HeaderText="详细信息" Target="_blank" Text="详细信息" />
HeaderText="详细信息" Target="_blank" Text="详细信息" />
9、自带分页写法
1 protected void GVReal_PageIndexChanging(object sender, GridViewPageEventArgs e)
2 {
3 this.GVReal.PageIndex = e.NewPageIndex;
4 }
2 {
3 this.GVReal.PageIndex = e.NewPageIndex;
4 }
10、单元格内容是否换行
fieldDateTime.ItemStyle.Wrap = false; //单元格内的内容不换行
11.链接传递中文乱码
<a href='WebUpdateCC.aspx?name=<%# System.Web.HttpUtility.UrlEncode((DataBinder.Eval(Container.DataItem,"Name")).ToString())%>' target="_blank">修改</a>