|
Hi everyone, I would like to represent a 2D matrix in a graphic interface, so could someone tell me which component I can use to do this in visual studio. Than you for any help. Regards
CardSpace | | AsToni Wednesday, September 09, 2009 8:45 AM | Hi AsToni, From my experience, we can use the DataGridView to display a matrix. We can set the RowHeadersVisible and ColumnHeadersVisible properties to false to hide the headers.If we want to show a new matrix within a cell, we need to add a child DataGridView to the main DataGridView to cover the cell region and load matrix data to it. This is a code snippet shows how to embed a child DataGridView to the main DataGridView:
//Create and initialize the DataGridView.
DataGridView view = new DataGridView();
view.ColumnHeadersVisible = false;
view.RowHeadersVisible = false;
//Adjust the location and size of the new DataGridView to cover the cell.
view.Bounds = this.dataGridView1.GetCellDisplayRectangle(this.dataGridView1.CurrentCell.ColumnIndex, this.dataGridView1.CurrentCell.RowIndex, true);
//Load data to dataGridView here.
this.dataGridView1.Controls.Add(view);
We can also create our own control to draw the matrix data. This is a code snippet:
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;
namespace MyForm.DrawSomething
{
public partial class DrawMatrixForm : Form
{
public DrawMatrixForm()
{
InitializeComponent();
}
private void DrawMatrixForm_Load(object sender, EventArgs e)
{
MatrixCtrl ctrl = new MatrixCtrl();
ctrl.Dock = DockStyle.Fill;
ctrl.Matrix = new int[][] { new int[] { 1, 2 }, new int[] { 3, 4 }, new int[] { 5, 6 } };
this.Controls.Add(ctrl);
}
}
public class MatrixCtrl : Control
{
//Matrix data.
private int[][] _matrix = null;
//The size of each cell.
private Size _cellSize = Size.Empty;
public int[][] Matrix
{
get { return _matrix; }
set
{
_matrix = value;
}
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
if (_matrix != null && _cellSize == Size.Empty)
{
//Initialize the cell size.
_cellSize.Width = this.ClientRectangle.Width / _matrix[0].Length;
_cellSize.Height = this.ClientRectangle.Height / _matrix.Length;
}
//Draw cells including cells and borders.
if (_matrix != null && _cellSize != Size.Empty)
{
for (int i = 0; i < _matrix.Length; i++)
for (int j = 0; j < _matrix[0].Length; j++)
{
this.DrawCell(i, j, e.Graphics);
}
}
}
private void DrawCell(int i, int j, Graphics g)
{
//Calculate the cell bounds.
Rectangle rect = new Rectangle();
rect.X = j * _cellSize.Width;
rect.Y = i * _cellSize.Height;
rect.Size = _cellSize;
//Draw the data.
using (Brush b = new SolidBrush(this.ForeColor))
{
g.DrawString(_matrix[i][j].ToString(), this.Font, b, rect,Format);
}
//Draw the borders.
g.DrawRectangle(Pens.Black, rect);
}
private static StringFormat Format
{
get
{
if (_format == null)
{
_format = new StringFormat();
_format.LineAlignment = StringAlignment.Center;
_format.Alignment = StringAlignment.Center;
}
return _format;
}
}
private static StringFormat _format = null;
}
}
Regards, Aland Li
Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread. - Marked As Answer byAland LiMSFT, ModeratorWednesday, September 16, 2009 10:33 AM
-
| | Aland Li Friday, September 11, 2009 4:44 AM | What kind of data is it..you may use datagrid to represent it.. | | Bharath kumar Y.S Wednesday, September 09, 2009 8:58 AM | Yes I know this one, but I would like to know if there is another component more advance for a matrix, so I can represent different nested levels. For example, a cell can be expanded and show another matrix within itself. CardSpace | | AsToni Wednesday, September 09, 2009 9:10 AM | Hi AsToni, From my experience, we can use the DataGridView to display a matrix. We can set the RowHeadersVisible and ColumnHeadersVisible properties to false to hide the headers.If we want to show a new matrix within a cell, we need to add a child DataGridView to the main DataGridView to cover the cell region and load matrix data to it. This is a code snippet shows how to embed a child DataGridView to the main DataGridView:
//Create and initialize the DataGridView.
DataGridView view = new DataGridView();
view.ColumnHeadersVisible = false;
view.RowHeadersVisible = false;
//Adjust the location and size of the new DataGridView to cover the cell.
view.Bounds = this.dataGridView1.GetCellDisplayRectangle(this.dataGridView1.CurrentCell.ColumnIndex, this.dataGridView1.CurrentCell.RowIndex, true);
//Load data to dataGridView here.
this.dataGridView1.Controls.Add(view);
We can also create our own control to draw the matrix data. This is a code snippet:
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;
namespace MyForm.DrawSomething
{
public partial class DrawMatrixForm : Form
{
public DrawMatrixForm()
{
InitializeComponent();
}
private void DrawMatrixForm_Load(object sender, EventArgs e)
{
MatrixCtrl ctrl = new MatrixCtrl();
ctrl.Dock = DockStyle.Fill;
ctrl.Matrix = new int[][] { new int[] { 1, 2 }, new int[] { 3, 4 }, new int[] { 5, 6 } };
this.Controls.Add(ctrl);
}
}
public class MatrixCtrl : Control
{
//Matrix data.
private int[][] _matrix = null;
//The size of each cell.
private Size _cellSize = Size.Empty;
public int[][] Matrix
{
get { return _matrix; }
set
{
_matrix = value;
}
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
if (_matrix != null && _cellSize == Size.Empty)
{
//Initialize the cell size.
_cellSize.Width = this.ClientRectangle.Width / _matrix[0].Length;
_cellSize.Height = this.ClientRectangle.Height / _matrix.Length;
}
//Draw cells including cells and borders.
if (_matrix != null && _cellSize != Size.Empty)
{
for (int i = 0; i < _matrix.Length; i++)
for (int j = 0; j < _matrix[0].Length; j++)
{
this.DrawCell(i, j, e.Graphics);
}
}
}
private void DrawCell(int i, int j, Graphics g)
{
//Calculate the cell bounds.
Rectangle rect = new Rectangle();
rect.X = j * _cellSize.Width;
rect.Y = i * _cellSize.Height;
rect.Size = _cellSize;
//Draw the data.
using (Brush b = new SolidBrush(this.ForeColor))
{
g.DrawString(_matrix[i][j].ToString(), this.Font, b, rect,Format);
}
//Draw the borders.
g.DrawRectangle(Pens.Black, rect);
}
private static StringFormat Format
{
get
{
if (_format == null)
{
_format = new StringFormat();
_format.LineAlignment = StringAlignment.Center;
_format.Alignment = StringAlignment.Center;
}
return _format;
}
}
private static StringFormat _format = null;
}
}
Regards, Aland Li
Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread. - Marked As Answer byAland LiMSFT, ModeratorWednesday, September 16, 2009 10:33 AM
-
| | Aland Li Friday, September 11, 2009 4:44 AM | Thank you, that is very helpful. I will try to integrate your solution in my code. Thank you for your reply CardSpace | | AsToni Tuesday, September 15, 2009 5:48 PM |
|