pmsc

Hi,

I'm trying to develop an application that does the following using a DGV.

Each row correspond to one user and it's going to show the availabilities of that user. So I have columns for the hours 8am to 6pm for Monday to Friday. Now what I want is do draw rectangles that span more than one column (only one row) to show when the user is available. I should be able to double click on the rectangle to change times and other options. If possible I should also be able to drag the rectangle from side to side to move the availability slot.

Any suggestions on how to do this I assume I need to do some custom painting, does anyone know of a good tutorial on how to do this

Thanks,

Pedro



Re: Windows Forms Data Controls and Databinding Custom painting

Ken Tucker

Maybe the RowPostEvent will help you.





Re: Windows Forms Data Controls and Databinding Custom painting

Rong-Chun Zhang - MSFT

Hi pmsc,

Do you mean you want to merge some cell in the same row if so, check this sample.

Code Snippet

namespace DGV

{

public partial class DGVMergeRows : Form

{

public DGVMergeRows()

{

InitializeComponent();

}

DataTable dt = new DataTable();

private void DGVMergeRows_Load(object sender, EventArgs e)

{

dt.Columns.Add("A");

dt.Columns.Add("B");

dt.Columns.Add("C");

dt.Columns.Add("D");

dt.Rows.Add(new object[] { "A1", "B1", "C1", "D1" });

dt.Rows.Add(new object[] { "A2", "B2", "C2", "D2" });

dt.Rows.Add(new object[] { "A3", "B3", "C3", "D3" });

this.dataGridView1.DataSource = dt;

this.dataGridView1.CellPainting += new DataGridViewCellPaintingEventHandler(dataGridView1_CellPainting);

}

private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)

{

if (e.RowIndex < 0 || e.RowIndex >= this.dataGridView1.Rows.Count - 1 || e.ColumnIndex == -1)

{

return;

}

if (e.ColumnIndex == 1 && this.dataGridView1[e.RowIndex, e.ColumnIndex + 1].Value.ToString() == "C3")

{

e.Handled = true;

}

if (e.ColumnIndex == 2 && e.Value.ToString() == "C3")

{

DataGridViewCell preCell = this.dataGridView1[e.ColumnIndex - 1, e.RowIndex];

Rectangle re = new Rectangle(e.CellBounds.Left - this.dataGridView1.Columns[e.ColumnIndex - 1].Width

, e.CellBounds.Top, e.CellBounds.Width + this.dataGridView1.Columns[e.ColumnIndex - 1].Width, e.CellBounds.Height);

e.Graphics.FillRectangle(Brushes.White, re);

Pen pen = new Pen(this.dataGridView1.BackgroundColor, 1);

pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Solid;

e.Graphics.DrawLine(pen, re.X, re.Y + re.Height - 1, re.X + re.Width, re.Y + re.Height - 1);

e.Graphics.DrawLine(pen, re.X + re.Width - 1, re.Y, re.X + re.Width - 1, re.Y + re.Height);

SizeF strSize = e.Graphics.MeasureString(e.Value.ToString(), this.dataGridView1.Font);

e.Graphics.DrawString(e.Value.ToString(), this.dataGridView1.Font

, Brushes.Black, re.X, re.Y + (re.Height - strSize.Height) / 2);

e.Handled = true;

}

}

}

}

Hope this helps.

Regards






Re: Windows Forms Data Controls and Databinding Custom painting

pmsc

Hi again,

What I want is for the 'availability' to be able to cover half of columns, ie just merging cells is not sufficient. So what I basically want is to position another control on a specific row and then have the control stay with that row as a I scroll the DGV. Because I assume that if I paint on the DGV using the Paint event I won't be able to click on it later, it will simply be part of the DGV, not an editable object.

In other words I want something similar to the calander view of Outlook where you can create events that you can move around on the calender. Any suggestions on how to do that

Thanks,

Pedro