读书人

DataGrid中怎么给模板列赋值

发布时间: 2012-05-15 14:35:29 作者: rapoo

DataGrid中如何给模板列赋值
//.aspx--代码:
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="<<" />
<asp:Button ID="Button2" runat="server" onclick="Button2_Click" Text=">>" />
<br />
<asp:datagrid id="dtgDj" runat="server" AllowSorting="True"
AutoGenerateColumns="False"
CellPadding="10" BorderWidth="1px"
BorderColor="#000099" BackColor="White" Width="100%" PageSize="3"
AllowPaging="True">
<ItemStyle HorizontalAlign="Center" BorderWidth="15px" BackColor="White" ForeColor="#330099"></ItemStyle>
<HeaderStyle Font-Bold="True" HorizontalAlign="Center" BackColor="#507CD1" ForeColor="#FFFFCC"></HeaderStyle>
<Columns>
<asp:TemplateColumn HeaderText="选 择">
<ItemTemplate>
<asp:CheckBox ID="chk0" runat="server" />
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="sgd_id" HeaderText="编 号"></asp:BoundColumn>
<asp:BoundColumn DataField="sgd_sqr" HeaderText="申购人"></asp:BoundColumn>
<asp:BoundColumn DataField="sgd_wpmc" HeaderText="申购物品"></asp:BoundColumn>


<asp:TemplateColumn HeaderText="状态">
<ItemTemplate>
<asp:Label style="Z-INDEX: 0" id="lblzt" runat="server">Label</asp:Label>
</ItemTemplate>
</asp:TemplateColumn>

</Columns>
<FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
<SelectedItemStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
<PagerStyle NextPageText="下一页" PrevPageText="上一页" HorizontalAlign="Center" ForeColor="#330099"
BackColor="#FFFFCC" Mode="NumericPages" Visible="False"></PagerStyle>
</asp:datagrid>

//.CS--绑定及分页代码-----------------------------------------
private void CheckBind()
{
string str = "";
str = "select * from zcsgd_t_v WHERE bmjl_tp='2' ";
DataSet ds = new DataSet();
ds = Info.Select(str, "zcsgd_t_v");

//if (ds.Tables[0].Rows.Count > 0)
//{
// Label lbl = new Label();
// for (int c = 0; c < ds.Tables[0].Rows.Count; c++)
// {
// lbl = (Label)this.dtgDj.Items[c].FindControl("lblzt");
// if (ds.Tables[0].Rows[c]["zhb_tp"].ToString() == "1")
// {
// if (ds.Tables[0].Rows[c]["sgd_stu"].ToString() == "0")//
// {
// lbl.Text = "等候审核";
// }
// else if (ds.Tables[0].Rows[c]["sgd_stu"].ToString() == "2")
// {
// lbl.Text = "<FONT color='#ff0066'>被撤销<FONT>";
// }


// }
// else if (ds.Tables[0].Rows[c]["zhb_tp"].ToString() == "2")
// {
// lbl.Text = "<FONT color='#999999'>已审核<FONT>";
// }
// }
//}

this.dtgDj.DataSource = ds;
this.dtgDj.DataBind();
}

protected void Button1_Click(object sender, EventArgs e)
{
//上一页
if (this.dtgDj.CurrentPageIndex != 0)
{
this.dtgDj.CurrentPageIndex = this.dtgDj.CurrentPageIndex - 1;
}
CheckBind();
}
protected void Button2_Click(object sender, EventArgs e)
{
//下一页
if (this.dtgDj.CurrentPageIndex != this.dtgDj.PageCount - 1)
{
this.dtgDj.CurrentPageIndex = this.dtgDj.CurrentPageIndex + 1;
}
CheckBind();
}
问题是这样的,屏蔽了中间那段给模板列中的Label赋值的代码后,一切正常,能实现分页。
如果运行中间那段代码,则提示错误:
异常详细信息: System.ArgumentOutOfRangeException: 索引超出范围。必须为非负值并小于集合大小。

问:有什么方法能实现模板列中Label赋值,并且能实现分页。


[解决办法]
<%# Eval("列名").ToString()%>
[解决办法]
你每次都取ds.Tables[0].Rows.Count总数,而你每页数量要少于总数,所以你遍历就要越界了
[解决办法]
感觉你的意思应该是格式化单元格内容

C# code
  protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)  {      if (e.Row.RowType == DataControlRowType.DataRow)      {          if (e.Row.Cells[索引].Text == "xxx")          {              e.Row.Cells[索引].Text = "bbbbb";          }      }  }
[解决办法]
控件加个事件GridView1_RowDataBound
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.Cells[zhb_tp的索引].Text == "1")
{
if (e.Row.Cells[sgd_stu的索引].Text == "0")
e.Row.Cells[索引].Text = "等待审核";
if (e.Row.Cells[sgd_stu的索引].Text == "2")
e.Row.Cells[索引].Text = "被撤销";
}
else if (e.Row.Cells[zhb_tp的索引].Text == "2")
{
e.Row.Cells[索引].Text = "已审核";
}
}
}

[解决办法]
datatable 中有数据么?没有的话
for (int c = 0; c < ds.Tables[0].Rows.Count; c++)
会报越界的错误

读书人网 >asp.net

热点推荐