Subject: Access cell in DataGrid - wrong casting?
Posted By: drasko Post Date: 12/18/2003 3:09:21 AM
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
Reply By: planoie Reply Date: 12/18/2003 9:14:06 AM
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.
Reply By: drasko Reply Date: 12/19/2003 8:12:13 AM
Yes you are right I need the first example, I tried with this:

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
Reply By: planoie Reply Date: 12/19/2003 9:30:29 AM
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.
Reply By: drasko Reply Date: 12/22/2003 3:45:34 AM
Finally solution:


  TextBox1.Text  = ((TextBox)(e.Item.Cells[1].FindControl("txtBox1"))).Text;

because Cells[1] belongs TemplateColumn, and
markup:

 <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

Go to topic 7782

Return to index page 982
Return to index page 981
Return to index page 980
Return to index page 979
Return to index page 978
Return to index page 977
Return to index page 976
Return to index page 975
Return to index page 974
Return to index page 973