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">
<aspataGrid 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>
</aspataGrid>
</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
--------------------------------------------------------------------------------
<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">
<aspataGrid 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>
</aspataGrid>
</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