admin 管理员组

文章数量: 887029


2024年1月14日发(作者:unix个人系统)

Winform下DataGridView控件自定义列

idView控件是net下,数据显示使用最多的控件之一,但是Datagridviewk控件列类型却仅仅只有6中

分别是button 、checkbox、combobox、image、link、textbox 等6种常见类型。这很难满足我们日常开发需要。如果需要复杂的应用,要么找第三方控件,要么只能自己开发。而功能强大的第三方控件往往是需要付费的。但我们开发需要的很可能只是简单的功能,如果为了某个简单功能而专门购买一个控件对于个人来说有些得不偿失。

那么我们只剩下自己开发一途。幸运的是DataGridView控件容许我们进行二次开发,可以自定义我们需要的控件列。下图就是自定义日期输入自定义列,通过下面的例子,你完全可以开发出自己需要的功能列。下面给出和C#代码和原理

自定义列必须自己写三个类,这三个类必须继承系统标准的类或实现系统标准接口。

这三个类实际上代表gridview控件中的列、列中的单元格、以及单元格中的具体控件

分别继承自系统

1、DataGridViewColumn 代表表格中的列

2、DataGridViewTextBoxCell 代表列中的单元格

3、IDataGridViewEditingControl 接口,单元格控件可以几本可以继承自任何标准控件或者自定义控件,但是必须实现IDataGridViewEditingControl

下面给出vb和C#的详细案例代码

一、C# 代码

using System;

using ;

public class CalendarColumn : DataGridViewColumn

{

public CalendarColumn() : base(new CalendarCell())

{

}

public override DataGridViewCell CellTemplate

{

get

{

return mplate;

}

set

{

// Ensure that the cell used for the template is a

CalendarCell.

if (value != null &&

!e().IsAssignableFrom(typeof(CalendarCell)))

{

throw new InvalidCastException("Must be a

CalendarCell");

}

mplate = value;

}

}

}

public class CalendarCell : DataGridViewTextBoxCell

{

public CalendarCell()

: base()

{

// Use the short date format.

= "d";

}

public override void InitializeEditingControl(int rowIndex, object

initialFormattedValue, DataGridViewCellStyle

dataGridViewCellStyle)

{

// Set the value of the editing control to the current cell value.

lizeEditingControl(rowIndex,

initialFormattedValue,

dataGridViewCellStyle);

CalendarEditingControl ctl =

gControl as CalendarEditingControl;

= (DateTime);

}

public override Type EditType

{

get

{

// Return the type of the editing contol that CalendarCell

uses.

return typeof(CalendarEditingControl);

}

}

public override Type ValueType

{

get

{

// Return the type of the value that CalendarCell contains.

return typeof(DateTime);

}

}

public override object DefaultNewRowValue

{

get

{

// Use the current date and time as the default value.

return ;

}

}

}

class CalendarEditingControl : DateTimePicker,

IDataGridViewEditingControl

{

DataGridView dataGridView;

private bool valueChanged = false;

int rowIndex;

public CalendarEditingControl()

{

= ;

}

// Implements the

gControlFormattedValue

// property.

public object EditingControlFormattedValue

{

get

{

return tDateString();

}

set

{

String newValue = value as String;

if (newValue != null)

{

= (newValue);

}

}

}

// Implements the

// tingControlFormattedValue

method.

public object GetEditingControlFormattedValue(

DataGridViewDataErrorContexts context)

{

return EditingControlFormattedValue;

}

// Implements the

// ellStyleToEditingControl

method.

public void ApplyCellStyleToEditingControl(

DataGridViewCellStyle dataGridViewCellStyle)

{

= ;

arForeColor = lor;

arMonthBackground =

lor;

}

// Implements the

gControlRowIndex

// property.

public int EditingControlRowIndex

{

get

{

return rowIndex;

}

set

{

rowIndex = value;

}

}

// Implements the

gControlWantsInputKey

// method.

public bool EditingControlWantsInputKey(

Keys key, bool dataGridViewWantsInputKey)

{

// Let the DateTimePicker handle the keys listed.

switch (key & e)

{

case :

case :

case :

case :

case :

case :

case wn:

case :

return true;

default:

return false;

}

}

// Implements the

eEditingControlForEdit

// method.

public void PrepareEditingControlForEdit(bool selectAll)

{

// No preparation needs to be done.

}

// Implements the IDataGridViewEditingControl

// .RepositionEditingControlOnValueChange property.

public bool RepositionEditingControlOnValueChange

{

get

{

return false;

}

}

// Implements the IDataGridViewEditingControl

// .EditingControlDataGridView property.

public DataGridView EditingControlDataGridView

{

get

{

return dataGridView;

}

set

{

dataGridView = value;

}

}

// Implements the IDataGridViewEditingControl

// .EditingControlValueChanged property.

public bool EditingControlValueChanged

{

get

{

return valueChanged;

}

set

{

valueChanged = value;

}

}

// Implements the IDataGridViewEditingControl

// .EditingPanelCursor property.

public Cursor EditingPanelCursor

{

get

{

return ;

}

}

protected override void OnValueChanged(EventArgs eventargs)

{

// Notify the DataGridView that the contents of the cell

// have changed.

valueChanged = true;

CurrentCellDirty(true);

eChanged(eventargs);

}

}

public class Form1 : Form

{

private DataGridView dataGridView1 = new DataGridView();

[STAThreadAttribute()]

public static void Main()

{

(new Form1());

}

public Form1()

{

= ;

(idView1);

+= new EventHandler(Form1_Load);

= "DataGridView calendar column demo";

}

private void Form1_Load(object sender, EventArgs e)

{

CalendarColumn col = new CalendarColumn();

(col);

nt = 5;

foreach (DataGridViewRow row in )

{

[0].Value = ;

}

}

}

二、VB代码

Imports System

Imports

Public Class CalendarColumn

Inherits DataGridViewColumn

Public Sub New()

(New CalendarCell())

End Sub

Public Overrides Property CellTemplate() As DataGridViewCell

Get

Return mplate

End Get

Set(ByVal value As DataGridViewCell)

' Ensure that the cell used for the template is a

CalendarCell.

If Not (value Is Nothing) AndAlso _

Not

e().IsAssignableFrom(GetType(CalendarCell)) _

Then

Throw New InvalidCastException("Must be a

CalendarCell")

End If

mplate = value

End Set

End Property

End Class

Public Class CalendarCell

Inherits DataGridViewTextBoxCell

Public Sub New()

' Use the short date format.

= "d"

End Sub

Public Overrides Sub InitializeEditingControl(ByVal rowIndex As

Integer, _

ByVal initialFormattedValue As Object, _

ByVal dataGridViewCellStyle As DataGridViewCellStyle)

' Set the value of the editing control to the current cell value.

lizeEditingControl(rowIndex,

initialFormattedValue, _

dataGridViewCellStyle)

Dim ctl As CalendarEditingControl = _

CType(gControl,

CalendarEditingControl)

= CType(, DateTime)

End Sub

Public Overrides ReadOnly Property EditType() As Type

Get

' Return the type of the editing contol that CalendarCell

uses.

Return GetType(CalendarEditingControl)

End Get

End Property

Public Overrides ReadOnly Property ValueType() As Type

Get

' Return the type of the value that CalendarCell contains.

Return GetType(DateTime)

End Get

End Property

Public Overrides ReadOnly Property DefaultNewRowValue() As Object

Get

' Use the current date and time as the default value.

Return

End Get

End Property

End Class

Class CalendarEditingControl

Inherits DateTimePicker

Implements IDataGridViewEditingControl

Private dataGridViewControl As DataGridView

Private valueIsChanged As Boolean = False

Private rowIndexNum As Integer

Public Sub New()

=

End Sub

Public Property EditingControlFormattedValue() As Object _

Implements

gControlFormattedValue

Get

Return tDateString()

End Get

Set(ByVal value As Object)

If TypeOf value Is [String] Then

= (CStr(value))

End If

End Set

End Property

Public Function GetEditingControlFormattedValue(ByVal context _

As DataGridViewDataErrorContexts) As Object _

Implements

tingControlFormattedValue

Return tDateString()

End Function

Public Sub ApplyCellStyleToEditingControl(ByVal

dataGridViewCellStyle As _

DataGridViewCellStyle) _

Implements

ellStyleToEditingControl

=

arForeColor = lor

arMonthBackground = lor

End Sub

Public Property EditingControlRowIndex() As Integer _

Implements gControlRowIndex

Get

Return rowIndexNum

End Get

Set(ByVal value As Integer)

rowIndexNum = value

End Set

End Property

Public Function EditingControlWantsInputKey(ByVal key As Keys, _

ByVal dataGridViewWantsInputKey As Boolean) As Boolean _

Implements

gControlWantsInputKey

' Let the DateTimePicker handle the keys listed.

Select Case key And e

Case , , , , _

, , wn,

Return True

Case Else

Return False

End Select

End Function

Public Sub PrepareEditingControlForEdit(ByVal selectAll As Boolean)

_

Implements

eEditingControlForEdit

' No preparation needs to be done.

End Sub

Public ReadOnly Property RepositionEditingControlOnValueChange() _

As Boolean Implements _

tionEditingControlOnValueChange

Get

Return False

End Get

End Property

Public Property EditingControlDataGridView() As DataGridView _

Implements

gControlDataGridView

Get

Return dataGridViewControl

End Get

Set(ByVal value As DataGridView)

dataGridViewControl = value

End Set

End Property

Public Property EditingControlValueChanged() As Boolean _

Implements

gControlValueChanged

Get

Return valueIsChanged

End Get

Set(ByVal value As Boolean)

valueIsChanged = value

End Set

End Property

Public ReadOnly Property EditingControlCursor() As Cursor _

Implements gPanelCursor

Get

Return

End Get

End Property

Protected Overrides Sub OnValueChanged(ByVal eventargs As EventArgs)

' Notify the DataGridView that the contents of the cell have

changed.

valueIsChanged = True

CurrentCellDirty(True)

eChanged(eventargs)

End Sub

End Class

Public Class Form1

Inherits Form

Private dataGridView1 As New DataGridView()

_

Public Shared Sub Main()

(New Form1())

End Sub

Public Sub New()

=

(idView1)

= "DataGridView calendar column demo"

End Sub

Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)

_

Handles

Dim col As New CalendarColumn()

(col)

nt = 5

Dim row As DataGridViewRow

For Each row In

(0).Value =

Next row

End Sub

End Class


本文标签: 控件 需要 开发 系统 代码