请大家结合MSDN的例子,说一下DataGrid设置数据源,何时使用DataSource、DataMember属性,何时使用SetDataBinding方法?DataSource、DataMember的例子: private void BindToDataView(DataGrid myGrid){ // Create a DataView using the DataTable. DataTable myTable = new DataTable("Suppliers"); // Insert code to create and populate columns. DataView myDataView = new DataView(myTable); myGrid.DataSource = myDataView; } private void BindToDataSet(DataGrid myGrid){ // Create a DataSet. DataSet myDataSet = new DataSet("myDataSet"); // Insert code to populate DataSet with several tables. myGrid.DataSource = myDataSet; // Use the DataMember property to specify the DataTable. myGrid.DataMember = "Suppliers"; } private DataView GetDataViewFromDataSource(){ // Create a DataTable variable, and set it to the DataSource. DataView myDataView; myDataView = (DataView) dataGrid1.DataSource; return myDataView; } private DataSet GetDataSetFromDataSource(){ // Create a DataSet variable, and set it to the DataSource. DataSet myDataSet; myDataSet = (DataSet) dataGrid1.DataSource; return myDataSet; } SetDataBinding方法的例子: private void BindControls(){ // Creates a DataSet named SuppliersProducts. DataSet SuppliersProducts = new DataSet("SuppliersProducts"); // Adds two DataTable objects, Suppliers and Products. SuppliersProducts.Tables.Add(new DataTable("Suppliers")); SuppliersProducts.Tables.Add(new DataTable("Products")); // Insert code to add DataColumn objects. // Insert code to fill tables with columns and data. // Binds the DataGrid to the DataSet, displaying the Suppliers table. dataGrid1.SetDataBinding(SuppliersProducts, "Suppliers"); }
运行时:是指双击后,程序在运行中ing...;
private void BindToDataView(DataGrid myGrid){
// Create a DataView using the DataTable.
DataTable myTable = new DataTable("Suppliers");
// Insert code to create and populate columns.
DataView myDataView = new DataView(myTable);
myGrid.DataSource = myDataView;
}
private void BindToDataSet(DataGrid myGrid){
// Create a DataSet.
DataSet myDataSet = new DataSet("myDataSet");
// Insert code to populate DataSet with several tables.
myGrid.DataSource = myDataSet;
// Use the DataMember property to specify the DataTable.
myGrid.DataMember = "Suppliers";
}
private DataView GetDataViewFromDataSource(){
// Create a DataTable variable, and set it to the DataSource.
DataView myDataView;
myDataView = (DataView) dataGrid1.DataSource;
return myDataView;
}
private DataSet GetDataSetFromDataSource(){
// Create a DataSet variable, and set it to the DataSource.
DataSet myDataSet;
myDataSet = (DataSet) dataGrid1.DataSource;
return myDataSet;
}
SetDataBinding方法的例子:
private void BindControls(){
// Creates a DataSet named SuppliersProducts.
DataSet SuppliersProducts = new DataSet("SuppliersProducts");
// Adds two DataTable objects, Suppliers and Products.
SuppliersProducts.Tables.Add(new DataTable("Suppliers"));
SuppliersProducts.Tables.Add(new DataTable("Products"));
// Insert code to add DataColumn objects.
// Insert code to fill tables with columns and data.
// Binds the DataGrid to the DataSet, displaying the Suppliers table.
dataGrid1.SetDataBinding(SuppliersProducts, "Suppliers");
}
DataGrid设置数据源,何时使用DataSource、DataMember属性,何时使用SetDataBinding方法?