Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Iterating through GridView 2

Status
Not open for further replies.

developer155

Programmer
Jan 21, 2004
512
US
Hi I have the below GridView and I am trying to iterate through all the name fields in it in AJAX function.

Here is the gridview
<asp:gridview id="CustomLists"
allowpaging="false"
runat="server"
CssClass="lists"
HeaderStyle-Wrap="false"
AutoGenerateColumns ="false"
OnRowDataBound="CustomLists_RowDataBound">
<HeaderStyle CssClass="header" ></HeaderStyle>

<Columns>

<asp:BoundField ReadOnly="True" DataField="customListID" SortExpression="customListID" HeaderText="id" HeaderStyle-CssClass="id">
<ItemStyle CssClass="id" />
</asp:BoundField>

<asp:BoundField ReadOnly="True" DataField="name" SortExpression="name" HeaderText="list name" HeaderStyle-CssClass="name">
<ItemStyle CssClass="name" />
</asp:BoundField>


And here is the code I wrote to iterate through it(crashes):

foreach (GridViewRow row in CustomLists.Rows)
{
DataRowView drv = (DataRowView)row.DataItem;
if (drv["name"].ToString() == "some value")
return "false";
}

Basically I am interested DataField="name" column. I need to examine each one and compare it to some value. PLease let me know what I am doing wrong while iterating

thanks
 
Can you do this whilst the data is binding? If so use the RowDataBound event. If not, you need to loop through the GridView.Rows collection and check the relevant Cells property not the DataItem.


____________________________________________________________
Mark,
[URL unfurl="true"]http://aspnetlibrary.com[/url]

Need help finding an answer? Try the Search Facility or read FAQ222-2244.
 
So you mean like this?

foreach (GridViewRow row in CustomLists.Rows)
{
if(row.Cells[2].ToString()=="something")

I cannot use FindControl since I do no have ID for the NAME cell (since its DataBound field)

thanks
 
I noticed that in AJAX call CustomLists grid reference is NULL so it crashes. I guess this is because there is no normal page cycle and it does not get initialized. Is there a way around it or do I have to choose some other method?

thanks
 
When you say AJAX what exactly do you mean?

When using AJAX you won't be able to reference the grid because you won't have the viewstate for the grid available on the server - because it isn't passed to the server.

If you are using .NET AJAX-like functionality then you can get around this (using the ICallbackHandler or the asp ajax.net library) because it does make use of the viewstate.

If you're writing your own Javascript for the first scenario then you could loop through all of the table rows (which is what a gridview will be rendered as anyway) and store them in an array. Then write them out to a delimited string to pass back to the server. Then split the string up and use the values. Then you could recreate the table on the server as a string and inject it into the html via your ajax response handler.

Bascially I'm saying that if you're using "real" AJAX then you can throw away the gridview and create your own table.

Don't do what I've said above if you are using asp.net ajax like libraries.

J
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top