Changing GridView data in code

C

cpnet

I have searched for this, but I'm not having much luck. I know in my .aspx
page, I can use <%# Eval(...) %> to render values from my SqlDatasource into
my page. But, Eval doesn't support the formatting I want. (I'm pulling a
description field from the database, and if it's more than 57 characters
long, I want to truncate the text and put "..." at the end). I'm not clear
on what GridView event I should be using (but I'm guessing RowDataBound).
I'm also not clear how in my code I get the value from my database field for
the current row, and how I set the proper page contol's text once I've
formatted it the way I want.
 
K

KJ

If Eval isn't enough, you can actually call a method in the databinding
syntax, for example:

<asp:label id="lblText" runat="Server" text="<%#
SomeMethod(Container.DataItem) %>"/>

Then in the codebehind (or wherever):

public string SomeMethod(object input)
{
//cast the object, or, alternatively, cast it before calling
SomeMethod, then change the input parameter type from object
//return a string
}

-KJ
 
K

KJ

If Eval isn't enough, you can actually call a method in the databinding
syntax, for example:

<asp:label id="lblText" runat="Server" text="<%#
SomeMethod(Container.DataItem) %>"/>

Then in the codebehind (or wherever):

public string SomeMethod(object input)
{
//cast the object, or, alternatively, cast it before calling
SomeMethod, then change the input parameter type from object
//return a string
}

-KJ
 
E

Edwin Knoppert

Yuo can also pass the EVAL() itself to a method.
Due empty problems i might recommend using .ToString as well.
Then the method's param simply is a string.
Using the event is also an option, but this might be quicker?
 
C

cpnet

I was able to call my own method from my .aspx page:

<% # FixText( Eval("MyDatabaseField")) %>

But, I had to make the FixText method public in my page. And, I've now
realized that I really want to format info from several database fields into
a single label. So, what I'd really like to do is just have a label in the
ItemTemplate of my GridView, and set the text of the Label during an event.
I assume the event is the GridView.RowDataBound event. But I don't know how
to access the Label control (I can't reference it by name in my .cs file).
And, I'm not sure how to reference the data fields for current row, for the
datasource bound to my GridView.
 
E

Edwin Knoppert

You'll need findcontrol on the row.
Just mess with it a little.
But.. that made me clear you'll abs. need uniqly named controls in this
grid.
Not per itemtemplate but the whole control.
So do not have a label1 twice.

There is still an itemdata or sort of property.
But i couldn't do much with it yet.
(I needed to determine if this was a date field and do some special
formatting)
Unless you know what cell is what fieldtype.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,539
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top