e.Item.Cells.Count in UpdateCommand event of Datagrid

Discussion in 'ASP .Net Web Controls' started by VBTECH, Apr 21, 2005.

  1. VBTECH

    VBTECH Guest

    I have a datagrid in an ASp.net application. Only one column is created at
    design time, rest of the columns are created and added to grid on the fly.

    When I try to update values in datagrid through UpdateCommand event code
    errors out.
    e.Item.Cells.Count returns 1 even though I have more columns.


    Here is the sample code for creating and adding columns on the fly.

    Private Sub GridLoad()
    Me.grdProjDetail.AutoGenerateColumns = False
    Dim ds As New DataSet
    ds = ReturnDataset_StoredProc("usp_StoredProc", Param1, Param2)

    Me.grdProjDetail.DataSource = ds

    'Create & add columns to DataGrid
    Dim col As BoundColumn

    'ProjectionDetailID
    col = New BoundColumn
    col.DataField = "ProjectionDetailID"
    Me.grdProjDetail.Columns.Add(col)

    'UpdateID
    col = New BoundColumn
    col.DataField = "UpdateID"
    Me.grdProjDetail.Columns.Add(col)

    'ItemNo
    col = New BoundColumn
    col.DataField = "ItemNo"
    Me.grdProjDetail.Columns.Add(col)

    'QuarterID
    col = New BoundColumn
    col.DataField = "QuarterID"
    Me.grdProjDetail.Columns.Add(col)

    'ITEM COLOR
    col = New BoundColumn
    col.DataField = "ITEMCOLOR"
    Me.grdProjDetail.Columns.Add(col)

    Me.grdProjDetail.DataBind()
    End Sub


    Your help is appreciated.
     
    VBTECH, Apr 21, 2005
    #1
    1. Advertising

  2. VBTECH

    Brock Allen Guest

    When you dynamically build controls (or columns in this case) on the server,
    you need to do this for every post back tot he server. So you need to rebuild
    the columns for the grid. This is typically done in Page_Init or by overriding
    CreateChildControls.

    -Brock
    DevelopMentor
    http://staff.develop.com/ballen



    > I have a datagrid in an ASp.net application. Only one column is
    > created at design time, rest of the columns are created and added to
    > grid on the fly.
    >
    > When I try to update values in datagrid through UpdateCommand event
    > code
    > errors out.
    > e.Item.Cells.Count returns 1 even though I have more columns.
    > Here is the sample code for creating and adding columns on the fly.
    >
    > Private Sub GridLoad()
    > Me.grdProjDetail.AutoGenerateColumns = False
    > Dim ds As New DataSet
    > ds = ReturnDataset_StoredProc("usp_StoredProc", Param1,
    > Param2)
    > Me.grdProjDetail.DataSource = ds
    >
    > 'Create & add columns to DataGrid
    > Dim col As BoundColumn
    > 'ProjectionDetailID
    > col = New BoundColumn
    > col.DataField = "ProjectionDetailID"
    > Me.grdProjDetail.Columns.Add(col)
    > 'UpdateID
    > col = New BoundColumn
    > col.DataField = "UpdateID"
    > Me.grdProjDetail.Columns.Add(col)
    > 'ItemNo
    > col = New BoundColumn
    > col.DataField = "ItemNo"
    > Me.grdProjDetail.Columns.Add(col)
    > 'QuarterID
    > col = New BoundColumn
    > col.DataField = "QuarterID"
    > Me.grdProjDetail.Columns.Add(col)
    > 'ITEM COLOR
    > col = New BoundColumn
    > col.DataField = "ITEMCOLOR"
    > Me.grdProjDetail.Columns.Add(col)
    > Me.grdProjDetail.DataBind()
    > End Sub
    > Your help is appreciated.
    >
     
    Brock Allen, Apr 21, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Learning SQL Server
    Replies:
    2
    Views:
    2,483
    Learning SQL Server
    Aug 7, 2003
  2. John A Grandy
    Replies:
    0
    Views:
    501
    John A Grandy
    Nov 22, 2004
  3. JC
    Replies:
    0
    Views:
    231
  4. srathin

    DataGrid1 UpdateCommand calling DataGrid2 UpdateCommand

    srathin, Apr 9, 2005, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    179
    srathin
    Apr 9, 2005
  5. srathin

    DataGrid1 UpdateCommand calling DataGrid2 UpdateCommand

    srathin, Apr 9, 2005, in forum: ASP .Net Web Controls
    Replies:
    2
    Views:
    164
    srathin
    Apr 11, 2005
Loading...

Share This Page