Invalid cast?

T

tshad

I have an ItemDataBound event for my delete button and am getting an error:

System.InvalidCastException: Specified cast is not valid.

The event code:

********************************************************
public void articleList_ItemDataBound(Object sender, DataGridItemEventArgs
e)
{
// First, make sure we're NOT dealing with a Header or Footer row
if ((e.Item.ItemType != ListItemType.Header) && (e.Item.ItemType !=
ListItemType.Footer))
{
// Now, reference the Button control that the Delete ButtonColumn
// has been rendered to
Button deleteButton = (Button)e.Item.Cells[0].Controls[0]; <-- the
error
******************************************************************

The DataGrid looks like:

<asp:datagrid id="articleList" runat="server" HeaderStyle-Font-Bold="True"
HeaderStyle-Font-Size="15pt"
HeaderStyle-ForeColor="White" HeaderStyle-BackColor="Navy"
AlternatingItemStyle-BackColor="#eeeeee"
AutoGenerateColumns="False" Font-Name="Verdana" DataKeyField="FullName"
OnItemDataBound="articleList_ItemDataBound"
OnDeleteCommand="articleList_DeleteFile">
<Columns>
<asp:HyperLinkColumn DataNavigateUrlField="Name" DataTextField="Name"
HeaderText="File Name" />
<asp:BoundColumn DataField="LastWriteTime" HeaderText="Last Write Time"
ItemStyle-HorizontalAlign="Center"
DataFormatString="{0:d}" />
<asp:BoundColumn DataField="Length" HeaderText="File Size"
ItemStyle-HorizontalAlign="Right" DataFormatString="{0:#,##0 bytes}" />
<asp:ButtonColumn Text="Delete" ButtonType="PushButton"
CommandName="Delete" />
</Columns>
</asp:datagrid>

What is wrong with the cast?

Thanks,

Tom
 
M

Mark Fitzpatrick

You may try to iterate through the collection of cells first and trace the
control types to make sure that you have the correct cell. By the looks of
this, Cell 0 is a HyperLink column and not a button. Try using Cell 3
instead.


You could also dump the cell contents to trace like

for(int i = 0; i < e.Item.Cells.Count;i++)
{
for(int j = 0; j < e.Item.Cells.Controls.Count;j++)
{
Trace.Write("Cell " + i.ToString() + " Control " +
j.ToString(),e.Item.Cells.Controls[j].GetType().ToString());
}
}

You may need to edit that as it's just a rough and I haven't run it
through the compiler.

Hope this helps,
Mark Fitzpatrick
Microsoft MVP - FrontPage
 
T

tshad

Mark Fitzpatrick said:
You may try to iterate through the collection of cells first and trace the
control types to make sure that you have the correct cell. By the looks of
this, Cell 0 is a HyperLink column and not a button. Try using Cell 3
instead.

That was it.

I had moved the button from the 1st control in the datagrid to the last one.

Thanks,

Tom
You could also dump the cell contents to trace like

for(int i = 0; i < e.Item.Cells.Count;i++)
{
for(int j = 0; j < e.Item.Cells.Controls.Count;j++)
{
Trace.Write("Cell " + i.ToString() + " Control " +
j.ToString(),e.Item.Cells.Controls[j].GetType().ToString());
}
}

You may need to edit that as it's just a rough and I haven't run it
through the compiler.

Hope this helps,
Mark Fitzpatrick
Microsoft MVP - FrontPage


tshad said:
I have an ItemDataBound event for my delete button and am getting an
error:

System.InvalidCastException: Specified cast is not valid.

The event code:

********************************************************
public void articleList_ItemDataBound(Object sender,
DataGridItemEventArgs e)
{
// First, make sure we're NOT dealing with a Header or Footer row
if ((e.Item.ItemType != ListItemType.Header) && (e.Item.ItemType !=
ListItemType.Footer))
{
// Now, reference the Button control that the Delete ButtonColumn
// has been rendered to
Button deleteButton = (Button)e.Item.Cells[0].Controls[0]; <-- the
error
******************************************************************

The DataGrid looks like:

<asp:datagrid id="articleList" runat="server"
HeaderStyle-Font-Bold="True" HeaderStyle-Font-Size="15pt"
HeaderStyle-ForeColor="White" HeaderStyle-BackColor="Navy"
AlternatingItemStyle-BackColor="#eeeeee"
AutoGenerateColumns="False" Font-Name="Verdana"
DataKeyField="FullName"
OnItemDataBound="articleList_ItemDataBound"
OnDeleteCommand="articleList_DeleteFile">
<Columns>
<asp:HyperLinkColumn DataNavigateUrlField="Name" DataTextField="Name"
HeaderText="File Name" />
<asp:BoundColumn DataField="LastWriteTime" HeaderText="Last Write Time"
ItemStyle-HorizontalAlign="Center"
DataFormatString="{0:d}" />
<asp:BoundColumn DataField="Length" HeaderText="File Size"
ItemStyle-HorizontalAlign="Right" DataFormatString="{0:#,##0 bytes}" />
<asp:ButtonColumn Text="Delete" ButtonType="PushButton"
CommandName="Delete" />
</Columns>
</asp:datagrid>

What is wrong with the cast?

Thanks,

Tom
 

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

Staff online

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top