ARTICLE

Changing Displayed Data at Run Time in the DataGrid Control

Posted by Dinesh Beniwal Articles | Visual Basic 2010 September 11, 2009
In this article you will learn how to Change Displayed Data at Run Time in the DataGrid Control
 
Reader Level:

Changing Displayed Data at Run Time in the Windows Forms DataGrid Control
 
After you have created a Windows Forms DataGrid using the design-time features, you may also wish to dynamically change elements of the DataSet object of the grid at run time. This can include changes to either individual values of the table or changing which data source is bound to the DataGrid control. Changes to individual values are done through the DataSet object, not the DataGrid control.

To change data programmatically

  • Specify the desired table from the DataSet object and the desired row and field from the table and set the cell equal to the new value.
    Note   To specify the first table of the DataSet or the first row of the table, use 0.

    The following example shows how to change the second entry of the first row of the first table of a dataset by clicking Button1. The DataSet (ds) and Tables (0 and 1) were previously created.

        Protected Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            ds.tables(0).rows(0)(1) = "NewEntry"
        End Sub

At run time you can use the SetDataBindings property to bind the DataGrid control to a different data source. For example, you may have several ADO.NET data controls, each connected to a different database.

To change the DataSource programmatically

  • Set the SetDataBinding method to the name of the data source and table you want to bind to.

    The following example shows how to change the date source using the SetDataBinding method to an ADO.NET data control (adoPubsAuthors) that is connected to the Authors table in the Pubs database.

        Private Sub ResetSource()
            DataGrid1.SetDataBinding(adoPubsAuthors, "Authors")
        End Sub

Login to add your contents and source code to this article
share this article :
post comment
 
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor