ComboBox, DateTimePicker Application in DataGrid (C #) ...

xiaoxiao2021-03-06  14

ComboBox, DateTimePicker Application in DataGrid (C #) ...

To use ComboBox, DateTimePicker in DataGrid, I can rewrite System.Windows.Forms.DataGridColumnStyle to do ....

(Please click on the VB.NET section)

First, bind ComboBox with DataGridColumnStyle, then rewrite DataGridColumnStyle ... Code and Under:

Using system;

Using system.windows.forms;

Using system.drawing;

Namespace Xuicontrols

{

///

/// Summary Description for DataGridComboBoxColumnStyle.

///

Public Class DataGridComboBoxColumnStyle: System.Windows.Forms.DataGridColumnStyle

{

Private ComboBox X_ComboBox = New ComboBox ();

Private bool iSEDITING;

Public DataGridcomboBoxColumnStyle (): base ()

{

//

// Todo: add constructor logic here

//

X_comboBox.visible = false;

}

Protected Override Void Abort (int Rownum)

{

ISEDITING = FALSE;

X_comboBox.click- = New EventHandler (this.comboBoxValueChanged);

Invalidate ();

}

Protected Override Bool Commit (CurrencyManager DataSource, int Rownum)

{

X_comboBox.bounds = Rectangle.empty;

X_comboBox.click- = New EventHandler (this.comboBoxValueChanged);

IF (! iSediting)

Return True;

ISEDITING = FALSE;

Try

{

String value = x_comboBox.Text;

SetColumnValueatrow (DataSource, Rownum, Value);

}

Catch (Exception)

{

Abort (rownum);

Return False;

}

Invalidate ();

Return True;

}

Protected Override Void Edit (CurrencyManager Source, Int Rownum, Rectangle Bounds, Bool Readonly, String InstantText, Bool Cellisvisible)

{

String value = (string) getColumnValueatrow (Source, Rownum);

IF (CellisVisible)

{

X_comboBox.bounds = New Rectangle

(Bounds.x 2, Bounds.y 2, Bounds.Width-4, Bounds.Height-4);

X_comboBox.text = value;

X_comboBox.visible = true;

X_comboBox.click = new eventhandler (this.comboBoxValueChanged);

}

Else

{

X_comboBox.text = value;

X_comboBox.visible = false;

}

IF (x_comboBox.visible)

DataGridtablesTyle.DataGrid.invalidate (Bounds);

}

Protected Override Void Paint (Graphics G, Rectangle Bounds, CurrencyManager Source, Int Rownum)

{

Paint (g, bounds, source, rownum, false);

}

Protected Override Void Paint

Graphics g,

Rectangle Bounds,

CurrencyManager Source,

Int rownum,

Bool aligntoright)

{

Paint

g, bounds,

Source,

Rownum,

Brushes.red,

Brushes.blue,

Aligntoright);

}

Protected Override Void Paint

Graphics g,

Rectangle Bounds,

CurrencyManager Source,

Int rownum,

Brush backbrush,

Brush forebrush,

Bool aligntoright)

{

String Date = (String)

GetColumnValueatrow (Source, Rownum);

Rectangle Rect = Bounds;

g.fillRectangle (Backbrush, Rect);

Rect.offset (0, 2);

Rect.height- = 2;

g.drawstring (date, this.datagridtablestyle.DataGrid.font, forebrush, rect);

//g.drawstring(date ,this.dataGridtablestyle.DataGrid.font ,stract.y); // ok

}

Protected Override Size GetPreferRedsize (Graphics G, Object Value)

{

Return New Size (100, 24);

}

Protected Override Int getminimumHeight ()

{

Return 24;

}

Protected Override int GETPREFERREDHEIGHT (Graphics G, Object Value)

{

Return 24;

}

Protected Override Void SetDataGridIncolumn (DataGrid Value)

{

Base.setDataGridincolumn (value);

IF (x_comboBox.parent! = null)

{

X_comboBox.parent.controls.remove (x_comboBox);

}

IF (value! = null)

{

Value.controls.add (x_comboBox);

}

}

Private Void ComboBoxValueChanged (Object Sender, Eventargs E)

{

THIS.IsEDITING = True;

Base.columnStartedEditing (x_comboBox);

}

Public void additem (String strItemname)

{

X_comboBox.Items.Add (stritemname);

}

}

}

Second, bind DateTimePicker with DataGridColumnStyle, then rewrite the DataGridColumnStyle ... Code and below: USING SYSTEM;

Using system.windows.forms;

Using system.drawing;

Namespace Xuicontrols

{

///

/// Summary Description for DataGriddateTimePickerColumnStyle.

///

Public Class DataGriddateTimePickerColumnStyle: DataGridColumnStyle

{

Private datepicker x_datetimepicker = new datetimepicker ();

Private bool iSEDITING;

Public DataGridDateTimePickerColumnStyle (): base ()

{

//

// Todo: add constructor logic here

//

X_datetimePicker.visible = false;

}

Protected Override Void Abort (int Rownum)

{

ISEDITING = FALSE;

X_datetimePicker.ValueChanged- = New EventHandler (this.timePickerValueChanged);

Invalidate ();

}

Protected Override Bool Commit (CurrencyManager DataSource, int Rownum)

{

X_DatetimePicker.bounds = Rectangle.empty;

X_datetimePicker.ValueChanged- = New EventHandler (this.timePickerValueChanged);

IF (! iSediting)

{

Return True;

}

ISEDITING = FALSE;

Try

{

System.datetime value. = X_datetimepicker.value;

SetColumnValueatrow (Datasource, Rownum, Values);

}

Catch (Exception)

{

Abort (rownum);

Return False;

}

Invalidate ();

Return True;

}

Protected Override Void Edit (CurrencyManager Source, Int Rownum, Rectangle Bounds, Bool Readonly, String InstantText, Bool Cellisvisible)

{

System.Datetime value = (datetime) getColumnValueatrow (Source, Rownum);

IF (CellisVisible)

{

X_DatetimePicker.bounds = New Rectangle

(Bounds.x 2, Bounds.y 2, Bounds.Width-4, Bounds.Height-4);

X_DatetimePicker.Value = Value;

X_DatetimePicker.visible = true;

X_DatetimePicker.ValueChanged = New EventHandler (this.timePickerValueChanged);

}

Else

{

X_datetimePicker.Value = value; x_datetimepicker.visible = false;

}

IF (x_datetimepicker.visible)

DataGridtablesTyle.DataGrid.invalidate (Bounds);

}

Protected Override Void Paint (Graphics G, Rectangle Bounds, CurrencyManager Source, Int Rownum)

{

Paint (g, bounds, source, rownum, false);

}

Protected Override Void Paint (Graphics G, Rectangle Bounds, CurrencyManager Source, Int Rownum, Bool Aligntoright)

{

Paint (G, Bounds, Source, Rownum, Brushes.Red, Brushes.blue, Aligntoright);

}

Protected Override Void Paint (Graphics G, Rectangle Bounds, Currencymanager Source, Int Rownum, Brush Backbrush, Brush Forebrush, Bool Aligntoright)

{

System.datetime Date = (datetime)

GetColumnValueatrow (Source, Rownum);

Rectangle Rect = Bounds;

g.fillRectangle (Backbrush, Rect);

Rect.offset (0, 2);

Rect.height- = 2;

g.drawstring (Date.Tostring ("D"), this.dataGridtablestyle.DataGrid.Font, Forebrush, Rect);

}

Protected Override Size GetPreferRedsize (Graphics G, Object Value)

{

Return New Size (100, 24);

}

Protected Override Int getminimumHeight ()

{

Return 24;

}

Protected Override int GETPREFERREDHEIGHT (Graphics G, Object Value)

{

Return 24;

}

Protected Override Void SetDataGridIncolumn (DataGrid Value)

{

Base.setDataGridincolumn (value);

IF (x_datetimepicker.parent! = null)

{

X_DatetimePicker.parent.controls.remove (x_datetimepicker);

}

IF (value! = null)

{

Value.controls.add (x_datetimepicker);

}

}

Private Void TimePickerValueChanged (Object Sender, Eventargs E)

{

THIS.IsEDITING = True;

Base.columnStartedEditing (x_datetimepicker);

}

}

}

● At this point, the two rewritten UiControl has been completed, and now create a new form, put a DataGird in the Form, we will implement the use of ComboBox, DateTimePicker, CheckBox ....

Code section:

Using system;

Using system.drawing; using system.collections;

Using system.componentmodel;

Using system.windows.forms;

Using system.data;

Namespace XForms

{

///

/// Summary description for form_diary.

///

Public class form_diary: system.windows.Forms.form

{

Private my_diarys.userControls.userControl_Header UserControl_Header1;

Private system.windows.forms.DataGrid DataGrid1;

Private DataTable namesDataTable;

Private Xuicontrols.xButton Button1;

///

/// Required Designer Variable.

///

Private system.componentmodel.Container Components = NULL;

Public form_diary ()

{

//

// Required for Windows Form Designer Support

//

InitializationComponent ();

//

// Todo: Add Any Constructionor Code After InitializationComponent Call

//

}

///

/// Clean Up Any Resources Being Used.

///

Protected Override Void Dispose (Bool Disposing)

{

IF (Disposing)

{

IF (Components! = NULL)

{

Components.dispose ();

}

}

Base.dispose (Disposing);

}

#Region Windows Form Designer Generated Code

///

/// Required Method for Designer Support - Do Not Modify

/// The contents of this method with the code editor.

///

Private vidinitiRizeComponent ()

{

This.userControl_Header1 = new my_diarys.userControls.userControl_Header ();

This.DataGrid1 = new system.windows.forms.dataGrid ();

THIS.BUTTON1 = New Xuicontrols.xbutton ();

(System.comPonentModel.isupportInitialize) (DataAgrid1)). Begininit ();

THIS.SUSPENDLAYOUT ();

//

// UserControl_Header1

//

This.userControl_Header1.Location = new system.drawing.point (0, 0);

This.userControl_Header1.name = "UserControl_Header1";

This.userControl_Header1.size = new system.drawing.size (162, 28);

this.userControl_Header1.tabindex = 0;

//

// DataGrid1

//

THIS.DATAGRID1.DATAMEMBER = "";

This.DataGrid1.HeaderForeColor = system.drawing.systemcolors.ControlText;

This.DataGrid1.location = new system.drawing.point (10, 102);

This.DataGrid1.name = "datagrid1";

This.DataGrid1.size = new system.drawing.size (528, 198);

this.DataGrid1.tabindex = 1;

//

// Button1

//

This.button1.location = new system.drawing.point (408, 378);

This.button1.name = "button1";

this.button1.tabindex = 2;

This.Button1.Text = "Button1";

//

// form_diary

//

THIS.AUTOSCALEBASESIZE = New System.drawing.size (5, 13);

THIS.CLIENTSIZE = New System.drawing.size (792, 547);

This.Controls.add (this.button1);

This.Controls.add (this.DataGrid1);

This.Controls.add (this.userControl_Header1);

THIS.NAME = "form_diary";

this.righttoleft = system.windows.forms.righttoleft.no;

This.Text = "form_dia";

THISLOAD = New System.EventHandler (this.form_diary_load);

(System.comPonentModel.isupportInitialize) (THISDATAGRID1)). Endinit ();

This.ResumeLayout (false);

}

#ndregion

[Stathread]

Static void

Main ()

{

Application.run (New XForms.form_Diary ());

}

Private void adddata ()

{

DataRow DROW = NamesDataTable.NewRow ();

DROW ["name"] = "name 1";

DROW ["state"] = "xxx";

DROW ["Date"] = New DateTime (2004, 11, 24);

NamesDataTable.Rows.Add (DROW);

DROW = NamesDataTable.newrow ();

DROW ["name"] = "name 2";

DROW ["Date"] = New DateTime (2004, 11, 24);

DROW ["state"] = "xxx"; namesDataTable.Rows.Add (drow);

DROW = NamesDataTable.newrow ();

DROW ["name"] = "name 3";

DROW ["state"] = "xxx";

DROW ["Date"] = New DateTime (2004, 11, 24);

NamesDataTable.Rows.Add (DROW);

DROW = NamesDataTable.newrow ();

DROW ["name"] = "name 4";

DROW ["state"] = "xxx";

DROW ["Date"] = New DateTime (2004, 11, 24);

NamesDataTable.Rows.Add (DROW);

DROW = NamesDataTable.newrow ();

DROW ["name"] = "name 5";

DROW ["state"] = "xxx";

DROW ["Date"] = New DateTime (2004, 11, 24);

NamesDataTable.Rows.Add (DROW);

NamesDataTable.acceptchanges ();

}

Private void addgridStyle ()

{

DataGridTableStyle MyGridStyle = New DataGridTableStyle ();

MyGridStyle.mappingName = "namestable";

DataGridTextBoxColumn NameColumnStyle =

New DataGridTextBoxColumn ();

NameColumnStyle.mappingname = "name";

NameColumnStyle.Headertext = "name";

MyGridStyle.GridColumnStyles.Add (NameColumnStyle);

Xuicontrols.DataGridDateTimePickerColumnStyle DateColumnStyle =

New Xuicontrols.DataGridDatetimePickerColumnStyle ();

DateColumnStyle.mappingName = "Date";

DatecolumnStyle.Headertext = "Date";

MyGridStyle.GridColumnStyles.Add (DateColumnStyle);

Xuicontrols.DataGridcomboBoxColumnStyle CMBStyle = New Xuicontrols.DataGridComboBoxColumnStyle ();

CMBStyle.mappingName = "state";

CMBStyle.Headertext = "state";

CMBStyle.Width = 100;

CMBStyle.Additem ("xxx");

CMBStyle.AddItem ("1111");

MyGridStyle.GridColumnStyles.Add (CMBStyle);

This.DataGrid1.tables.add (myGridStyle);

Private void form_diary_load (Object Sender, System.EventArgs E)

{

NamesDataTable = New DataTable ("nameSTable");

NamesDataTable.columns.add (New Datacolumn ("Name"));

NamesDataTable.columns.add (New Datacolumn ("State");

// NamesDataTable.Columns.Add (New Datacolumn ("Date"));

Datacolumn datecolumn = new datacolumn

("Date", TypeOf (System.DateTime));

NamesDataTable.columns.add (datecolumn);

DataSet nameDataSet = new dataset ();

NamesDataSet.tables.add (namesdataable);

This.DataGrid1.datasource = namesdata

THIS.DATAGRID1.DATAMEMBER = "nameSTable";

AddgridStyle ();

Adddata ();

}

}

}

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

New Post(0)