Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / March 2008

Tip: Looking for answers? Try searching our database.

Dynamic BoundColumn

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
RN1 - 16 Mar 2008 20:36 GMT
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
Stan - 16 Mar 2008 22:14 GMT
> This is how I am adding a BoundColumn to a DataGrid dynamically:
>
[quoted text clipped - 90 lines]
>
> Ron

Hi

Try setting the "AutoGenerateColumns" property to false.
RN1 - 17 Mar 2008 01:20 GMT
> > This is how I am adding a BoundColumn to a DataGrid dynamically:
>
[quoted text clipped - 96 lines]
>
> - 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

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.