【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.
我们在程序中需要对列表控件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.
相关文章推荐
- Asp.net开发常用技巧收集二
- ASP.NET中GridView控件常用技巧
- 测试ASP.NET 2.0中Gridview控件高级技巧
- Asp.net技巧:gridview获取当前行索引的方法
- asp.netGridView使用技巧
- ASP.NET 2.0中Gridview控件技巧(1)
- 【转】GridView技巧【ASP.NET GRIDVIEW】
- ASP.NET jQuery 食谱19 (通过jQuery操作GridView技巧集合)(转)
- asp.net开发常用技巧收集
- Asp.net开发常用技巧收集
- asp.net开发常用技巧收集
- ASP.NET 2.0中Gridview控件高级技巧
- ASP.NET技巧:使用Gridview绑定数据库中的图片
- asp.net开发常用技巧收集
- 【ASP.NET】GRIDVIEW控件的一般使用技巧
- asp.net性能的技巧[收集资料]
- ASP.NET中Gridview控件高级技巧详细分析
- ASP.NET 2.0中Gridview控件高级技巧
- ASP.net技巧:DataList、Gridview主从表明细显示
- ASP.NET 2.0中Gridview控件高级技巧