如何修剪gridview内容 [英] how to trim gridview content
本文介绍了如何修剪gridview内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否有任何一段代码,以便我可以限制一列长度字。
简介 - 我想修剪gridview的一个数据列的内容。
解决方案
嗨Praveen,
你总是可以在数据视图上使用子字符串函数,但你必须在一行中使用它行基础。这意味着,将使用一个循环来获得列上所需的长度。
Ex:
// 创建一个临时DataTable来操作数据并从dataview获取数据到dt
DataTable dt = yourDataView.ToTable();
for ( int i = 0 ; i < dt.Rows.Count; i ++)
{
dt.Rows [i] [column] = dt.Rows [i] [column] .ToString()。Substring( 0 , 100 );
}
// 现在,使用此DataTable更新DataView
yourDataView = dt.AsDataView();
或者,您可以在数据视图中创建一个新列并使用 myDataColumn.Expression 在上面获取所需的数据。 点击此处 [ ^ ]
请看看它并尽最大努力。
谢谢你,
Vamsi
换句话说,你可以简单地用模板字段替换boundfield。例如,请查看以下
< asp:TemplateField >
< ItemTemplate >
<%#(( string )Eval( diarycontent))。长度< ; 100 ? Eval( diarycontent):(( string )Eval( diarycontent))。子串( 0 , 100 )+ 。 .. %> )
< / ItemTemplate >
< / asp:TemplateField >
gridview代码:
< asp:gridview id = GridView1 runat = server allowpaging = 跨度> True xmlns:asp = #unknown >
AutoGenerateColumns = 假 DataKeyNames = op DataSourceID = SqlDataSource1
style = z-index:1; left:276px; font-size:24px;上:321px;位置:绝对;身高:131px; width:717px
ForeColor = White
onrowcommand = GridView1_RowCommand >
< columns>
< asp:boundfield datafield = diarycontent headertext = 日记内容 >
SortExpression = diarycontent />
< asp:boundfield datafield = < span class =code-string> diarypass headertext = Diary Pass >
SortExpression = diarypass />
< asp:boundfield datafield = diarydate headertext = 日记日期 >
SortExpression = diarydate />
< asp:boundfield datafield = opp headertext = 页码 sortexpression = opp />
< asp:buttonfield buttontype = 按钮 commandname = 选择 >
HeaderText = 查看日记页面 ShowHeader = True Text = 查看日记页面 /> ;
< / asp:buttonfield > < / asp:boundfield > < / asp:boundfield > < / asp:boundfield > < / 列 >
< / asp:gridview >
这里日记内容字段的字长为1000瓦。
如何将其修剪为100?
Is there any piece of code so that i can restrict one of columns length word..
Brief- i want to trim the content of one data column of gridview.
解决方案
Hi Praveen,
You could always use a substring function on a dataview but you have to do it on a row by row basis. It means, a loop is to be used to get the required length on a column.
Ex:
// Create a temp DataTable for manipulating the data and get the data from dataview into dt DataTable dt = yourDataView.ToTable(); for(int i = 0; i < dt.Rows.Count; i++) { dt.Rows[i][column] = dt.Rows[i][column].ToString().Substring(0, 100); } // Now, update the DataView with this DataTable yourDataView = dt.AsDataView();
Or, you could create a new column in the dataview and use myDataColumn.Expression on it to get the required data. Click Here[^]
Please do take a look at it and give your best shot.
Thank you,
Vamsi
In another way, you could simply replace the boundfield with a template field. For example look below <asp:TemplateField> <ItemTemplate> <%# ((string)Eval("diarycontent")).Length < 100? Eval("diarycontent") :((string)Eval("diarycontent")).Substring(0,100) + "..."%>) </ItemTemplate> </asp:TemplateField>
gridview code:
<asp:gridview id="GridView1" runat="server" allowpaging="True" xmlns:asp="#unknown"> AutoGenerateColumns="False" DataKeyNames="op" DataSourceID="SqlDataSource1" style="z-index: 1; left: 276px;font-size:24px; top: 321px; position: absolute; height: 131px; width: 717px" ForeColor="White" onrowcommand="GridView1_RowCommand"> <columns> <asp:boundfield datafield="diarycontent" headertext="Diary Content"> SortExpression="diarycontent" /> <asp:boundfield datafield="diarypass" headertext="Diary Pass"> SortExpression="diarypass" /> <asp:boundfield datafield="diarydate" headertext="Diary Date"> SortExpression="diarydate" /> <asp:boundfield datafield="opp" headertext="Page Number" sortexpression="opp" /> <asp:buttonfield buttontype="Button" commandname="Select"> HeaderText="View Diary Page" ShowHeader="True" Text="View Diary Page" /> </asp:buttonfield></asp:boundfield></asp:boundfield></asp:boundfield></columns> </asp:gridview>
Here the Diary content field got word length of 1000 w .
How can i trim that to 100 ??
这篇关于如何修剪gridview内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文