473,847 Members | 1,792 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Changing GridView data in code

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.
Dec 8 '05 #1
5 2661
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(Cont ainer.DataItem) %>"/>

Then in the codebehind (or wherever):

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

-KJ

Dec 8 '05 #2
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(Cont ainer.DataItem) %>"/>

Then in the codebehind (or wherever):

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

-KJ

Dec 8 '05 #3
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?

"KJ" <n_**********@m ail.com> schreef in bericht
news:11******** *************@o 13g2000cwo.goog legroups.com...
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(Cont ainer.DataItem) %>"/>

Then in the codebehind (or wherever):

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

-KJ

Dec 8 '05 #4
I was able to call my own method from my .aspx page:

<% # FixText( Eval("MyDatabas eField")) %>

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.RowDat aBound 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.

Dec 9 '05 #5
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.

"cpnet" <cp***@nospam.n ospam> schreef in bericht
news:eT******** ******@TK2MSFTN GP11.phx.gbl...
I was able to call my own method from my .aspx page:

<% # FixText( Eval("MyDatabas eField")) %>

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.RowDat aBound 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.

Dec 10 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2623
by: Nalaka | last post by:
Hi, I have two questions about gridViews. 1. How can I intercept the row/column values at loading to change values? 2. After I update a row (using default update functionality), how can I re-format the updated row fields. I have looked at gridView.rowUpdated method, but cannot figure out how....
7
14826
by: | last post by:
Hello, Does anyone have an idea on how I can filter the data in the gridview control that was returned by an sql query? I have a gridview that works fine when I populate it with data. Now I want to look at that data and filter it based on what is in it. I know that this could have been done with data sets and data views in asp.net 1.1 but how is this done now in asp.net 2.0?
0
1237
by: Greg Ramsey | last post by:
Hi - I'm a real newbie to ASP . . so please forgive me. . . and please point me in the right direction for how to do this "the right way" . . :) Using Northwind DB as an example, I have a gridview bound to a sqldatasource, with a SelectQuery = SELECT , , , FROM That all works fine. . . but what I want to do next is this: Users clicks on button "Customers and Suppliers By City", and the code for the button simply sets SelectCommand =...
7
5386
by: GaryDean | last post by:
I'm trying to load a GridView up with data manually, in code. I'm not using any datasource. Using this code.... ArrayList myRowArrayList; GridViewRow myGVR = new GridViewRow(0,0,DataControlRowType.DataRow, DataControlRowState.Normal); TableCell myCell = new TableCell(); TextBox myTextbox = new TextBox(); myTextbox.Text = "hello world"; myCell.Controls.Add(myTextbox);
5
57934
by: Kimmo Laine | last post by:
Hi is there a way to change propertys attribute from the code? Let´s say that i have the following property in my class: public int Count } Is there a way to change the displayname, from my code, at runtime to "Number of bikes".
0
2443
by: landesjoe | last post by:
Hi, here's my problem in short: Text boxes in gridview don't seem to hold their value if the column's .Visible property is changed back and forth. I've got a form with a gridview populated from a data view (which in turn is loaded from a manually setup DataTable for testing purposes). One of the columns in the grid is a checkbox that's tied to an event handler that'll change the .Visible property of a column with a text box. That text...
0
1712
jenka1980
by: jenka1980 | last post by:
What is the best way to correspond SkinId in GridView to the data which bound to it ? I have dataset which I bound to a GridView. Dataset contains a table where part of rows are title rows. What I'm trying to achieve is to make this "theme able", so I could set different styles for title and simple rows. Problem is that you can't change SkinId of control that already added to controls collection after page init and I can populate dataset...
4
9795
by: mohaaron | last post by:
This seems like it should be simple to do but for some reason I have been unable to make it work. I would like to databind a SqlDataSource to a GridView during the click event of a button. This sounds easy but the next requirement is that the GridView is editable. So I have included the SelectCommand and the UpdateCommand on the SqlDataSource to allow the GridView to be editable. I have now been able to get the GridView to display data...
3
5249
by: Peter | last post by:
I have a GridView which is populated by List<ofObjects> Does anyone have example of how to sort the columns of this GridView? I have found examples without DataSourceControl but these use DataTable, I am using List of Objects. Here's one example: http://ryanolshan.com/technology/gridview-without-datasourcecontrol-datasource/
0
9886
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10983
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10647
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10706
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9482
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5719
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5911
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4528
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3164
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.