Tuesday, September 14, 2010
Grid Line Settings of DataGridView in C# Window Application
//Add a window form and add a datagridview on that form. and then write the code given below
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 Nevron.UI.WinForm.Controls;
namespace MyCustomGrid
{
public partial class Form1 : Form
{
//Create a Context menu for the grid control on which events will fire(execute)
public ContextMenuStrip Mycontextmenu = new ContextMenuStrip();
//Create a property name of the grid control, Now the grid name is "_MyGrid" in place of datagridview1.
private DataGridView _MyGrid;
public DataGridView MyGrid
{
get { return _MyGrid; }
set { _MyGrid = value; }
}
//here the constructor
public Form1()
{
InitializeComponent();
_MyGrid = dataGridView1; // Pass the dataGridView1 to the _MyGrid so that dataGridView1 refer to _MyGrid
}
//Write the Context menu strip items on the Form load Event.
private void Form1_Load(object sender, EventArgs e)
{
ToolStripMenuItem tsHorizontle, tsVertical, tsGrid,tsHideGrid;
tsHorizontle = new ToolStripMenuItem("Horizontle Lines");
tsHorizontle.Click += new EventHandler(tsHorizontle_Click);
tsVertical = new ToolStripMenuItem("Vertical Lines");
tsVertical.Click += new EventHandler(tsVertical_Click);
tsGrid = new ToolStripMenuItem("Full Grid Lines");
tsGrid.Click += new EventHandler(tsGrid_Click);
tsHideGrid = new ToolStripMenuItem("Hide Grid Lines");
tsHideGrid.Click += new EventHandler(tsHideGrid_Click);
Mycontextmenu.Items.Add(tsVertical);
Mycontextmenu.Items.Add(tsHorizontle);
Mycontextmenu.Items.Add(tsGrid);
Mycontextmenu.Items.Add(tsHideGrid);
this.ContextMenuStrip = Mycontextmenu; //here this refrers to the Grid Control : _MyGrid
}
private void tsHideGrid_Click(object sender, EventArgs e) //Hide grid line event
{
this._MyGrid.CellBorderStyle = DataGridViewCellBorderStyle.None;
}
private void tsGrid_Click(object sender, EventArgs e)
{
//Customized the cells on the basis of Customized Class given at last of the code
DataGridViewAdvancedBorderStyle mystyle = new DataGridViewAdvancedBorderStyle();
mystyle.All = DataGridViewAdvancedCellBorderStyle.None;
DataGridViewAdvancedBorderStyle myplaceholder = new DataGridViewAdvancedBorderStyle();
for (int i = 0; i < _MyGrid.RowCount; i++)
{
for (int j = 0; j < _MyGrid.ColumnCount; j++)
{
CustomDataGridViewCell cellName = new CustomDataGridViewCell();
_MyGrid.Rows[i].Cells[j] = cellName;
cellName.AdvancedBorderStyle.Left = DataGridViewAdvancedCellBorderStyle.Inset;
cellName.AdvancedBorderStyle.Right = DataGridViewAdvancedCellBorderStyle.Inset;
cellName.AdvancedBorderStyle.Top = DataGridViewAdvancedCellBorderStyle.Inset;
cellName.AdvancedBorderStyle.Bottom = DataGridViewAdvancedCellBorderStyle.Inset;
}
}
//Or in simple way, you can write only this single line code
//this._MyGrid.CellBorderStyle = DataGridViewCellBorderStyle.Single;
}
private void tsVertical_Click(object sender, EventArgs e)
{
VerticalLines();
//Or in simple way, you can write only this single line code
//this._MyGrid.CellBorderStyle = DataGridViewCellBorderStyle.SingleVertical;
}
private void tsHorizontle_Click(object sender, EventArgs e)
{
HorizontleLines();
//Or in simple way, you can write only this single line code
//this._MyGrid.CellBorderStyle = DataGridViewCellBorderStyle.SingleHorizontal;
}
private void VerticalLines()
{
DataGridViewAdvancedBorderStyle mystyle = new DataGridViewAdvancedBorderStyle();
mystyle.All = DataGridViewAdvancedCellBorderStyle.None;
DataGridViewAdvancedBorderStyle myplaceholder = new DataGridViewAdvancedBorderStyle();
for (int i = 0; i < _MyGrid.RowCount; i++)
{
for (int j = 0; j < _MyGrid.ColumnCount; j++)
{
CustomDataGridViewCell cellName = new CustomDataGridViewCell();
_MyGrid.Rows[i].Cells[j] = cellName;
cellName.AdvancedBorderStyle.Left = DataGridViewAdvancedCellBorderStyle.Inset;
cellName.AdvancedBorderStyle.Right = DataGridViewAdvancedCellBorderStyle.Inset;
cellName.AdvancedBorderStyle.Top = DataGridViewAdvancedCellBorderStyle.None;
cellName.AdvancedBorderStyle.Bottom = DataGridViewAdvancedCellBorderStyle.None;
}
}
}
private void HorizontleLines()
{
DataGridViewAdvancedBorderStyle mystyle = new DataGridViewAdvancedBorderStyle();
mystyle.All = DataGridViewAdvancedCellBorderStyle.None;
DataGridViewAdvancedBorderStyle myplaceholder = new DataGridViewAdvancedBorderStyle();
for (int i = 0; i < _MyGrid.RowCount; i++)
{
for (int j = 0; j < _MyGrid.ColumnCount; j++)
{
CustomDataGridViewCell cellName = new CustomDataGridViewCell();
_MyGrid.Rows[i].Cells[j] = cellName;
cellName.AdvancedBorderStyle.Top = DataGridViewAdvancedCellBorderStyle.Inset;
cellName.AdvancedBorderStyle.Bottom = DataGridViewAdvancedCellBorderStyle.Inset;
cellName.AdvancedBorderStyle.Left = DataGridViewAdvancedCellBorderStyle.None;
cellName.AdvancedBorderStyle.Right = DataGridViewAdvancedCellBorderStyle.None;
}
}
}
private void button1_Click(object sender, EventArgs e)// here we add the rows on the grid control
{
_MyGrid.Rows.Add();
}
}
//here the customized class for the grid lines
public class CustomDataGridViewCell : DataGridViewTextBoxCell
{
private DataGridViewAdvancedBorderStyle _style;
public CustomDataGridViewCell()
: base()
{
_style = new DataGridViewAdvancedBorderStyle();
_style.Bottom = DataGridViewAdvancedCellBorderStyle.None;
_style.Top = DataGridViewAdvancedCellBorderStyle.None;
_style.Left = DataGridViewAdvancedCellBorderStyle.None;
_style.Right = DataGridViewAdvancedCellBorderStyle.None;
}
public DataGridViewAdvancedBorderStyle AdvancedBorderStyle
{
get { return _style; }
set
{
_style.Bottom = value.Bottom;
_style.Top = value.Top;
_style.Left = value.Left;
_style.Right = value.Right;
}
}
protected override void PaintBorder(Graphics graphics, Rectangle clipBounds, Rectangle bounds, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle)
{
base.PaintBorder(graphics, clipBounds, bounds, cellStyle, _style);
}
protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
{
base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, value, formattedValue, errorText, cellStyle, _style, paintParts);
}
}
}
Subscribe to:
Post Comments (Atom)
http://www.codeproject.com/Articles/34337/How-to-merge-cells-with-equal-values-in-a-GridView
ReplyDeleteYe link open karo or ye ho sakta h to kar ke send karo
shashi.ranjan766@gmail.com pe
enna da ithu.....oru thelivana bathil irukka?
ReplyDelete