这个问题可以这样做:
1)建立数据库视图或存储过程,将O,1,2改成0--完成,1--进行中,2--未开工,这个很容易,而且最好,建议你使用这种方式,而且这种方式基本上不用该程序代码,只要把原来的表名改成现在的视图名就可以了
2)在Grid的DataItemCreate或ItemDateBind事件(大概是这个名)中填写代码,判断该列的值,然后设置,这种方式下你还可以设置不同的背景色,如果完成的一行设为红色,进行中的设为绿色,没开工的设为黄色
1)建立数据库视图或存储过程,将O,1,2改成0--完成,1--进行中,2--未开工,这个很容易,而且最好,建议你使用这种方式,而且这种方式基本上不用该程序代码,只要把原来的表名改成现在的视图名就可以了
2)在Grid的DataItemCreate或ItemDateBind事件(大概是这个名)中填写代码,判断该列的值,然后设置,这种方式下你还可以设置不同的背景色,如果完成的一行设为红色,进行中的设为绿色,没开工的设为黄色
解决方案 »
- 生成解决方案报错
- 做好一个网站之后.....怎么办
- 页面后台方法调用
- 求创建access的命令 c#中
- -->高难问题:在添加新的ASPX页面时,怎样让后台的CODE自动继承自己写的基类.
- ASP.NET URL重写问题
- 散分200庆祝自己在新公司第一天上班
- 在DataGrid中,如何要让每行的LinkButton根据不同行的情况来显示或者灰化,我该怎么做啊???大虾
- 关于ASP.NET OA界面控制的问题
- 使用了RequiredFieldValidator验证控件后,怎样清除TextBox控件中的内容
- 各位大哥,能告诉我左边的treeview的关闭按钮是怎么做出来的么?怎么样才能作出那样的效果?
- 思归,和微软专家看过来,一个简单问题----------在线等,马上给分!!!
其实你所说的第一个方法使用存储过程我也想到了,不过我们经理不让我用,他说是便于以后系统的移值,没办法,对于你所说的ItemDataBound事件,我还不是很会用,要怎么在这个事件中取到模板中的控件,又如何设置它的文本,(因为模板中Label控件的ID都是服务器分配的),还有如何取出当前值的itend值,能不能附上一段代码好吗?对了,如果不介意留下你们的MSN可以交个朋友好不..再次非常感谢你们...拜托了.
我们一般把这些对应表单独存在一张表中叫做代码表。在需要用到的像上述的对应关系,存入主表中的是它的值0,1,2,其该值和对应值中文意思再代码表中。
当需要用DataGrid显示时候,不需要用Template,直接在CS中用方法转化过来
或者在DataSet取得值时,如查询条件:"select name,DMToDMNR(xb) as xb
from userinfo",其中DMToDMNR是在SQL Server 自定义的函数,很简单的函数从代码表中输入它的值,得到它的对应值就行了
然后DataGrid中,字段帮定还是一样,name,xb等
WHEN 1 THEN '进行中'
WHEN 2 THEN '未开工'
ELSE '完成'
END AS send_status
这个语句在SQL Server中可以使用,不过在其它数据库服务器中可以使用吗?有没有可以在页面实现的方法呢?
<%@ Page Inherits="MyDataGridPage" src="Datagrid.cs" %>
<html>
<body>
<h3><font face="Verdana">Curstomize Datagrid</font></h3>
<form runat="server">
<ASP:DataGrid id="MyDataGrid" runat="server">
<Columns>
<asp:TemplateColumn>
<HeaderTemplate><b>Name</b></HeaderTemplate>
<ItemTemplate>
<asp:Label runat="server"/>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
<asp:button id="Button1" runat="Server" Text="click" />
</form>
</body>
</html>
</html>2. datagrid.cs:
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;public class MyDataGridPage : Page
{
protected DataGrid MyDataGrid;
protected override void OnLoad(EventArgs e)
{
if (!IsPostBack)
{
string sConn = "server=localhost;database=pubs;uid=sa;pwd=;";
SqlDataAdapter myDA = new SqlDataAdapter("select * from authors", sConn); DataSet ds = new DataSet();
myDA.Fill(ds, "authors"); DataView dv = ds.Tables["authors"].DefaultView;
MyDataGrid.AutoGenerateColumns = false;
MyDataGrid.ItemDataBound += new DataGridItemEventHandler(OnItemDataBound); MyDataGrid.DataSource= dv;
MyDataGrid.DataBind();
}
} string[] sTaskStatus = {"完成","进行中","未开工"}; protected void OnItemDataBound(object sender, DataGridItemEventArgs e)
{
if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
string sID = DataBinder.Eval(e.Item.DataItem, "au_id" ).ToString();
int n = Convert.ToInt32(sID.Substring(0,3)); ControlCollection cc = e.Item.Cells[0].Controls;
if (cc.Count > 0)
{
for (int i=0; i < cc.Count; i++)
{
if (cc[i] != null && cc[i] is Label)
{
((Label)cc[i]).Text = (sTaskStatus[n%3]) + ":" + sID;
}
}
}
}
}
}
真的很感谢你,照你的方法的确可以,只是我还有两处不明,还望解答:
1:MyDataGrid.ItemDataBound += new DataGridItemEventHandler(OnItemDataBound);
这一句是什么意思,有什么用处呢?DataGrid不是有ItemDataBound事件吗?为什么还要加一个呢>2. if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
这一句是什么函意啊?3.假如同一个模板内有两个不同的控件都要赋值怎么办呢??>谢谢啦!!!!
<asp:datagrid OnItemDataBound=".." ...>
then just remove that line2. DataGrid is displayed as a HTML table, it could have Header, Footer, Pager, Item, AlternatingItem,... normally, ListItemType.Item means row 1,3,5,...and ListItemType.AlternatingItem means row 2,4,6,....the if statement just checks if you are dealing with the table body3. go through
e.Item.Cells[0],e.Item.Cells[1],...,e.Item.Cells[e.Item.Cells.Count-1]