您的位置:首页 > 编程语言 > ASP

【ASP.NET】GridView中的技巧收集

2009-02-11 16:04 281 查看
1.列表中数字变字符的简易方法

我们在程序中需要对列表控件Datagrid、Gridview等进行绑定时,有时需要将具体特定含义的数字变成字符,我将自己处理的一种方式贴出来做个记录。

原理:通过增加新的一列,根据数字列的关系,在新列中赋字符或者汉字。然后绑定新列。

代码:

前台:

<asp:BoundField DataField="statueName" HeaderText="报表状态" SortExpression="statueName">

<itemstyle horizontalalign="center" />

</asp:BoundField>

后台:

#region 绑定列表事件

protected void BindData1()

{

string where = "";

if (!string.IsNullOrEmpty(txtEnterpriseName.Text.Trim()))

{

where += " and a.Enterprise_Code like '%" + txtEnterpriseName.Text.Trim() + "%' or a.EnterpriseName like '%" + txtEnterpriseName.Text.Trim() + "%' ";

}

DataSet ds = DalHelper.MonthlyEnterpriseMonitor.GetList(where);

ds.Tables[0].Columns.Add("statueName",typeof(String));

for (int i = 0; i < ds.Tables[0].Rows.Count; i++)

{

switch (ds.Tables[0].Rows[i]["Status"].ToString())

{

//1 已报未审

case "1":

ds.Tables[0].Rows[i]["statueName"] = "已报未审";

break;

//2 已报打回

case "2":

ds.Tables[0].Rows[i]["statueName"] = "已报打回";

break;

//3.修改重报

case "3":

ds.Tables[0].Rows[i]["statueName"] = "修改重报";

break;

//4.已报通过

case "4":

ds.Tables[0].Rows[i]["statueName"] = "已报通过";

break;

}

}

this.gridView.DataSetSource = ds;

this.gridView.DataBind();

}

#endregion

2.前台调用后台的方法,并将前台参数带入

前台:

<asp:TemplateField HeaderText="操作" ShowHeader="False">

<itemtemplate>

<a href="#" style="display: <%# GetStatus(Eval("statueName"))%> "><img border="0" alt="" height="15" src="../images/houtai_42.gif" width="16" /></a>

</itemtemplate>

</asp:TemplateField>

后台:

protected string GetStatus(object data)

{

string ReturnValue = "none";

if (data != null)

{

if (data.ToString() == "已报打回")

{

ReturnValue = "block";

}

}

ReturnValue;

}

3.GridView不換行

1.

ItemsGrid.Attributes.Add("style", "word-break:keep-all;word-wrap:normal")

2.

在绑定数据的“编辑列”中将某列的HeaderStyle和ItemStyle的Wrap设为false.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: