Gridview 编辑值
Posted
技术标签:
【中文标题】Gridview 编辑值【英文标题】:Gridview edit value 【发布时间】:2011-09-25 14:11:36 【问题描述】:我有一个从数据库动态填充的网格视图。有些字段很长。我找到了一种减少单元格中文本长度的方法。
现在,当我在编辑视图中时,所有的字段都在单行文本框中,对于文本量来说太小了。如何用另一个字段替换 texbox?最好用<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;
【问题讨论】:
【参考方案1】:试试下面的代码
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;
【讨论】:
这行不通。 RowDataBound 上没有文本框。它是在 Rowediting 上创建的。【参考方案2】:找到了解决办法:
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
【讨论】:
以上是关于Gridview 编辑值的主要内容,如果未能解决你的问题,请参考以下文章