[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1338
  • Last Modified:

BoundField trim text gridview

how can we do this ?
0
goodluck11
Asked:
goodluck11
1 Solution
 
BuggyCoderCommented:
Two ways to do this:-
1. use ondatabound event, find the control and trim its text using trim method of string instance.
2. convert your bound field to TemplateField and do something like this:-

<asp:TemplateField HeaderText="Bulletin" >
<ItemTemplate>
<asp:Label ID="LabelBulletin" runat="server" Text='<%# Eval("Bulletin").ToString().Substring(0,Math.Min(20,Eval("Bulletin").ToString().Length))+"...." %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>

Open in new window

0
 
DhaestCommented:
For trimming values in gridview follow this code-


private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (e.RowIndex > -1)
           {
                if (e.Value != null)
               {
                   e.Value = e.Value.ToString().Trim();
                }
           }  
      }
0
 
Rose BabuSenior Team ManagerCommented:
update the cell data using RowDataBound event as like below.
protected void GridView1_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{

	if (e.Row.RowType == DataControlRowType.DataRow) {

		for (i = 0; i <= e.Row.Cells.Count - 1; i++) {
			e.Row.Cells[i].Text = e.Row.Cells[i].Text.Trim();

		}

	}

}

Open in new window

Try this. It will work for you.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now