DataGrid 使用总结

  1. 关于在DATAGRID中编辑数据的设置如下:

1) 设置删除行。

<asp:buttoncolumn text="删除" buttontype="PushButton" headertext="删除" commandname="Delete"><itemstyle width="3em"></itemstyle></asp:buttoncolumn>

2) 设置编辑行。

<asp:editcommandcolumn buttontype="PushButton" updatetext="修改" headertext="编辑" canceltext="返回" edittext="编辑"></asp:editcommandcolumn>

3)       设置列

<asp:templatecolumn headertext="产品名称">

<itemtemplate>

        <asp:label id="lblProductName" runat=server text='<%# DataBinder.Eval(Container, "DataItem.product_name") %>'>

        </asp:label>

</itemtemplate>

<edititemtemplate>

<asp:textbox id="txtProductName" onblur="IsInforEmpty('产品名称',this.value);" cssclass="wid8em textEnable" maxlength=50 runat =server text='<%# DataBinder.Eval(Container, "DataItem.product_name") %>'>

</asp:textbox>

</edititemtemplate>

</asp:templatecolumn>

4) 在后台给删除按钮增加删除确认提示。

private void dgdProduct_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)

{

        //判断是否是数据行

       if(e.Item.ItemType==ListItemType.Item||e.Item.ItemType==ListItemType.AlternatingItem)

       {

               ((Button)(e.Item.Cells[0].Controls[0])).Attributes.Add ("onclick","return confirm('你确认删除吗?');");

       }

}

5) 自动分页功能。

a.       首先在前台的属性中设置 allowpaging="True" 还有 pagesize="10" 当然你也可以没页显示5行,或者随便多少行

b.       在后台的事件中要指定现在显示的页码,另外重新绑定数据。

private void dgdPreviewBooks_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)

{

        dgdPreviewBooks.CurrentPageIndex = e.NewPageIndex;

        if(Session["bookOnPreview"]!=null)

        {

                DataTable searchBooks = ((DataTable)Session["bookOnPreview"]);

                dgdPreviewBooks.DataSource=searchBooks;

                dgdPreviewBooks.DataBind();

        }

}

c.       另外注意在每一次绑定数据的时候都要指定dgdPreviewBooks.CurrentPageIndex这个属性,保险一点可以设置为0dgdPreviewBooks.CurrentPageIndex=0)这是因为有可能每次的数据页数不一样多,当你没有重新设置的时候,它还是按照上一次的设置进行显示的。这时候就有可能引起问题。

6) 从某一个单元中获取数据(依据某一列的数据去设置另外一列数据或其格式)。

private void dgdLendBooks_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)

 {

if(e.Item.ItemType==ListItemType.Item||e.Item.ItemType==ListItemType.AlternatingItem)

{

          //如果综合评分小于0时显示为红色

          int overdays = Int32.Parse(((Label)(e.Item.Cells[1].FindControl("lblOverdays"))).Text);

          if(overdays<0)

          {

                 e.Item.Cells[6].ForeColor=Color.Red;

          }

   }

       }

在这个示例中是获取第2列(index=1)的数据。这样前台当然得是Label显示的了。

((Label)dgdPreviewBooks.Items[i].Cells[0].FindControl("lblPreviewBooksID")).Text

你也可以搞个循环,依次读出某几列的数据.

posted on 2007-09-10 09:24  black263  阅读(151)  评论(0编辑  收藏  举报