Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 1.0 and 1.1 > ASP.NET 1.0 and 1.1 Basics
|
ASP.NET 1.0 and 1.1 Basics ASP.NET discussion for users new to coding in ASP.NET 1.0 or 1.1. NOT for the older "classic" ASP 3 or the newer ASP.NET 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 18th, 2003, 04:09 AM
Authorized User
 
Join Date: Jun 2003
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Default Access cell in DataGrid - wrong casting?

Hi all,

In my DataGrid1 I want to read one cell (data type int from database) and put it's value in the TextBox1 control.



    private void DataGrid1_UpdateCommand(object source,
           System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {....
     TextBox1.Text = (Convert.ToInt64((e.Item.Cells[1].Controls[0]))).ToString();
     ....
    }




I got run time error mesage : "Specified cast is not valid."

Can anyone tell me how to do this casting?

Thanks,
Nenad
 
Old December 18th, 2003, 10:14 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

e.Item.Cells[1].Controls[0] will return you the first control in cell 1. Usually you need to convert that to something explicitly so you can access a property value for that control:

    TextBox1.Text = CType(e.Item.Cells[1].Controls[0], TextBox).Text;

However, if you are attempting to just get the value that's in the cell, you can just access the "Text" property of that cell:

    e.Item.Cells[1].Text

The method you show here looks like the update handler for the grid so I would believe that you need the first suggestion unless the cell in question is in a readonly column.

When dealing with data grids, you must remember that after the data has been dumped to the grid, all the datatypes are as good as gone. Everything is a string in the grid. So you will always have to convert from a string back into the datatype you want.

Peter
------------------------------------------------------
Work smarter, not harder.
 
Old December 19th, 2003, 09:12 AM
Authorized User
 
Join Date: Jun 2003
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Yes you are right I need the first example, I tried with this:
Code:
TextBox1.Text  = ((TextBox)(e.Item.Cells[4].Controls[0])).Text;
but I still get the same error: Specified cast is not valid ???
Second example works with readonly cols like you said.

I have to solve this urgent

Thanks for your advice
 
Old December 19th, 2003, 10:30 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Are you sure you have the right cell index? That index should match based on the columns in the "columns" area of the markup. Do you have any non-visible columns? You must count all columns.

Paste your datagrid markup if you are still having a problem.

Peter
------------------------------------------------------
Work smarter, not harder.
 
Old December 22nd, 2003, 04:45 AM
Authorized User
 
Join Date: Jun 2003
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Finally solution:

Code:
  TextBox1.Text  = ((TextBox)(e.Item.Cells[1].FindControl("txtBox1"))).Text;
because Cells[1] belongs TemplateColumn, and
markup:
Code:
 <EditItemTemplate>
   <asp:TextBox ID="txtBox1" BackColor=#ffcccc Width=50 runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Kolicina") %>'>
   </asp:TextBox>
</EditItemTemplate>
Thank you Peter you really helped me,

Nenad





Similar Threads
Thread Thread Starter Forum Replies Last Post
error casting button in datagrid Hughesie78 C# 2 May 2nd, 2008 08:04 AM
Getting DataGrid Cell Data lifewarped General .NET 2 February 20th, 2005 07:00 PM
DataGrid cell events?? Help Kathryn ASP.NET 1.0 and 1.1 Basics 4 January 31st, 2005 10:12 AM
DataGrid Cell mrideout BOOK: Beginning ASP.NET 1.0 0 August 17th, 2004 12:34 PM
how to access cell of datagrid? basant ADO.NET 1 December 30th, 2003 02:01 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.