Bind OLEDBDataAdapter to DataGrid under WinForm

zhaozj2021-02-16  49

Bind OLEDBDataAdapter to the DataGrid under WinForm, this statement is a bit strange: you may want to see it, I have used it to write it in the evening, and I don't want everyone to think.

Of course, there is such a binding method, then we can also inherit the DataGrid binding SqlDataAdapter. You may try a try, and if you don't write, you will take the jade.

Related Attributes and Events:

CustomDataGrid instantiate a new CustomDataGrid controls for OleDB CustomDataGrid CustomDataGrid instantiating a new control for OleDB DataGridOleDbDataAdapter CustomDataGrid to control the assignment of new OleDbDataAdapter FillDataSetAndBindAfterBindOleDBDataAdapter CustomDataGrid BindOleDbDataAdapter whether new data DataBind CustomDataGrid passed after the new value of OleDBDataAdapter Binding Update Submit all Cancelupdate Abandon All Change Deleterows Delete All Selection (Delete does not affect the original data, if you need to update the database) EnableSelect does not allow multiple options (related to delete, related to interface) ConstructDataGridDisplayStyles created according to DataSet data type Data display mode addSelectRow adds new columns to select the title of the tabletittle attribute corresponding table

Using system.collections; using system.drawing; using system.data; using system.windows.form;

Namespace Forward.winui {///

/// CustomDataGrid summary description. /// public class customDataGrid: system.windows.Forms.DataGrid {/// // The required designer variable. /// private system.componentmodel.container components = null;

// This is the OLEDBDATAADB.OLDBDATAADAPTER BINDB.OLDBDATAADAPTER BINDB.OLDBDATAADAPTER Bindo.oledbdataAdapter;

// This is the DataSet object, which is included in this DataGrid, private system.data.dataSet ContentDataSet;

// CustomDataGrid's BindoledBDataAdapter is incorporated in the new value to display new data private bool fillafterbindadp;

// Allow multi-line selection private bool enablemultiselectriselect;

// Each column name private string [] title, ///

/// instantizes a new CustomDataGrid control for OLEDB /// public customDataGrid () {// This call is Windows The. Forms Form Designer is required. InitializationComponent ();

// Todo: Add any initialization after InitializationComponent call} ///

// / instantiate a new CustomDataGrid control for OLEDB /// /// Introduced OLEDBDataAdapter Public CustomDataGrid (System.Data.oledb.oledbDataAdapter Bindoledbadp) {// This call is required for the Windows.Forms Form Designer. InitializationComponent ();

// Todo: Add any initialization bindoledbadp = this.DataGridoledbdataadapter;} in the initializationComponent call.

///

/// Clean all the resources being used. /// Protected Override Void Dispose (Bool Disposing) {if (disponents! = Null) Components.dispose ();} Base.Dispose (Disposing);

#Region Component Designer Generated Code ///

/// Designer Supports the required method - Do not use the code editor // to modify the contents of this method. /// private () {components = new system.componentmodel.container (); this.contentDataSet = new system.data.dataset (); title = null;} #ENDREGON

CustomDataGrid #region to control the assignment of new OleDbDataAdapter ///

/// CustomDataGrid to control the assignment of new OleDbDataAdapter /// public System.Data.OleDb.OleDbDataAdapter DataGridOleDbDataAdapter {get {return this.BindOleDbDataAdapter;} Set {bindoledbdataadapter = value; // Whether to populate the dataset and display the IF (FillafterbindadP) {databind ();}}

} #Endregion

#region FillDataSetAndBindAfterBindOleDBDataAdapter CustomDataGrid BindOleDbDataAdapter whether the new data ///

/// CustomDataGrid after passing the new value of the new data is displayed BindOleDbDataAdapter /// public bool FillDataSetAndBindAfterBindOleDBDataAdapter {get the new value of the incoming {Return FillafterbindAdP;} set {filmfterbindadp = value;}} #endregion

#region DataBind CustomDataGrid OleDBDataAdapter bound to the ///

/// CustomDataGrid OleDBDataAdapter bound to the /// public void DataBind () {if (this.BindOleDbDataAdapter! = null) {this.ContentDataSet .Tables.Clear (); BindOleDbDataAdapter.Fill (this.ContentDataSet); this.DataSource = null; constructDataGridDisplayStyles (); this.DataSource = this.ContentDataSet.Tables [0];}} # endregion # region Update commit all changes / // /// submits all changes /// public new void update () {if (bindoledbdataadapter! = null) {if (this.contentDataSet.tables [0]! = null) {TRY {

BindoledBDataAdapter.Update ((System.Data.DataTable);} catch (system.exception ex) {messagebox.show (this, ex. amount ,.toString ());

} Finally {

}}}} #ENDREGION

#Region Cancelupdate Abandon All Change ///

/// Abandon All Change /// Public Void Cancelupdate () {databind ();} #ENDREGION

#Region Delete Delete All Selection Item ///

// Delete All Selection (Delete does not affect the original data, you must update the database) /// public void deleterows () {if (EnableMultiTISElectr ) {IF (this.bindoledbdataadapter! = Null) {

INT i = 0; BOOL [] Row; Row = New Bool [(System.Data.DataTable) this.datasource) .Rows.count]; Foreach (DataRow Dr in ((System.Data.DataTable) this.DataSource .ROWS) {IF (DR ["SELECT"])) {row [i] = true;} else {row [i] = false;} i ;}

For (i = (system.data.dataable) this.datasource) .Rows.count-1; i> = 0; I ---) {if (convert.toboolean (row [i])) {((System. Data.DataTable) .datasource) .rows [i] .delete (); // ((System.Data.DataTable) this.datasource) .Rows.removeat (i);}}}}

} #Endregion

#REGION EnableSelect Do you allow multiple options (related to deletion {Return enablemultiselectriselect;} set {enablemultiselectr = value;}} #ENDREGON

#REGON CONSTRUCTDATAGRIDDISPLAYSTYLES Create data display mode ///

// / ////////mmary> private vid constructdataGridDisplayStyles () {ix (this.contentDataSet.tables [0 "according to DataSet data type ]! = null) {system.windows.Forms.DataGridColumnStyle [] tempdgcs;

INT count; int currcount = 0;

IF (enablemultiselect) {count = this.contentDataSet.Tables [0] .COLUMNS.COUNT 1;} else {count = this.contentDataSet.tables [0] .columns.count;

EnableMultiselect) {addselectrow ();

Tempdgcs = new system.windows.Forms.DataGridColumnStyle [count];

if (EnableMultiSelect) {tempDGCS [0] = new System.Windows.Forms.DataGridBoolColumn (); tempDGCS [0] .HeaderText = "Select"; tempDGCS [0] .Width = 80;} foreach (DataColumn dc in this.ContentDataSet .Tables [0] .Columns) {switch (dc.DataType.ToString ()) {case "System.Boolean": tempDGCS [currCount] = new System.Windows.Forms.DataGridBoolColumn (); tempDGCS [currCount] .MappingName = dc.ColumnName; break; default: tempDGCS [currCount] = new System.Windows.Forms.DataGridTextBoxColumn (); tempDGCS [currCount] .MappingName = dc.ColumnName; break;} currCount ;}

System.windows.Forms.DataGridtableStyle Tempdgts = New DataGridTableStyle ();

// This is used to join the new column IF (TEMPDGTS.GRIDCOLUMNSTYLES.ADD (Tempdgcs [count-1]) for doing the selection;} int J; j = 0; if (this.enableMultiselect) { J = count-1;} else {j = count;}

For (int i = 0; i = i) {tempDgts.gridcolumnStyles [i] .Headertext = titsetame [i];}}}

THIS.TABLESTYLES.CLEAR (); tempdgts.mappingname = this.contentDataSet.Tables [0] .tablename; THIS.TABLESTYLES.ADD (TEMPDGTS); tempdgts.dispose ();}} #ENDREGION

///

/// Add new column, used to select /// private void addselectrow () {Datacolumn DC = New Datacolumn ("SELECT", System.Type.gettype ("System.Boolean" )); Dc.defaultValue = false; this.contentDataSet.Tables [0] .COLUMNS.ADD (DC);} /// /// public string [ ] Tabletittle {get {return title}} set {tittlename = value;}}}}}}}}}}}}}}

Welcome to advice, thank you!

转载请注明原文地址:https://www.9cbs.com/read-24775.html

New Post(0)