DataGridView 助手类






4.83/5 (27投票s)
Winforms 的 DataGridView 助手类
引言
在本文中,我将解释如何在 Winform 应用程序中为 DataGridView
创建一个帮助类。在我的一个 Web 项目中,我想在运行时动态地创建 datagridview
。当我启动我的项目时,我正在寻找 DataGridView
的帮助类,并使用 Google 找到了一个帮助类。但是我找不到任何 DataGridView
的帮助类。所以我计划为 DataGridView
创建一个帮助类。结果,我为 DataGridView
创建了一个帮助类。我想与您分享我的帮助类,以便您可以在您的项目中使用我的类,并使代码简单易懂。
为什么我们需要帮助类
- 帮助类将使我们的代码部分更加简单和标准。
DataGridview
的所有事件都可以在helperClass
中定义。在我们的窗体中,我们可以从帮助类调用方法,使我们的代码更简单。- 在类中进行一次设计可以用于整个项目。
- 如果我们只在类文件中更改设计,而无需在每个窗体中重新设计,这将非常有用。
我们在帮助类中有什么
- 设计您的
Datagridview
,将Datagridview
添加到您的控件中,例如Panel
、Tab
或您的窗体 - 绑定列
CheckBox
列TextBox
列- 数字
TextBox
列 ComboBox
列DateTimePicker
列Button
列- 颜色对话框列
DataGridView
事件,例如CellClick
、CellContentClick
等。
这是一个带有按钮列的颜色对话框的示例。
Using the Code
首先,我们可以从帮助类开始,然后我们可以看到如何在 Winform 中使用该帮助类。在我的帮助类中,我有以下函数来简化设计和绑定。
布局
Generategrid
Templatecolumn
NumeriTextboxEvents
DateTimePickerEvents
DGVClickEvents
colorDialogEvents
我们可以看到该类的一些重要函数,然后我将在此处粘贴我的完整类代码。
布局
此方法将为 datagridView
设置 BackgroundColor
、BackColor
、AllowUserToAddRows
等。在此方法中,我们传递我们的 Datagridview
并设置网格的所有设计部分。
//Set all the DGV layout,
#region Layout
public static void Layouts(DataGridView ShanuDGV, Color BackgroundColor,
Color RowsBackColor, Color AlternatebackColor, Boolean AutoGenerateColumns,
Color HeaderColor, Boolean HeaderVisual, Boolean RowHeadersVisible,
Boolean AllowUserToAddRows)
{
// Grid Background Color
ShanuDGV.BackgroundColor = BackgroundColor;
// Grid BackColor
ShanuDGV.RowsDefaultCellStyle.BackColor = RowsBackColor;
// GridColumnStylesCollection Alternate Rows Backcolr
ShanuDGV.AlternatingRowsDefaultCellStyle.BackColor = AlternatebackColor;
// Auto generated here set to true or false.
ShanuDGV.AutoGenerateColumns = AutoGenerateColumns;
// ShanuDGV.DefaultCellStyle.Font =
// new Font("Verdana", 10.25f, FontStyle.Regular);
// ShanuDGV.ColumnHeadersDefaultCellStyle.Font =
// new Font("Calibri", 11, FontStyle.Regular);
// Column Header back Color
ShanuDGV.ColumnHeadersDefaultCellStyle.BackColor = HeaderColor;
// header Visisble
ShanuDGV.EnableHeadersVisualStyles = HeaderVisual;
// Enable the row header
ShanuDGV.RowHeadersVisible = RowHeadersVisible;
// to Hide the Last Empty row here, we use false.
ShanuDGV.AllowUserToAddRows = AllowUserToAddRows;
}
#endregion
Generategrid
在此方法中,我们传递我们的 Datagridview
并设置高度、宽度、位置,并将 datagriview
绑定到我们选择的控件。
public static void Generategrid
(DataGridView ShanuDGV, Control cntrlName, int width, int height, int xval, int yval)
{
ShanuDGV.Location = new Point(xval, yval);
ShanuDGV.Size = new Size(width, height);
//ShanuDGV.Dock = docktyope.
cntrlName.Controls.Add(ShanuDGV);
}
TemplateColumn
这是 helperclass
中的重要方法。在这里,我们传递 Datagriview
并将列定义为 Bound
、Checkbox
、Textbox DateTimePicker
等。在这里,我们设置每个列的宽度、Alignment
、Visibility
、BackColor
、FontColor
等。
public static void Templatecolumn(DataGridView ShanuDGV, ShanuControlTypes ShanuControlTypes,
String cntrlnames, String Headertext, String ToolTipText, Boolean Visible, int width,
DataGridViewTriState Resizable, DataGridViewContentAlignment cellAlignment,
DataGridViewContentAlignment headerAlignment, Color CellTemplateBackColor,
DataTable dtsource, String DisplayMember, String ValueMember, Color CellTemplateforeColor)
{
switch (ShanuControlTypes)
{
case ShanuControlTypes.CheckBox:
DataGridViewCheckBoxColumn dgvChk = new DataGridViewCheckBoxColumn();
dgvChk.ValueType = typeof(bool);
dgvChk.Name = cntrlnames;
dgvChk.HeaderText = Headertext;
dgvChk.ToolTipText = ToolTipText;
dgvChk.Visible = Visible;
dgvChk.Width = width;
dgvChk.SortMode = DataGridViewColumnSortMode.Automatic;
dgvChk.Resizable = Resizable;
dgvChk.DefaultCellStyle.Alignment = cellAlignment;
dgvChk.HeaderCell.Style.Alignment = headerAlignment;
if (CellTemplateBackColor.Name.ToString() != "Transparent")
{
dgvChk.CellTemplate.Style.BackColor = CellTemplateBackColor;
}
dgvChk.DefaultCellStyle.ForeColor = CellTemplateforeColor;
ShanuDGV.Columns.Add(dgvChk);
break;
case ShanuControlTypes.BoundColumn:
DataGridViewColumn col = new DataGridViewTextBoxColumn();
col.DataPropertyName = cntrlnames;
col.Name = cntrlnames;
col.HeaderText = Headertext;
col.ToolTipText = ToolTipText;
col.Visible = Visible;
col.Width = width;
col.SortMode = DataGridViewColumnSortMode.Automatic;
col.Resizable = Resizable;
col.DefaultCellStyle.Alignment = cellAlignment;
col.HeaderCell.Style.Alignment = headerAlignment;
if (CellTemplateBackColor.Name.ToString() != "Transparent")
{
col.CellTemplate.Style.BackColor = CellTemplateBackColor;
}
col.DefaultCellStyle.ForeColor = CellTemplateforeColor;
ShanuDGV.Columns.Add(col);
break;
case ShanuControlTypes.TextBox:
DataGridViewTextBoxColumn dgvText = new DataGridViewTextBoxColumn();
dgvText.ValueType = typeof(decimal);
dgvText.DataPropertyName = cntrlnames;
dgvText.Name = cntrlnames;
dgvText.HeaderText = Headertext;
dgvText.ToolTipText = ToolTipText;
dgvText.Visible = Visible;
dgvText.Width = width;
dgvText.SortMode = DataGridViewColumnSortMode.Automatic;
dgvText.Resizable = Resizable;
dgvText.DefaultCellStyle.Alignment = cellAlignment;
dgvText.HeaderCell.Style.Alignment = headerAlignment;
if (CellTemplateBackColor.Name.ToString() != "Transparent")
{
dgvText.CellTemplate.Style.BackColor = CellTemplateBackColor;
}
dgvText.DefaultCellStyle.ForeColor = CellTemplateforeColor;
ShanuDGV.Columns.Add(dgvText);
break;
case ShanuControlTypes.ComboBox:
DataGridViewComboBoxColumn dgvcombo = new DataGridViewComboBoxColumn();
dgvcombo.ValueType = typeof(decimal);
dgvcombo.Name = cntrlnames;
dgvcombo.DataSource = dtsource;
dgvcombo.DisplayMember = DisplayMember;
dgvcombo.ValueMember = ValueMember;
dgvcombo.Visible = Visible;
dgvcombo.Width = width;
dgvcombo.SortMode = DataGridViewColumnSortMode.Automatic;
dgvcombo.Resizable = Resizable;
dgvcombo.DefaultCellStyle.Alignment = cellAlignment;
dgvcombo.HeaderCell.Style.Alignment = headerAlignment;
if (CellTemplateBackColor.Name.ToString() != "Transparent")
{
dgvcombo.CellTemplate.Style.BackColor = CellTemplateBackColor;
}
dgvcombo.DefaultCellStyle.ForeColor = CellTemplateforeColor;
ShanuDGV.Columns.Add(dgvcombo);
break;
case ShanuControlTypes.Button:
DataGridViewButtonColumn dgvButtons = new DataGridViewButtonColumn();
dgvButtons.Name = cntrlnames;
dgvButtons.FlatStyle = FlatStyle.Popup;
dgvButtons.DataPropertyName = cntrlnames;
dgvButtons.Visible = Visible;
dgvButtons.Width = width;
dgvButtons.SortMode = DataGridViewColumnSortMode.Automatic;
dgvButtons.Resizable = Resizable;
dgvButtons.DefaultCellStyle.Alignment = cellAlignment;
dgvButtons.HeaderCell.Style.Alignment = headerAlignment;
if (CellTemplateBackColor.Name.ToString() != "Transparent")
{
dgvButtons.CellTemplate.Style.BackColor = CellTemplateBackColor;
}
dgvButtons.DefaultCellStyle.ForeColor = CellTemplateforeColor;
ShanuDGV.Columns.Add(dgvButtons);
break;
}
}
NumerictextBoxEvent
在此方法中,我们传递 Datagridview
和需要设置为 NumericTextbox
Column
的 ColumnIndex
列表。使用 Gridview
的 EditingControlShowing
事件,我检查所有需要仅接受来自 textbox
的数字的列。
public void NumeriTextboxEvents(DataGridView ShanuDGV,List<int> columnIndexs)
{
shanuDGVs = ShanuDGV;
listcolumnIndex=columnIndexs;
ShanuDGV.EditingControlShowing +=
new DataGridViewEditingControlShowingEventHandler
(dShanuDGV_EditingControlShowing);
}
// grid Editing Control Showing
private void dShanuDGV_EditingControlShowing
(object sender, DataGridViewEditingControlShowingEventArgs e)
{
e.Control.KeyPress -=
new KeyPressEventHandler(itemID_KeyPress); //This line of code resolved my issue
if (listcolumnIndex.Contains(shanuDGVs.CurrentCell.ColumnIndex))
{
TextBox itemID = e.Control as TextBox;
if (itemID != null)
{
itemID.KeyPress += new KeyPressEventHandler(itemID_KeyPress);
}
}
}
//Grid Key press event
private void itemID_KeyPress(object sender, KeyPressEventArgs e)
{
if (!char.IsControl(e.KeyChar)
&& !char.IsDigit(e.KeyChar))
{
e.Handled = true;
}
}
DatagridView helperClass 完整源代码
这是 Datagridview
帮助类的完整源代码。我已经创建了所有需要使用的必要方法。如果用户需要更多功能,他们也可以在此类中添加这些功能并在您的项目中使用。
class ShanuDGVHelper
{
#region Variables
public DataGridView shanuDGVs = new DataGridView();
List<int> listcolumnIndex;
int DateColumnIndex=0;
int ColorColumnIndex = 0;
int ClickColumnIndex = 0;
DateTimePicker shanuDateTimePicker;
String EventFucntions;
# endregion
//Set all the Grid layout
#region Layout
public static void Layouts(DataGridView ShanuDGV, Color BackgroundColor,
Color RowsBackColor, Color AlternatebackColor, Boolean AutoGenerateColumns,
Color HeaderColor, Boolean HeaderVisual, Boolean RowHeadersVisible,
Boolean AllowUserToAddRows)
{
//Grid Background Color
ShanuDGV.BackgroundColor = BackgroundColor;
//Grid Back Color
ShanuDGV.RowsDefaultCellStyle.BackColor = RowsBackColor;
//GridColumnStylesCollection Alternate Rows Backcolor
ShanuDGV.AlternatingRowsDefaultCellStyle.BackColor = AlternatebackColor;
// Auto generated here set to true or false.
ShanuDGV.AutoGenerateColumns = AutoGenerateColumns;
// ShanuDGV.DefaultCellStyle.Font =
// new Font("Verdana", 10.25f, FontStyle.Regular);
// ShanuDGV.ColumnHeadersDefaultCellStyle.Font =
// new Font("Calibri", 11, FontStyle.Regular);
// Column Header back Color
ShanuDGV.ColumnHeadersDefaultCellStyle.BackColor = HeaderColor;
// header Visisble
ShanuDGV.EnableHeadersVisualStyles = HeaderVisual;
// Enable the row header
ShanuDGV.RowHeadersVisible = RowHeadersVisible;
// to hide the Last Empty row here, we use false.
ShanuDGV.AllowUserToAddRows = AllowUserToAddRows;
}
#endregion
// Add your grid to your selected Control
// and set height, width, position of your grid.
#region Variables
public static void Generategrid(DataGridView ShanuDGV,
Control cntrlName, int width, int height, int xval, int yval)
{
ShanuDGV.Location = new Point(xval, yval);
ShanuDGV.Size = new Size(width, height);
// ShanuDGV.Dock = docktyope.
cntrlName.Controls.Add(ShanuDGV);
}
#endregion
// Template Column: In this column, we can add Textbox, Label,
// Check Box, Dropdown box, etc.
#region Templatecolumn
public static void Templatecolumn(DataGridView ShanuDGV,
ShanuControlTypes ShanuControlTypes, String cntrlnames, String Headertext,
String ToolTipText, Boolean Visible, int width, DataGridViewTriState Resizable,
DataGridViewContentAlignment cellAlignment,
DataGridViewContentAlignment headerAlignment,
Color CellTemplateBackColor, DataTable dtsource, String DisplayMember,
String ValueMember, Color CellTemplateforeColor)
{
switch (ShanuControlTypes)
{
case ShanuControlTypes.CheckBox:
DataGridViewCheckBoxColumn dgvChk = new DataGridViewCheckBoxColumn();
dgvChk.ValueType = typeof(bool);
dgvChk.Name = cntrlnames;
dgvChk.HeaderText = Headertext;
dgvChk.ToolTipText = ToolTipText;
dgvChk.Visible = Visible;
dgvChk.Width = width;
dgvChk.SortMode = DataGridViewColumnSortMode.Automatic;
dgvChk.Resizable = Resizable;
dgvChk.DefaultCellStyle.Alignment = cellAlignment;
dgvChk.HeaderCell.Style.Alignment = headerAlignment;
if (CellTemplateBackColor.Name.ToString() != "Transparent")
{
dgvChk.CellTemplate.Style.BackColor = CellTemplateBackColor;
}
dgvChk.DefaultCellStyle.ForeColor = CellTemplateforeColor;
ShanuDGV.Columns.Add(dgvChk);
break;
case ShanuControlTypes.BoundColumn:
DataGridViewColumn col = new DataGridViewTextBoxColumn();
col.DataPropertyName = cntrlnames;
col.Name = cntrlnames;
col.HeaderText = Headertext;
col.ToolTipText = ToolTipText;
col.Visible = Visible;
col.Width = width;
col.SortMode = DataGridViewColumnSortMode.Automatic;
col.Resizable = Resizable;
col.DefaultCellStyle.Alignment = cellAlignment;
col.HeaderCell.Style.Alignment = headerAlignment;
if (CellTemplateBackColor.Name.ToString() != "Transparent")
{
col.CellTemplate.Style.BackColor = CellTemplateBackColor;
}
col.DefaultCellStyle.ForeColor = CellTemplateforeColor;
ShanuDGV.Columns.Add(col);
break;
case ShanuControlTypes.TextBox:
DataGridViewTextBoxColumn dgvText = new DataGridViewTextBoxColumn();
dgvText.ValueType = typeof(decimal);
dgvText.DataPropertyName = cntrlnames;
dgvText.Name = cntrlnames;
dgvText.HeaderText = Headertext;
dgvText.ToolTipText = ToolTipText;
dgvText.Visible = Visible;
dgvText.Width = width;
dgvText.SortMode = DataGridViewColumnSortMode.Automatic;
dgvText.Resizable = Resizable;
dgvText.DefaultCellStyle.Alignment = cellAlignment;
dgvText.HeaderCell.Style.Alignment = headerAlignment;
if (CellTemplateBackColor.Name.ToString() != "Transparent")
{
dgvText.CellTemplate.Style.BackColor = CellTemplateBackColor;
}
dgvText.DefaultCellStyle.ForeColor = CellTemplateforeColor;
ShanuDGV.Columns.Add(dgvText);
break;
case ShanuControlTypes.ComboBox:
DataGridViewComboBoxColumn dgvcombo = new DataGridViewComboBoxColumn();
dgvcombo.ValueType = typeof(decimal);
dgvcombo.Name = cntrlnames;
dgvcombo.DataSource = dtsource;
dgvcombo.DisplayMember = DisplayMember;
dgvcombo.ValueMember = ValueMember;
dgvcombo.Visible = Visible;
dgvcombo.Width = width;
dgvcombo.SortMode = DataGridViewColumnSortMode.Automatic;
dgvcombo.Resizable = Resizable;
dgvcombo.DefaultCellStyle.Alignment = cellAlignment;
dgvcombo.HeaderCell.Style.Alignment = headerAlignment;
if (CellTemplateBackColor.Name.ToString() != "Transparent")
{
dgvcombo.CellTemplate.Style.BackColor = CellTemplateBackColor;
}
dgvcombo.DefaultCellStyle.ForeColor = CellTemplateforeColor;
ShanuDGV.Columns.Add(dgvcombo);
break;
case ShanuControlTypes.Button:
DataGridViewButtonColumn dgvButtons = new DataGridViewButtonColumn();
dgvButtons.Name = cntrlnames;
dgvButtons.FlatStyle = FlatStyle.Popup;
dgvButtons.DataPropertyName = cntrlnames;
dgvButtons.Visible = Visible;
dgvButtons.Width = width;
dgvButtons.SortMode = DataGridViewColumnSortMode.Automatic;
dgvButtons.Resizable = Resizable;
dgvButtons.DefaultCellStyle.Alignment = cellAlignment;
dgvButtons.HeaderCell.Style.Alignment = headerAlignment;
if (CellTemplateBackColor.Name.ToString() != "Transparent")
{
dgvButtons.CellTemplate.Style.BackColor = CellTemplateBackColor;
}
dgvButtons.DefaultCellStyle.ForeColor = CellTemplateforeColor;
ShanuDGV.Columns.Add(dgvButtons);
break;
}
}
#endregion
// Numeric Textbox event and check for key press event
// for accepting only numbers for the selected column
#region Numeric Textbox Events
public void NumeriTextboxEvents(DataGridView ShanuDGV,List<int> columnIndexs)
{
shanuDGVs = ShanuDGV;
listcolumnIndex=columnIndexs;
ShanuDGV.EditingControlShowing +=
new DataGridViewEditingControlShowingEventHandler
(dShanuDGV_EditingControlShowing);
}
// grid Editing Control Showing
private void dShanuDGV_EditingControlShowing
(object sender, DataGridViewEditingControlShowingEventArgs e)
{
e.Control.KeyPress -=
new KeyPressEventHandler(itemID_KeyPress);//This line of code resolved my issue
if (listcolumnIndex.Contains(shanuDGVs.CurrentCell.ColumnIndex))
{
TextBox itemID = e.Control as TextBox;
if (itemID != null)
{
itemID.KeyPress += new KeyPressEventHandler(itemID_KeyPress);
}
}
}
// Grid Key press event
private void itemID_KeyPress(object sender, KeyPressEventArgs e)
{
if (!char.IsControl(e.KeyChar)
&& !char.IsDigit(e.KeyChar))
{
e.Handled = true;
}
}
#endregion
// Add an dateTime Picker control to existing Textbox Column
#region DateTimePicker control to textbox column
public void DateTimePickerEvents
(DataGridView ShanuDGV, int columnIndexs,ShanuEventTypes eventtype)
{
shanuDGVs = ShanuDGV;
DateColumnIndex = columnIndexs;
ShanuDGV.CellClick += new DataGridViewCellEventHandler(shanuDGVs_CellClick);
//switch (eventtype)
//{
// case ShanuEventTypes.CellClick:
// ShanuDGV.CellClick +=
// new DataGridViewCellEventHandler(shanuDGVs_CellClick);
// break;
// case ShanuEventTypes.cellContentClick:
// ShanuDGV.CellContentClick +=
// new DataGridViewCellEventHandler(shanuDGVs_CellContentClick);
// break;
//}
}
// In this cell click event, DateTime Picker Control
// will be added to the selected column
private void shanuDGVs_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == DateColumnIndex)
{
shanuDateTimePicker = new DateTimePicker();
shanuDGVs.Controls.Add(shanuDateTimePicker);
shanuDateTimePicker.Format = DateTimePickerFormat.Short;
Rectangle dgvRectangle =
shanuDGVs.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true);
shanuDateTimePicker.Size = new Size(dgvRectangle.Width, dgvRectangle.Height);
shanuDateTimePicker.Location = new Point(dgvRectangle.X, dgvRectangle.Y);
// shanuDateTimePicker.Visible = true;
}
}
#endregion
// Button Click evnet
#region Button Click Event
public void DGVClickEvents(DataGridView ShanuDGV, int columnIndexs,
ShanuEventTypes eventtype)
{
shanuDGVs = ShanuDGV;
ClickColumnIndex = columnIndexs;
ShanuDGV.CellContentClick += new DataGridViewCellEventHandler
(shanuDGVs_CellContentClick_Event);
}
private void shanuDGVs_CellContentClick_Event
(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == ClickColumnIndex)
{
MessageBox.Show("Button Clicked " +
shanuDGVs.Rows[e.RowIndex].Cells
[e.ColumnIndex].Value.ToString());
}
}
#endregion
// Button Click Event to show Color Dialog
#region Button Click Event to show Color Dialog
public void colorDialogEvents(DataGridView ShanuDGV,
int columnIndexs, ShanuEventTypes eventtype)
{
shanuDGVs = ShanuDGV;
ColorColumnIndex = columnIndexs;
ShanuDGV.CellContentClick += new DataGridViewCellEventHandler
(shanuDGVs_CellContentClick);
}
private void shanuDGVs_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == ColorColumnIndex)
{
MessageBox.Show("Button Clicked " +
shanuDGVs.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString());
ColorDialog cd = new ColorDialog();
cd.ShowDialog();
shanuDGVs.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.BackColor = cd.Color;
}
}
#endregion
}
现在,让我们看看如何在我们的 winform 项目中使用这个帮助类。
- 将 *helperClass* 文件添加到您的项目中。
- 在您的窗体
Load
中,调用一个方法来动态创建您的Datagridview
,并调用函数来设计、绑定和设置网格的每一列。
在这里,我们可以看到我创建了一个名为“generatedgvColumns
”的方法,并在我的窗体 Load
事件中调用了该方法。
public void generatedgvColumns()
{
// First, generate the grid Layout Design
ShanuDGVHelper.Layouts(shanuDGV, Color.LightSteelBlue,
Color.AliceBlue, Color.WhiteSmoke, false, Color.SteelBlue, false, false, false);
// Set Height,width and add panel to your selected control
ShanuDGVHelper.Generategrid(shanuDGV, pnlShanuGrid, 1000, 600, 10, 10);
// CheckboxColumn creation
ShanuDGVHelper.Templatecolumn(shanuDGV, ShanuControlTypes.CheckBox,
"Chk", "ChkCol", "Check Box Column", true,
60, DataGridViewTriState.True, DataGridViewContentAlignment.MiddleCenter,
DataGridViewContentAlignment.MiddleCenter, Color.Transparent, null,
"", "", Color.Black);
// BoundColumn creation
ShanuDGVHelper.Templatecolumn(shanuDGV, ShanuControlTypes.BoundColumn,
"DGVBoundColumn", "DGVBoundColumn", "Bound Column",
true, 120, DataGridViewTriState.True, DataGridViewContentAlignment.MiddleCenter,
DataGridViewContentAlignment.MiddleCenter, Color.Transparent,
null, "", "", Color.Black);
// TextboxColumn creation
ShanuDGVHelper.Templatecolumn(shanuDGV, ShanuControlTypes.TextBox,
"DGVTXTColumn", "DGVTXTColumn", "textBox Column",
true, 130, DataGridViewTriState.True, DataGridViewContentAlignment.MiddleLeft,
DataGridViewContentAlignment.MiddleLeft, Color.White, null, "", "", Color.Black);
// NumerictextColumn creation
ShanuDGVHelper.Templatecolumn(shanuDGV, ShanuControlTypes.TextBox,
"DGVNumericTXTColumn", "NCol", "textBox Column",
true, 60, DataGridViewTriState.True, DataGridViewContentAlignment.MiddleRight,
DataGridViewContentAlignment.MiddleCenter, Color.MistyRose,
null, "", "", Color.Black);
// BoundColumn creation which will be used as Datetime picker
ShanuDGVHelper.Templatecolumn(shanuDGV, ShanuControlTypes.BoundColumn,
"DGVDateTimepicker", "DGVDateTimepicker", "For Datetime Column",
true, 160, DataGridViewTriState.True, DataGridViewContentAlignment.MiddleLeft,
DataGridViewContentAlignment.MiddleLeft, Color.Transparent,
null, "", "", Color.Black);
// ComboBox Column creation
ShanuDGVHelper.Templatecolumn(shanuDGV, ShanuControlTypes.ComboBox,
"DGVComboColumn", "ComboCol", "Combo Column",
true, 160, DataGridViewTriState.True, DataGridViewContentAlignment.MiddleCenter,
DataGridViewContentAlignment.MiddleRight, Color.Transparent, dtName,
"Name", "Value", Color.Black);
// Button Column creation
ShanuDGVHelper.Templatecolumn(shanuDGV, ShanuControlTypes.Button,
"DGVButtonColumn", "ButtonCol", "Button Column",
true, 120, DataGridViewTriState.True, DataGridViewContentAlignment.MiddleCenter,
DataGridViewContentAlignment.MiddleRight, Color.Transparent, null,
"", "", Color.Black);
// Color Dialog Column creation
ShanuDGVHelper.Templatecolumn(shanuDGV, ShanuControlTypes.Button,
"DGVColorDialogColumn", "ButtonCol", "Button Column",
true, 120, DataGridViewTriState.True, DataGridViewContentAlignment.MiddleCenter,
DataGridViewContentAlignment.MiddleRight, Color.Transparent, null,
"", "", Color.Black);
// Numeric Textbox Setting and add the Numeric Textbox column index to list
lstNumericTextBoxColumns = new List<int>();
lstNumericTextBoxColumns.Add(shanuDGV.Columns["DGVNumericTXTColumn"].Index);
// Numeric textbox events to allow only numeric is that column
objshanudgvHelper.NumeriTextboxEvents(shanuDGV, lstNumericTextBoxColumns);
// Datetime Picker Bind to an existing textbox Column
objshanudgvHelper.DateTimePickerEvents
(shanuDGV, shanuDGV.Columns["DGVDateTimepicker"].Index,
ShanuEventTypes.CellClick);
// Add Color Dialog to Button Column
objshanudgvHelper.colorDialogEvents
(shanuDGV, shanuDGV.Columns["DGVColorDialogColumn"].Index,
ShanuEventTypes.cellContentClick);
// DGV button Click Event
objshanudgvHelper.DGVClickEvents
(shanuDGV, shanuDGV.Columns["DGVButtonColumn"].Index,
ShanuEventTypes.cellContentClick);
// Bind data to DGV.
shanuDGV.DataSource = objDGVBind;
}
- “
ShanuDGVHelper.Layouts()
”:此方法用于设置网格的布局,例如是否自动生成、BackgroundColor
、AlternatebackColor
、RowHeadersVisible
等。 - “
ShanuDGVHelper.Generategrid()
”:此方法用于设置Height
、Width
并将datagridview
添加到我们选择的控件,例如,这里我已将 DGV 添加到面板控件。 - “
ShanuDGVHelper.Templatecolumn
”:此方法用于将我们的列类型定义为Checkbox
、Textbox
、Combobox
等。对于此方法,我们传递列名、列标题文本、列宽、列背景颜色等。
关注点
希望这个 Datagridview
帮助类对读者有用。
历史
- 2014 年 11 月 13 日:版本 1.0