首页 > 代码库 > 控制GridView中字段的长度,规范数据

控制GridView中字段的长度,规范数据

前台:

 

       <asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound">            <Columns>                <asp:TemplateField>                    <HeaderTemplate>                        <input id="cb_head" type="checkbox" name="cb_head" onclick="SelectAll(this,<%=GridView1.ClientID %>);" />                    </HeaderTemplate>                    <ItemTemplate>                        <asp:CheckBox ID="ChkChild1" runat="server" />                    </ItemTemplate>                    <ItemStyle HorizontalAlign="Center" />                    <FooterStyle HorizontalAlign="Center" />                </asp:TemplateField>            </Columns>        </asp:GridView>

后台:

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)    {        //在RowDataBound事件中调下面的方法        int[] cellIndexs = new int[4];        this.setGridViewInfo(e,1, cellIndexs, 11);    }    #region    /// <summary>    /// 控制GridView中显示字符串的长度 并 在ToolTip中显示全部文本    /// </summary>    /// <param name="e">GridViewRowEventArgs e</param>    /// <param name="cellIndexs">GridView中显示单元格的个数</param>    /// <param name="maxStrLength">要显示的最大字符串长度</param>    private void setGridViewInfo(System.Web.UI.WebControls.GridViewRowEventArgs e,int stata_index, int[] cellIndexs, int maxStrLength)    {        string CellText = string.Empty;        //因为我的GridView前一列是CheckBox,所以i从1开始        for (int i = stata_index; i < cellIndexs.Length; i++)        {            CellText = e.Row.Cells[i].Text.Trim();            if (CellText.Length > maxStrLength)            {                e.Row.Cells[i].Text = CellText.Substring(0, maxStrLength - 1) + " <strong style=‘Color:blue‘ mce_style=‘Color:blue‘> ... </strong>";                e.Row.Cells[i].ToolTip = CellText;            }        }    }    #endregion