Click here to Skip to main content
15,921,052 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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.
Posted
Updated 27-May-13 1:19am
v3
Comments
RelicV 24-May-13 2:02am    
What have you done till now..? We can help you but we need your effort first.

Also, your english sucks.
Meaning of Lie: A lie is a false statement to a person or group made by another person or group who knows is not the whole truth, intentionally.
praveen iThesisArt 24-May-13 3:11am    
I got one dataview on my form page which is having one column whose length is 1000 (description about item ) . So while using dataview it is showing whole of its content which is not desired.. I just want to trim that Column Length ..Please help ??
praveen iThesisArt 24-May-13 3:11am    
Also can you tell me substring function you are talking about .

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:
C#
// 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
 
Share this answer
 
v3
Comments
praveen iThesisArt 24-May-13 3:06am    
can you tell me the substring method .. am not getting that..
RelicV 24-May-13 3:32am    
Praveen, I've added the code block to the answer. Check it.
praveen iThesisArt 27-May-13 7:18am    
Hi vamsi..
As am using gridview in my asp.net form and one of the column got 1000 words limit so its harsh to show all . So i want to truncate string of that column to substring of length 15 words. Sadly your above example is not working.
RelicV 27-May-13 7:21am    
Hi Praveen, which example is not working..? Provided Block of code or the provided link..? If you're talking about the block of code, then its an example and should work for cutting the word length to 100. Let me know.
praveen iThesisArt 28-May-13 0:11am    
Sir the piece of code for cutting the word length to 100 showing error.
DataTable dt = yourDataView.ToTable();
-> This line is showing error. As i got GridView1 in my project and it is unable to execute GridView1.ToTable();
-> Am having this piece of code to be executed at Page_Load..
XML
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>
 
Share this answer
 
gridview code:
C#
<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 ??
 
Share this answer
 
Comments
RelicV 29-May-13 1:26am    
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>
praveen iThesisArt 29-May-13 14:20pm    
Thank you so much relicV... Ihis all what i needed..
RelicV 30-May-13 1:29am    
You see now, providing your code will help us solving your issues instead of we assuming something else and providing you useless code for your application. Anyways, have a good day.

Regards
Vamsi

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900