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 MyWinFormSample
{
public partial class Form7 : Form
{
public Form7()
{
InitializeComponent();
this.detailControl = new DetailUserControl { Visible = false };
this.detailControl.BackColor = Color.Yellow;
this.Controls.Add(detailControl);
this.Controls.SetChildIndex(detailControl, 0);
}
private UserControl detailControl;
private void Form7_Load(object sender, EventArgs e)
{
DataGridViewTextBoxColumn col = new DataGridViewTextBoxColumn();
col.DefaultCellStyle.NullValue = "+";
col.HeaderText = "";
col.Width = 30;
col.ReadOnly = true;
col.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
this.dataGridView1.Columns.Add(col);
this.dataGridView1.CellClick += new DataGridViewCellEventHandler(dataGridView1_CellClick);
}
void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 0)
{
if (this.dataGridView1[e.ColumnIndex, e.RowIndex].Value == null)
this.dataGridView1[e.ColumnIndex, e.RowIndex].Value = "+";
if (this.dataGridView1[e.ColumnIndex, e.RowIndex].Value.ToString().Trim() == "+")
{
//Rebound the detail form to show new data.
var cellBounds = this.dataGridView1.GetCellDisplayRectangle(e.ColumnIndex,e.RowIndex,false);
var offsetLocation = new Point(cellBounds.X, cellBounds.Y + cellBounds.Height);
offsetLocation.Offset(this.dataGridView1.Location);
detailControl.Location = offsetLocation;
detailControl.Show();
this.dataGridView1[e.ColumnIndex, e.RowIndex].Value = "-";
}
else
{
detailControl.Hide();
this.dataGridView1[e.ColumnIndex, e.RowIndex].Value = "+";
}
}
}
}
}
Let me know if this does not help.
http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/338a7dbc-20ba-4304-8d48-b6774626210c/