Gridview编辑值

发布于 2024-11-18 07:29:11 字数 1769 浏览 3 评论 0原文

我有一个从数据库动态填充的网格视图。有些字段相当长。我找到了一种方法来缩短单元格中文本的长度。 现在,当我处于编辑视图中时,所有字段都位于单行文本框中,对于文本量来说太小了。如何用另一个字段替换文本框?最好使用

<div style="overflow:auto;">
    <asp:GridView ID="gvData" OnRowDataBound="gvData_RowDataBound" OnRowEditing="gvData_RowEditing" runat="server" CellPadding="4" 
        ForeColor="#333333" GridLines="Vertical" AllowPaging="True" 
        AutoGenerateDeleteButton="True" AutoGenerateEditButton="True" DataSourceID="DS">
        <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
        <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" />
        <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
    </asp:GridView>
    <asp:SqlDataSource ID="DS" runat="server"></asp:SqlDataSource>
</div>

protected void gvData_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        for (int i = 0; i < e.Row.Cells.Count; i++)
        {
            TextBox txtBox = new TextBox();

            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();
            }
            e.Row.Cells[i].Wrap = false;

        }
    }
}

I have a gridview which is populated dynamicaly from the database. Some of the fields are pretty long. I found a way to cut the length of the text in the cell.
Now, when I am in the edit view, all the fiels are in singleline textboxes, which are too small for the amount of text. How can I replace a texbox with another field? Preferrably with <textarea></textarea>

<div style="overflow:auto;">
    <asp:GridView ID="gvData" OnRowDataBound="gvData_RowDataBound" OnRowEditing="gvData_RowEditing" runat="server" CellPadding="4" 
        ForeColor="#333333" GridLines="Vertical" AllowPaging="True" 
        AutoGenerateDeleteButton="True" AutoGenerateEditButton="True" DataSourceID="DS">
        <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
        <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" />
        <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
    </asp:GridView>
    <asp:SqlDataSource ID="DS" runat="server"></asp:SqlDataSource>
</div>

protected void gvData_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        for (int i = 0; i < e.Row.Cells.Count; i++)
        {
            TextBox txtBox = new TextBox();

            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();
            }
            e.Row.Cells[i].Wrap = false;

        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

瑶笙 2024-11-25 07:29:11

尝试下面的代码

protected void gvData_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        for (int i = 0; i < e.Row.Cells.Count; i++)
        {
            TextBox txtBox = e.Row.Cells[i].Controls[0] as TextBox;
    txtBox.TextMode = TextBoxMode.MultiLine;

        }
    }
}

try the following code

protected void gvData_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        for (int i = 0; i < e.Row.Cells.Count; i++)
        {
            TextBox txtBox = e.Row.Cells[i].Controls[0] as TextBox;
    txtBox.TextMode = TextBoxMode.MultiLine;

        }
    }
}
孤云独去闲 2024-11-25 07:29:11

找到了解决方案:

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 { }
    }
}

found a solution:

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 { }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文