Introduction
Why we need a helper class
- A helper class will make our code part Simple and Standard.
- All the events of a DataGridView can be defined in the Helper Class. In our form we can call the method from the helper class and make our code simple.
- A one-time design in the class can be used for the entire project.
- This will be useful if we change the design only in the class file and there is no need to redesign in each form.
What we have in a helper class
- Design your DataGridView, add the DataGridView to your controls like Panel, Tab or in your form
- Bound Column
- CheckBox Column
- TextBox Column
- Numeric TextBox Column
- ComboBox Column
- DateTimePicker Column
- Button Column
- Colour Dialog Column
- Image Column
- DataGridView Events like CellClick,CellContentClick and so on
Here is a sample with Color Dialog from the button Column.
Using the code
First we can start with the helper class and then we can see how to use the helper class in Windows Forms. In my helper class I have the fallowing function to make the design and bind simple.
- Layout
- Generategrid
- Templatecolumn
- NumeriTextboxEvents
- DateTimePickerEvents
- DGVClickEvents
- colorDialogEvents
- datagridImageAddEvent
We can see a few important functions of the class and then I will paste my full class code here.
Layout: This method will set the BackgroundColor, BackColor, AllowUserToAddRows and so on for the DataGridView. In this method we pass our DataGridView and setting all the design part for grid.
- #region Layout
-
- public static void Layouts(DataGridView ShanuDGV, Color BackgroundColor, Color RowsBackColor, Color AlternatebackColor, Boolean AutoGenerateColumns, Color HeaderColor, Boolean HeaderVisual, Boolean RowHeadersVisible, Boolean AllowUserToAddRows, Color HeaderForeColor, int headerHeight) {
-
- ShanuDGV.BackgroundColor = BackgroundColor;
-
-
- ShanuDGV.RowsDefaultCellStyle.BackColor = RowsBackColor;
-
-
- ShanuDGV.AlternatingRowsDefaultCellStyle.BackColor = AlternatebackColor;
-
-
- ShanuDGV.AutoGenerateColumns = AutoGenerateColumns;
-
-
-
-
- ShanuDGV.ColumnHeadersDefaultCellStyle.BackColor = HeaderColor;
- ShanuDGV.ColumnHeadersDefaultCellStyle.ForeColor = HeaderForeColor;
- ShanuDGV.ColumnHeadersHeight = headerHeight;
-
- ShanuDGV.EnableHeadersVisualStyles = HeaderVisual;
-
-
- ShanuDGV.RowHeadersVisible = RowHeadersVisible;
-
-
- ShanuDGV.AllowUserToAddRows = AllowUserToAddRows;
- }
- #endregion
Generategrid: In this method we pass our DataGridView and set the height, width and position and bind the DataGridView to our selected control.
- #region Generategrid
- 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);
-
- cntrlName.Controls.Add(ShanuDGV);
- }
- #endregion
TemplateColumn: This is the important method in the helperclass. Here we pass the DataGridView and define the columns as Bound, Checkbox, TextBox, DateTimePicker and so on. Here we set each column width, Alignment, visibility, BackColor, Font Color and so on.
- 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.HeaderText = Headertext;
- dgvcombo.ToolTipText = ToolTipText;
- 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.HeaderText = Headertext;
- dgvButtons.ToolTipText = ToolTipText;
- 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;
- case ShanuControlTypes.ImageColumn:
- DataGridViewImageColumn dgvnestedBtn = new DataGridViewImageColumn();
- dgvnestedBtn.Name = cntrlnames;
- dgvnestedBtn.Image = Image.FromFile("shanu.JPG");
- dgvnestedBtn.HeaderText = Headertext;
- dgvnestedBtn.ToolTipText = ToolTipText;
-
- dgvnestedBtn.Visible = Visible;
- dgvnestedBtn.Width = width;
- dgvnestedBtn.SortMode = DataGridViewColumnSortMode.Automatic;
- dgvnestedBtn.Resizable = Resizable;
- dgvnestedBtn.DefaultCellStyle.Alignment = cellAlignment;
- dgvnestedBtn.HeaderCell.Style.Alignment = headerAlignment;
- ShanuDGV.Columns.Add(dgvnestedBtn);
- break;
- }
- }
NumerictextBoxEvent: In this method we pass the DataGridView and list of ColumnIndexes that need to be set as a NumbericTextbox Column. Using the EditingControlShowing Event of DataGridView, I check for all the columns that need to be accepted as only numbers from the TextBox.
-
- #region Numeric Textbox Events
- public void NumeriTextboxEvents(DataGridView ShanuDGV, List < int > columnIndexs) {
-
- shanuDGVs = ShanuDGV;
- listcolumnIndex = columnIndexs;
-
- ShanuDGV.EditingControlShowing += new DataGridViewEditingControlShowingEventHandler(dShanuDGV_EditingControlShowing);
- }
-
- private void dShanuDGV_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) {
- e.Control.KeyPress -= new KeyPressEventHandler(itemID_KeyPress);
- if (listcolumnIndex.Contains(shanuDGVs.CurrentCell.ColumnIndex)) {
- TextBox itemID = e.Control as TextBox;
- if (itemID != null) {
- itemID.KeyPress += new KeyPressEventHandler(itemID_KeyPress);
- }
- }
- }
-
- private void itemID_KeyPress(object sender, KeyPressEventArgs e) {
- if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar)) {
- e.Handled = true;
- }
- }
- #endregion
DataGridView helperClass Full Source Code : Here is the full source code of the DataGridView helper class. I have created all the necessary methods that need to be used. If the user needs more functions then the user can add those functions as well here in this class and use in your projects.
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using System.Data;
-
-
-
-
-
-
-
-
-
- namespace ShanuDGVHelper_Demo
- {
- 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;
- int imgColumnIndex = 0;
- DataTable objDGVBind = new DataTable();
- Boolean imgBindchk = false;
- #endregion
-
- #region Layout
- public static void Layouts(DataGridView ShanuDGV, Color BackgroundColor, Color RowsBackColor, Color AlternatebackColor, Boolean AutoGenerateColumns, Color HeaderColor, Boolean HeaderVisual, Boolean RowHeadersVisible, Boolean AllowUserToAddRows, Color HeaderForeColor, int headerHeight) {
-
- ShanuDGV.BackgroundColor = BackgroundColor;
-
- ShanuDGV.RowsDefaultCellStyle.BackColor = RowsBackColor;
-
- ShanuDGV.AlternatingRowsDefaultCellStyle.BackColor = AlternatebackColor;
-
- ShanuDGV.AutoGenerateColumns = AutoGenerateColumns;
-
-
-
- ShanuDGV.ColumnHeadersDefaultCellStyle.BackColor = HeaderColor;
- ShanuDGV.ColumnHeadersDefaultCellStyle.ForeColor = HeaderForeColor;
- ShanuDGV.ColumnHeadersHeight = headerHeight;
-
- ShanuDGV.EnableHeadersVisualStyles = HeaderVisual;
-
- ShanuDGV.RowHeadersVisible = RowHeadersVisible;
-
- ShanuDGV.AllowUserToAddRows = AllowUserToAddRows;
- }
- #endregion
-
- #region Generategrid
- 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);
-
- cntrlName.Controls.Add(ShanuDGV);
- }
- #endregion
-
- #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.HeaderText = Headertext;
- dgvcombo.ToolTipText = ToolTipText;
- 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.HeaderText = Headertext;
- dgvButtons.ToolTipText = ToolTipText;
- 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;
- case ShanuControlTypes.ImageColumn:
- DataGridViewImageColumn dgvnestedBtn = new DataGridViewImageColumn();
- dgvnestedBtn.Name = cntrlnames;
- dgvnestedBtn.Image = Image.FromFile("shanu.JPG");
- dgvnestedBtn.HeaderText = Headertext;
- dgvnestedBtn.ToolTipText = ToolTipText;
-
- dgvnestedBtn.Visible = Visible;
- dgvnestedBtn.Width = width;
- dgvnestedBtn.SortMode = DataGridViewColumnSortMode.Automatic;
- dgvnestedBtn.Resizable = Resizable;
- dgvnestedBtn.DefaultCellStyle.Alignment = cellAlignment;
- dgvnestedBtn.HeaderCell.Style.Alignment = headerAlignment;
- ShanuDGV.Columns.Add(dgvnestedBtn);
- break;
- }
- }
- #endregion
- #region Image Column bind using Cellformatting
- public void datagridImageAddEvent(DataGridView ShanuDGV, int imgColIndex, DataTable objDGVBinds) {
- shanuDGVs = ShanuDGV;
- objDGVBind = objDGVBinds;
- imgColumnIndex = imgColIndex;
- shanuDGVs.CellFormatting += new DataGridViewCellFormattingEventHandler(shanuDGVs_CellFormatting);
- }
- private void shanuDGVs_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) {
- if (e.ColumnIndex == imgColumnIndex) {
- if (imgBindchk == false) {
- shanuDGVs.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = Image.FromFile(objDGVBind.Rows[e.RowIndex][imgColumnIndex].ToString());
- if (e.RowIndex == objDGVBind.Rows.Count - 1) {
- imgBindchk = true;
- }
- }
- }
- }
- #endregion
-
- #region Numeric Textbox Events
- public void NumeriTextboxEvents(DataGridView ShanuDGV, List < int > columnIndexs) {
- shanuDGVs = ShanuDGV;
- listcolumnIndex = columnIndexs;
- ShanuDGV.EditingControlShowing += new DataGridViewEditingControlShowingEventHandler(dShanuDGV_EditingControlShowing);
- }
-
- private void dShanuDGV_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) {
- e.Control.KeyPress -= new KeyPressEventHandler(itemID_KeyPress);
- if (listcolumnIndex.Contains(shanuDGVs.CurrentCell.ColumnIndex)) {
- TextBox itemID = e.Control as TextBox;
- if (itemID != null) {
- itemID.KeyPress += new KeyPressEventHandler(itemID_KeyPress);
- }
- }
- }
-
- private void itemID_KeyPress(object sender, KeyPressEventArgs e) {
- if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar)) {
- e.Handled = true;
- }
- }#endregion
-
- #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);
-
-
-
-
-
-
-
-
-
- }
-
- 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);
-
- }
- }
- #endregion
-
- #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
-
- #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
- }
- }
-
- public enum ShanuControlTypes {
- BoundColumn, TextBox, ComboBox, CheckBox, DateTimepicker, Button, NumericTextBox, ColorDialog, ImageColumn
- }
- public enum ShanuEventTypes {
- CellClick, cellContentClick, EditingControlShowing
- }
Now let’s see how to use this Helper Class in our Windows Forms project.
- Add the helperClass file to your project.
- In your form Load call a Method to create your DataGridView dynamically and call the functions to design, bind and set each column of your grid.
Here we can see I have created a method called "generatedgvColumns” and call this method in my Form Load Event.In this method i will set the Hegith,Width and styles to the datagridview,create all columns with Column type as TextboxColumn,ButtonColumn,CheckBoxColumn,ImageColumn,DateTimePickerColumn and etc,Set events for grid like CellContentClick event and etc, bind the Dataseource to the grid.
- public void generatedgvColumns()
- {
-
-
- ShanuDGVHelper.Layouts(shanuDGV, Color.LightSteelBlue, Color.AliceBlue, Color.LightSkyBlue, false, Color.SteelBlue, false, false, false, Color.White, 40);
-
-
- ShanuDGVHelper.Generategrid(shanuDGV, pnlShanuGrid, 1000, 600, 10, 10);
-
-
- ShanuDGVHelper.Templatecolumn(shanuDGV, ShanuControlTypes.CheckBox, "Chk", "CHKCOL", "Check Box Column", true, 60, DataGridViewTriState.True, DataGridViewContentAlignment.MiddleCenter, DataGridViewContentAlignment.MiddleCenter, Color.Transparent, null, "", "", Color.Black);
-
-
- ShanuDGVHelper.Templatecolumn(shanuDGV, ShanuControlTypes.BoundColumn, "DGVBoundColumn", "BOUNDCOL", "Bound Column", true, 120, DataGridViewTriState.True, DataGridViewContentAlignment.MiddleCenter, DataGridViewContentAlignment.MiddleCenter, Color.Transparent, null, "", "", Color.Black);
-
-
- ShanuDGVHelper.Templatecolumn(shanuDGV, ShanuControlTypes.TextBox, "DGVTXTColumn", "TXTCOL", "textBox Column", true, 130, DataGridViewTriState.True, DataGridViewContentAlignment.MiddleLeft, DataGridViewContentAlignment.MiddleLeft, Color.White, null, "", "", Color.Black);
-
-
- ShanuDGVHelper.Templatecolumn(shanuDGV, ShanuControlTypes.TextBox, "DGVNumericTXTColumn", "NO.COL", "textBox Column", true, 60, DataGridViewTriState.True, DataGridViewContentAlignment.MiddleRight, DataGridViewContentAlignment.MiddleCenter, Color.MistyRose, null, "", "", Color.Black);
-
-
- ShanuDGVHelper.Templatecolumn(shanuDGV, ShanuControlTypes.BoundColumn, "DGVDateTimepicker", "DATECOL", "For Datetime Column", true, 160, DataGridViewTriState.True, DataGridViewContentAlignment.MiddleLeft, DataGridViewContentAlignment.MiddleLeft, Color.Transparent, null, "", "", Color.Black);
-
-
- ShanuDGVHelper.Templatecolumn(shanuDGV, ShanuControlTypes.ComboBox, "DGVComboColumn", "COMBOCOL", "Combo Column", true, 160, DataGridViewTriState.True, DataGridViewContentAlignment.MiddleCenter, DataGridViewContentAlignment.MiddleRight, Color.Transparent, dtName, "Name", "Value", Color.Black);
-
-
-
- ShanuDGVHelper.Templatecolumn(shanuDGV, ShanuControlTypes.Button, "DGVButtonColumn", "BUTTONCOL", "Button Column", true, 120, DataGridViewTriState.True, DataGridViewContentAlignment.MiddleCenter, DataGridViewContentAlignment.MiddleRight, Color.Transparent, null, "", "", Color.Black);
-
-
- ShanuDGVHelper.Templatecolumn(shanuDGV, ShanuControlTypes.Button, "DGVColorDialogColumn", "COLORPICKER", "Button Column", true, 120, DataGridViewTriState.True, DataGridViewContentAlignment.MiddleCenter, DataGridViewContentAlignment.MiddleRight, Color.Transparent, null, "", "", Color.Black);
-
-
- ShanuDGVHelper.Templatecolumn(shanuDGV, ShanuControlTypes.ImageColumn, "DGVImgColumn", "IMGCOL", "IMG Column", true, 80, DataGridViewTriState.True, DataGridViewContentAlignment.MiddleCenter, DataGridViewContentAlignment.MiddleRight, Color.Transparent, null, "", "", Color.Black);
-
-
-
- lstNumericTextBoxColumns = new List < int > ();
- lstNumericTextBoxColumns.Add(shanuDGV.Columns["DGVNumericTXTColumn"].Index);
-
-
- objshanudgvHelper.NumeriTextboxEvents(shanuDGV, lstNumericTextBoxColumns);
-
-
- objshanudgvHelper.datagridImageAddEvent(shanuDGV, shanuDGV.Columns["DGVImgColumn"].Index, dtSource);
-
-
-
- objshanudgvHelper.DateTimePickerEvents(shanuDGV, shanuDGV.Columns["DGVDateTimepicker"].Index, ShanuEventTypes.CellClick);
-
-
- objshanudgvHelper.colorDialogEvents(shanuDGV, shanuDGV.Columns["DGVColorDialogColumn"].Index, ShanuEventTypes.cellContentClick);
-
-
- objshanudgvHelper.DGVClickEvents(shanuDGV, shanuDGV.Columns["DGVButtonColumn"].Index, ShanuEventTypes.cellContentClick);
-
-
-
- shanuDGV.DataSource = dtSource;
- }
- #endregion
"ShanuDGVHelper.Layouts()" this method sets the layout of the grid, like autogenrated or not, BackgroundColor, AlternatebackColor, RowHeadersVisible and so on.
"ShanuDGVHelper.Generategrid()" this method sets the Height, Width and adds the DataGridView to our selected control, for example here I have added the DGV to a panel control.
" ShanuDGVHelper.Templatecolumn" this method defines our Column type as Checkbox,TextBox, Combobox and so on. For this method we pass the Column Name, Column Header Text, Column Width, Column Back Color and so on.