开发者

GridView with too much text

I have a GridView that pulls data out of the database and populates its fields automaticaly.

<asp:GridView ID="gvData" runat="server" CellPadding="4" 
        ForeColor="#333333" GridLines="None" AllowPaging="True" AutoGenerateDeleteButton="True" 
        AutoGenerateEditButton="True" DataSourceID="DS" PageSize="1"  CssClass="q">
        <RowStyle BackColor="#F7F6F3" ForeColor="#333333" Height="20"  CssClass="q"/>
        <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#284775" ForeColor="White" />
        <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
        <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <EditRowStyle BackColor="#999999" Height="15px" />
        <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
    </asp:GridView>
    <asp:SqlDataSource ID="DS" runat="server"></asp:SqlDataSource>

Some of the fields are too long. Is there any way to cut the length of the text and 开发者_如何学编程insert a button that pops-up a new window with text? The gridview aslo generates Edit and Delete buttons. Pressing an Edit button gives me little texboxes. How can I work around them to make the same button with a popup window for editing?

Another way that I thought of was creating a textarea with scrollbar. I know how to add a field with a scrollbar manually but it is created as a label, so when I press Edit, this field is not editable. And again, it is manual, but my gridview is auto populated. How can I populate large fields with textareas with scrollbars automatically?

Any help will be greatly appreciated! Thank you!


Okay, so I found a solution for shortening text:

protected void gvData_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        for (int i = 0; i < e.Row.Cells.Count; i++)
        {
            ViewState["OrigData"] = e.Row.Cells[i].Text;
            if (e.Row.Cells[i].Text.Length >= 30)
            {
                e.Row.Cells[i].Text = e.Row.Cells[i].Text.Substring(0, 30) + "...";
                e.Row.Cells[i].ToolTip = ViewState["OrigData"].ToString();
            }
        }

    }

} 

Then add OnRowDataBound="gvData_RowDataBound":

<asp:GridView ID="gvData" runat="server" CellPadding="4" ForeColor="#333333"
 GridLines="None" AllowPaging="True" AutoGenerateDeleteButton="True"
 AutoGenerateEditButton="True" DataSourceID="DS" PageSize="1"  
 OnRowDataBound="gvData_RowDataBound">   

This cuts the length to 30 characters and displays the full text in the ToolTip.


take a look at this post, I think it could be useful.

http://social.msdn.microsoft.com/forums/en-US/winformsdatacontrols/thread/051887c6-2d87-4361-a5a1-cd5213609673/


Answer to the second part:

    protected void gvData_PreRender(object sender, EventArgs e)
{
    if (this.gvData.EditIndex != -1)
    {
        TextBox tb = new TextBox();

        for (int i = 0; i < gvData.Rows[gvData.EditIndex].Cells.Count; i++)
            try
            {
                tb = (TextBox)
                    gvData.Rows[gvData.EditIndex].Cells[i].Controls[0];

                if (tb.Text.Length >= 30)
                {
                    tb.TextMode = TextBoxMode.MultiLine;
                }
            }
            catch { }
         }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜