最近做一个任务列表的项目,里面有一TaskStatus列,值为0或者1,在用GridView进行数据绑定的时候,转换0为“未完成”,1为“已完成”,并且把相对应的color设置为red和green。可是在进行数据绑定的时候却没有获取到那个值。如果直接显示0和1是可以的。
只是绑定事件的代码
Code
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow && e.Row.RowState != DataControlRowState.Edit)
{
Int32 istatus = Convert.ToInt32(e.Row.Cells[2].Text);//这里的这个e.Row.Cells[2].Text获取到的就是"";,没有值
if (istatus == 0)
{
e.Row.Cells[2].Text = "未完成";
e.Row.Cells[2].Style.Add("color", "red");
}
else if (istatus == 1)
{
e.Row.Cells[2].Text = "已完成";
e.Row.Cells[2].Style.Add("color", "green");
}
}
}
这里是GridView的源码
Code
<asp:GridView ID="GridView1" runat="server" CellPadding="4" DataKeyNames="id"
ForeColor="#333333" GridLines="None" Width="797px" AutoGenerateColumns="False"
onrowdatabound="GridView1_RowDataBound"
onselectedindexchanged="GridView1_SelectedIndexChanged"
style="text-align: left" Height="194px"
onrowcommand="GridView1_RowCommand" onrowediting="GridView1_RowEditing"
onrowcancelingedit="GridView1_RowCancelingEdit"
onrowupdated="GridView1_RowUpdated" onrowupdating="GridView1_RowUpdating"
EnableViewState="False" onrowdeleting="GridView1_RowDeleting">
<RowStyle BackColor="#E3EAEB" />
<Columns>
<asp:TemplateField HeaderText="任务名称">
<EditItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("taskName") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server" Target="_blank"
NavigateUrl='<%# Eval("id", "taskDetails.aspx?id={0}") %>'
Text='<%# Eval("taskName") %>'></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="创建时间">
<EditItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("ct") %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("ct") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="状态">
<EditItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("taskStatus") %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("taskStatus") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="操作">
<EditItemTemplate>
<asp:LinkButton ID="LinkBtnUpdate" runat="server" CommandName="Update">更新</asp:LinkButton>
|<asp:LinkButton ID="LinkBtnCancle" runat="server" CommandName="Cancel" Text="取消"></asp:LinkButton>
</EditItemTemplate>
<ItemTemplate>
<asp:LinkButton ID="LinkButtonEdit" runat="server" CommandName="Edit"
>编辑任务</asp:LinkButton>
|
<asp:LinkButton ID="LinkBChangeStatus" runat="server"
CommandName="changeStatus" CommandArgument='<%# Eval("id") %>'>改变状态</asp:LinkButton>
|
<asp:LinkButton ID="LinkButtonDelete" runat="server" CommandName="Delete" >删除</asp:LinkButton>
<cc1:ConfirmButtonExtender ID="LinkButtonDelete_ConfirmButtonExtender"
runat="server" ConfirmText="确认要删除这一项吗?" Enabled="True" TargetControlID="LinkButtonDelete">
</cc1:ConfirmButtonExtender>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#7C6F57" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>