Results 1 to 7 of 7

Thread: Whats wrong wiht this code? Invalid cast

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    May 2002
    Posts
    1,602

    Whats wrong wiht this code? Invalid cast

    VB Code:
    1. Dim chkBox As New CheckBox
    2.         Dim oOrder As New COrder
    3.         For Each di As DataGridItem In dgOrders.Items
    4.             chkBox = CType(di.Cells(4).Controls(0), CheckBox)
    5.             If chkBox.Checked Then
    6.                 oOrder.CloseOrder(di.Cells(0).Text)
    7.             End If
    8.         Next

    This code occurs when I hit a save button and it checks which checkboxes in the grid that was checked and update the db (oorder.closeorder)

    but the line

    VB Code:
    1. chkBox = CType(di.Cells(4).Controls(0), CheckBox)

    doesn't work, I get an invalid cast exception... why? is there any other way to do it?

    kind regards
    henrik

  2. #2
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Make sure Control(0) is in fact a checkbox. Try:
    Msgbox(TypeName(di.Cells(4).Controls(0)))

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    May 2002
    Posts
    1,602
    Hm ok this is wierd. In my datagrid I have:

    Buttoncolumn, bound control, boundcontrol, templatecontrol, templatecontrol


    When I check

    di.Cells(0).Controls(0) I get linkbutton, which is great

    di.Cells(1).Controls(0) <--invalid index (no control here)

    di.Cells(2).Controls(0) <--invalid index (no control here)

    di.Cells(3).Controls(0) <--LITERAL Control (wierd, because it's checkbox)

    di.Cells(4).Controls(0) <--LITERAL Control (wierd, because it's checkbox)


    I really have no clue why it is like this....?


    kind regards
    Henrik

  4. #4
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    IF I know exactly what control belongs there..
    I use:
    VB Code:
    1. Dim X as Checkbox = DirectCast(di.Cells(4).Controls(0),CheckBox)

    I think your problem lies in the fact, that the datagrid has temporarily placed a literal where the checkbox will take it place (before the page has rendered)or before the data has been binded. Once the binding actually occurs, the datagrid probably renders a checkbox in there.

    You see, you need to put your code in the databinding event, or shortly after in the prerender.
    Last edited by nemaroller; Feb 19th, 2004 at 11:43 PM.

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    May 2002
    Posts
    1,602
    Well the page works like this:

    First I render a few textboxes and a search button

    The user click search and I perform a databind on the datagrid if I got any values back from the db. I also make a button "save" visible, and it is in that button event that I want to loop through the datagrid and check if any checkboxes are checked... Here is the code

    VB Code:
    1. Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.         If Not Page.IsPostBack Then
    3.             FillUI()
    4.         End If
    5.     End Sub
    6.  
    7.     Public Sub FillUI()
    8.         Dim oCompany As New clsCompany
    9.         Dim oStatus As New clsStatus
    10.         With ddlSalesCompany
    11.             .DataSource = oCompany.GetNamesDr
    12.             .DataValueField = "COMPANY_KEY_NO"
    13.             .DataTextField = "NAME"
    14.             .DataBind()
    15.             .SelectedItem.Text = "*"
    16.             oCompany = Nothing
    17.         End With
    18.  
    19.  
    20.  
    21.     End Sub
    22.  
    23.     Private Sub btnSearchInvoice_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearchInvoice.Click
    24.         Dim oOrder As New COrder
    25.         dgOrders.DataSource = oOrder.GetOrders(txtOrderNo.Text, ddlSalesCompany.SelectedItem.Text, ddlStatus.SelectedItem.Text)
    26.         dgOrders.DataBind()
    27.         If dgOrders.Items.Count < 1 Then
    28.             dgOrders.Visible = False
    29.             btnSaveOrder.Visible = False
    30.         Else
    31.             dgOrders.Visible = True
    32.             btnSaveOrder.Visible = True
    33.         End If
    34.  
    35.     End Sub
    36.  
    37.  
    38.  
    39.     Public Sub SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs)
    40.         Dim lb As New LinkButton
    41.         lb = CType(e.Item.Cells(0).Controls(0), LinkButton)
    42.         Server.Transfer("AInvoiceDetails.aspx" & "?OrderId=" & lb.Text)
    43.         'Response.Write("Text" & lb.Text)
    44.     End Sub
    45.  
    46.  
    47.  
    48.     Private Sub btnSaveOrder_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSaveOrder.Click
    49.         'Here I want to make something nice for each checkbox that is checked
    50.  
    51.     End Sub


    Im really not sure what you mean... the data is bound to the grid... and the savebutton doesn't perform a postback?? Because when I click the button the data is still bound to the grid!

    anyone with an explanation???

    kind regards
    Henrik

  6. #6
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    Your save button must be posting back, because unless you wrote JScript, (and the fact that you have Private Sub btnSaveOrder_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSaveOrder.Click
    in your code means your handling it on a postback.)

    What I was getting at was the Checkboxes may not exist inside your datagrid at the time you are checking (the SaveOrder_Click function), because the DataGrid hasn't rendered yet.

    Try putting this in the Page_Init event (it may solve it), or it may not:
    EnsureChildControls()

  7. #7
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    Otherwise try this code in your savebutton-click event (grabbed from mspress Programming VB.NET):
    VB Code:
    1. Dim dg as DataGridItem
    2. For Each dg In dgrCompanies.Items 'find the checkbox in the row
    3. Dim cb As CheckBox = DirectCast(dg.FindControl ( "chkSelect"),CheckBox) 'replace chkSelect with name of your checkbox
    4.    'if checked
    5.    IF cb.Checked Then
    6.     'do something
    7.    End if
    8. Next

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width