现在的位置: 首页 > 综合 > 正文

BINDING A GRID THROUGH 3 WAYS

2013年06月07日 ⁄ 综合 ⁄ 共 8579字 ⁄ 字号 评论关闭

The image below illustrates all the data binding methods will be mentioned :

FRIST  Using .NET Data Providers

Three objects must be created to supply a grid control with database data. First, a connection object is
needed to specify the database location and access method. Next, a data adapter object must be created.
Data adapters serve as a bridge between physical data and a dataset. They use connection objects to
access databases. Once a data adapter object is created and configured, it can generate a DataSet
object - the last object that needs to be created. This object actually supplies the grid with data.

There are three ways to bind a grid control to DataSet data.

BINDING A GRID TO AN MDB TABLE.

A DESIGN-TIME SAMPLE


 

As stated above, three objects should be created for binding to data. When connecting to an MDB table, you will need the OleDBConnection and OleDBDataAdapter objects. The DataSet object will be generated by using a data adapter. Note that you have two alternatives for creating a connection object. First, place an OleDBConnection component onto a form and customize it. The alternative is to use the OleDBDataAdapter component's wizard. This wizard lets you create the connection object during data adapter configuration. This topic will use the second method.

Follow the steps below to bind a grid control to the Products table in the NWind database:

  • Create a new Windows Application project.
  • Double-click the OleDBDataAdapter component within the Toolbox. Click the Next button in the Data Adapter Configuration Wizard window invoked.

  • Click the New Connection... button to invoke the Data Link Properties dialog.

    In the dialog's Provider page, choose the Microsoft Jet 4.0 OLE DB Provider option. Switch to the Connection page and provide the path of the desired MDB file. Click the OK button to close the dialog.

  • Click the Next button twice. Type the 'SELECT * FROM Products' statement into the memo and click the Next button once again.

  • Click the Finish button to close the wizard. As a result, the configured OleDBConnection and OleDBDataAdapter components have been added to the form.

  • Right-click the data adapter component and choose the Generate Dataset... item of the context menu. In the dialog invoked, click the OK button. As a result, the DataSet component will be generated.

  • Place a GridControl onto a form and set its GridControl.DataSource property to the dataset's Products table.

    Note that after binding the table to the grid, columns are created automatically for all table fields when the application is started. However, you also have the option of creating the column set manually. Please refer to the Creating Columns and Binding Them to Data Fields topic for details.

  • Write the following code line for the form's Load event handler.

  • private void Form1_Load(object sender, System.EventArgs e) {
       oleDbDataAdapter1.Fill(dataSet11);
    }
    
  • Run the application. The grid control will look similar to the one displayed below.

 

A RUNTIME SAMPLE


 

The code below can be used to bind a grid control to the Products table of the NWind database. This produces the same result as the design time example above.

using System.Data.OleDb;
// ...
// creating a connection object
OleDbConnection connection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data 
Source=E://DBs//NWIND.MDB"); // creating a data adapter OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT * FROM Products", connection); // creating and filling a dataset DataSet sourceDataSet = new DataSet(); adapter.Fill(sourceDataSet); // specifying the data source for the grid control gridControl1.DataSource = sourceDataSet.Tables[0];
SECOND Binding to XML Data
To bind a grid control to XML data, you need to create a DataSet object. This object provides methods
allowing you to load XML files, streams containing XML data, etc. Once data is loaded into the DataSet,
you can initialize the grid's
GridControl.DataSource property with a table from that DataSet or the table's
bound data view. Alternatively, you can set the
GridControl.DataSource and GridControl.DataMember
properties to the DataSet and the desired table name.
 

The image below illustrates binding to XML data.

When your grid is bound to XML data, changing grid cell values does not update the data source. Data
changes are actually applied to the DataSet where the data was loaded. To apply changes to the data
source, use the methods provided by the DataSet class. For instance, if data has been loaded from an
XML file using the ReadXML method, you need to call the WriteXML method to overwrite that file.

Note that binding to XML data can only be done at runtime. The grid control, however, allows you to
preview data at design time. To do this, you need to create columns and invoke the grid designer's
Layout Designer page. It provides the Load Data from XML page which can be used to load the data
and the Grid Preview page to display it.

 

The code sample below binds a grid control to XML data stored in a file. The last code line creates
columns for all fields within the bound data source. As stated above, as an alternative you can create
columns at design time. Please refer to the
Creating Columns and Binding Them to Data Fields topic
for details.


// creating a DataSet and filling it with data from an XML file DataSet xmlDataSet = new DataSet(); xmlDataSet.ReadXml("e://DBs//Cars.xml"); // binding the grid to the DataSet gridControl1.DataSource = xmlDataSet.Tables[0]; // creating columns for all data source fields gridControl1.MainView.PopulateColumns();

THIRD Binding to Data Created at Runtime 
 

Binding a grid control is not restricted to data already stored in a database or file. Data can also be
created and supplied to the XtraGrid at runtime and this topic describes the process. To learn about
other data binding methods, refer to the
Data Binding Overview topic.

 

SUPPLYING A GRID WITH DATA CREATED AT RUNTIME.


 

This data binding mode requires that the grid's GridControl.DataSource property accepts an object
holding a list of "records". Each "record" is an object whose public properties are considered to be
record fields and property values are field values. You will need to follow the steps below to supply
your grid control with data created at runtime:

  • Declare a class whose instances will represent records. This class's public properties will be
    treated as fields.
  • Declare a class implementing the IList, ITypedList or IBindingList interface. This class will
    provide a list serving as the grid's data source. This list's elements must be record objects.

    Note: the differences between the IList, ITypedList and IBindingList interfaces are described
    below. If you don't want to create your own list object, you can use any of the existing objects
    implementing these interfaces. For instance, an ArrayList object can serve as the grid's data
    source. Thus, this step is optional.

  • Create a new list instance and fill it with records. Assign the list to the grid's
    GridControl.DataSource property.

As stated above, the grid's data source must implement one of the three interfaces. The list below
describes the difference between data sources implementing each interface type.

  • Objects implementing the IList interface. Such data sources must have at least one "record".
    Otherwise, bound views will not be able to create any rows. Views bound to such data sources
    will not be notified of data changes and thus must be updated manually (for instance, by calling
    the
    BaseView.LayoutChanged method).
  • Objects implementing the ITypedList interface. In this case, it is not necessary to have any
    "records" for rows to be created. Data change notifications are not supported.
  • Objects implementing the IBindingList interface (derived from the IList). This data source type
    does not have the shortcomings of the other interfaces. The interface declares the ListChanged
    event which is used by the control to update itself when bound data is changed.

In versions prior to 3.xx of the XtraGrid the IXtraList interface could be implemented for a custom data
source to speed up the sorting and grouping operations. Now this interface is obsolete since the
XtraGrid sorts data itself using an optimized algorithm.

 

SUPPLYING A GRID WITH DATA CREATED AT RUNTIME ( A SAMPLE)


 

First, a class that represents an individual record should be declared. The code below declares a class
with the ID, Name and Age public properties. These properties will be data source fields. Note that
the ID property is declared as read-only. As a result, its associated column will also be read-only.

public class Record {
   int id, age;
   string name;
   public Record(int id, string name, int age) {
      this.id = id;
      this.name = name;
      this.age = age;
   }
   public int ID { get { return id; } }
   public string Name {
      get { return name; }
      set { name = value; }
   }
   public int Age {
      get { return age; }
      set { age = value; }
   }
}

Once the record class is declared, the data source object can be filled with records.
This example will use an ArrayList as the grid's data source. Thus, there is no need
to create a custom object implementing the IList, ITypedList or IBindingList interf-
ace.

The code below fills an ArrayList with records and assigns it to the grid's
GridControl.DataSource property. It also calls the BaseView.PopulateColumns method
to create columns for all the data source fields. Please refer to the Creating
Columns and Binding Them to Data Fields
topic for more information on column creation.

ArrayList listDataSource = new ArrayList();
listDataSource.Add(new Record(1, "Jane", 19));
listDataSource.Add(new Record(2, "Joe", 30));
listDataSource.Add(new Record(3, "Bill", 15));
listDataSource.Add(new Record(4, "Michael", 42));
gridControl1.DataSource = listDataSource;
gridControl1.MainView.PopulateColumns();
After the above code has been executed, the grid will look similar to the one displayed
below.

抱歉!评论已关闭.