1 Replies - 6820 Views - Last Post: 21 May 2007 - 10:18 AM Rate Topic: -----

#1 beish1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 21-May 07

Gridview textbox - action depending on length

Posted 21 May 2007 - 05:15 AM


I have textbox displaying a description field from a database in a gridview. I want to display up to 100 characters only. If number characters > 100, want to display "...more" after they are displayed, which can then be selected to display all the description in a pop-up window.


Is This A Good Question/Topic? 0
  • +

Replies To: Gridview textbox - action depending on length

#2 JellyBean  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 60
  • Joined: 25-April 07

Re: Gridview textbox - action depending on length

Posted 21 May 2007 - 10:18 AM

How are you binding data to the GridView? You can always use a TemplateField for one column in the datagrid, which can call a method which does some kind of string truncation.

TemplateFields are very good for this kind of stuff where you need to do a little bit more than just databinding.

Try something like this:
  <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
		  <asp:BoundField DataField="OriginalString" DataFormatString="{0}" HeaderText="Original String" />
		  <asp:TemplateField HeaderText="Trimmed String">
		  <asp:Label ID="Label1" runat="server" Text='<%# MyStringFormat(Eval("OriginalString", "{0}")) %>'></asp:Label>

And in the code-behind, you could have something like this:
protected void Page_Load(object sender, EventArgs e)
	// create some dummy data and bind it to the grid...
	DataTable t = new DataTable();
	t.Columns.Add(new DataColumn("OriginalString", typeof(string)));
	DataRow r = t.NewRow();
	r["OriginalString"] = "FooBar Foo BarFoo";
	r = t.NewRow();
	r["OriginalString"] = "FooBar";
	GridView1.DataSource = t;

protected string MyStringFormat(string value)
	int maxLength = 10;
	if (value.Length > maxLength)
		return value.Substring(0, maxLength) + "...";
	return value;

Of course you can change the maxLength value in the MyStringFormat method to, say, 100 if you wanted ;)

Good luck!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1