Dynamic BoundColumn

R

RN1

This is how I am adding a BoundColumn to a DataGrid dynamically:

--------------------------------------------------------------------------------
<script runat="server">
Sub Page_Load(ByVal obj As Object, ByVal ea As EventArgs)
Call AddNewColumn()
If Not (Page.IsPostBack) Then
BindGrid()
End If
End Sub

Sub BindGrid()
Dim dSet As DataSet
Dim strSQL As String
Dim sqlConn As SqlConnection
Dim sqlDapter As SqlDataAdapter

strSQL = "SELECT * FROM TestTable"
sqlConn = New SqlConnection(".....")
sqlDapter = New SqlDataAdapter(strSQL, sqlConn)

dSet = New DataSet
sqlDapter.Fill(dSet, "DynamicDG")

dgDynamic.DataSource = dSet
dgDynamic.DataMember = "DynamicDG"
dgDynamic.DataBind()
End Sub

Sub AddNewColumn()
Dim bc As BoundColumn

bc = New BoundColumn
bc.DataField = "Marks"
bc.HeaderText = "MARKS"
End Sub

Sub EditItem(ByVal obj As Object, ByVal ea As
DataGridCommandEventArgs)
dgDynamic.EditItemIndex = ea.Item.ItemIndex
BindGrid()
End Sub

Sub CancelItem(ByVal obj As Object, ByVal ea As
DataGridCommandEventArgs)
Response.Write("CancelCommand<br>")
dgDynamic.EditItemIndex = -1
BindGrid()
End Sub

Sub UpdateItem(ByVal obj As Object, ByVal ea As
DataGridCommandEventArgs)
Response.Write("UpdateCommand<br>")
dgDynamic.EditItemIndex = -1
BindGrid()
End Sub
</script>

<form runat="server">
<asp:DataGrid ID="dgDynamic" OnCancelCommand="CancelItem"
OnEditCommand="EditItem" OnUpdateCommand="UpdateItem" runat="server">
<Columns>
<%-- 2 TemplateColumns come here --%>

<asp:EditCommandColumn CancelText="CANCEL" EditText="EDIT"
HeaderText="EDIT" UpdateText="UPDATE"/>
</Columns>
</asp:DataGrid>
</form>
--------------------------------------------------------------------------------

When the DataGrid is in the editable mode (i.e. the UPDATE & CANCEL
links are rendered instead of the EDIT link) & I click the UPDATE
link, the OnUpdateCommand event handler named UpdateItem gets
invoked...that's fine but when the CANCEL link is clicked, still the
OnUpdateCommand event handler (UpdateItem) gets invoked (instead of
the OnCancelCommand event handler)!

What's causing this eccentricity?

Please note that if I comment out the line

Call AddNewColumn()

in the Page_Load sub (in other words, I don't add the BoundColumn
dynamically to the DataGrid), then clicking the CANCEL link when the
DataGrid is in the editable mode correctly invokes the OnCancelCommand
event handler & clicking the UPDATE link invokes the OnUpdateCommand
event handler. The eccentricity that I have pointed out above results
only when the BoundColumn is added dynamically to the DataGrid.

Thanks,

Ron
 
S

Stan

This is how I am adding a BoundColumn to a DataGrid dynamically:

---------------------------------------------------------------------------­-----
<script runat="server">
    Sub Page_Load(ByVal obj As Object, ByVal ea As EventArgs)
        Call AddNewColumn()
        If Not (Page.IsPostBack) Then
            BindGrid()
        End If
    End Sub

    Sub BindGrid()
        Dim dSet As DataSet
        Dim strSQL As String
        Dim sqlConn As SqlConnection
        Dim sqlDapter As SqlDataAdapter

        strSQL = "SELECT * FROM TestTable"
        sqlConn = New SqlConnection(".....")
        sqlDapter = New SqlDataAdapter(strSQL, sqlConn)

        dSet = New DataSet
        sqlDapter.Fill(dSet, "DynamicDG")

        dgDynamic.DataSource = dSet
        dgDynamic.DataMember = "DynamicDG"
        dgDynamic.DataBind()
    End Sub

    Sub AddNewColumn()
        Dim bc As BoundColumn

        bc = New BoundColumn
        bc.DataField = "Marks"
        bc.HeaderText = "MARKS"
    End Sub

    Sub EditItem(ByVal obj As Object, ByVal ea As
DataGridCommandEventArgs)
        dgDynamic.EditItemIndex = ea.Item.ItemIndex
        BindGrid()
    End Sub

    Sub CancelItem(ByVal obj As Object, ByVal ea As
DataGridCommandEventArgs)
        Response.Write("CancelCommand<br>")
        dgDynamic.EditItemIndex = -1
        BindGrid()
    End Sub

    Sub UpdateItem(ByVal obj As Object, ByVal ea As
DataGridCommandEventArgs)
        Response.Write("UpdateCommand<br>")
        dgDynamic.EditItemIndex = -1
        BindGrid()
    End Sub
</script>

<form runat="server">
<asp:DataGrid ID="dgDynamic" OnCancelCommand="CancelItem"
OnEditCommand="EditItem" OnUpdateCommand="UpdateItem" runat="server">
<Columns>
<%-- 2 TemplateColumns come here --%>

<asp:EditCommandColumn CancelText="CANCEL" EditText="EDIT"
HeaderText="EDIT" UpdateText="UPDATE"/>
</Columns>
</asp:DataGrid>
</form>
---------------------------------------------------------------------------­-----

When the DataGrid is in the editable mode (i.e. the UPDATE & CANCEL
links are rendered instead of the EDIT link) & I click the UPDATE
link, the OnUpdateCommand event handler named UpdateItem gets
invoked...that's fine but when the CANCEL link is clicked, still the
OnUpdateCommand event handler (UpdateItem) gets invoked (instead of
the OnCancelCommand event handler)!

What's causing this eccentricity?

Please note that if I comment out the line

Call AddNewColumn()

in the Page_Load sub (in other words, I don't add the BoundColumn
dynamically to the DataGrid), then clicking the CANCEL link when the
DataGrid is in the editable mode correctly invokes the OnCancelCommand
event handler & clicking the UPDATE link invokes the OnUpdateCommand
event handler. The eccentricity that I have pointed out above results
only when the BoundColumn is added dynamically to the DataGrid.

Thanks,

Ron

Hi

Try setting the "AutoGenerateColumns" property to false.
 
R

RN1

Hi

Try setting the "AutoGenerateColumns" property to false.- Hide quoted text -

- Show quoted text -

No, Stan, setting AutoGenerateColumns to False doesn't make any
difference. Any other ideas?

BTW, have you encountered such a behavior anytime?

Thanks,

Regards,

Ron
 

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

Similar Threads

BoundColumn TextBox 2
DropDownList DataGrid 1
DataGrid Edit Problem 9
BoundColumn 1
DataBind In ItemDataBound Event 1
IsPostBack 4
DataBind 1
Insert New Record 3

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top