finally i done with reference of
http://www.softwaremasons.com/ChristysBlog/tabid/65/EntryId/19/Writing-a-Popup-Textbox-UserControl-for-a-DataGridView-in-Windows-Forms-ndash-Part-1.aspx[
^]
http://www.softwaremasons.com/ChristysBlog/tabid/65/EntryId/22/Writing-a-Popup-Textbox-UserControl-for-a-DataGridView-in-Windows-Forms-ndash-Part-2.aspx[
^]
//usercontrol design mode take textbox and button
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Test_Win
{
public partial class DataGridViewButtonTextboxColumn : UserControl, IDataGridViewEditingControl
{
private DataGridView dataGridView;
private bool valueChanged;
private int rowIndex;
private int _maxInputLength = 255;
Lookup objfrm = null;
public DataGridViewButtonTextboxColumn()
{
this.TabStop = false;
InitializeComponent();
this.txtEnter.MaxLength = _maxInputLength;
}
private void BtnOk_Click(object sender, EventArgs e)
{
objfrm = new Lookup();
objfrm.ShowDialog();
if (objfrm.EmpNumber != null)
txtEnter.Text = objfrm.EmpNumber;
}
private void txtEnter_TextChanged(object sender, EventArgs e)
{
this.OnTextChanged(e);
}
protected override void OnGotFocus(EventArgs e)
{
base.OnGotFocus(e);
this.txtEnter.Focus();
}
[Category("Appearance"), Description("Text Value"), DisplayName("TextValue")]
public string TextValue
{
get
{
return this.txtEnter.Text;
}
set
{
if (value.Length <= MaxLength)
{
this.txtEnter.Text = value;
}
}
}
[Category("Behavior"), Description("Max Input Length"), DefaultValue(255), DisplayName("MaxLength")]
public int MaxLength
{
get
{
return _maxInputLength;
}
set
{
if (value > 0)
{
_maxInputLength = value;
this.txtEnter.MaxLength = _maxInputLength;
}
}
}
#region Properties
#region Properties for implementation of IDataGridViewEditingControl interface
public virtual DataGridView EditingControlDataGridView
{
get
{
return this.dataGridView;
}
set
{
this.dataGridView = value;
}
}
public virtual object EditingControlFormattedValue
{
get
{
return GetEditingControlFormattedValue(DataGridViewDataErrorContexts.Formatting);
}
set
{
this.TextValue = (string)value;
}
}
public virtual int EditingControlRowIndex
{
get
{
return this.rowIndex;
}
set
{
this.rowIndex = value;
}
}
public virtual bool EditingControlValueChanged
{
get
{
return this.valueChanged;
}
set
{
this.valueChanged = value;
}
}
public virtual Cursor EditingPanelCursor
{
get
{
return Cursors.Default;
}
}
public virtual bool RepositionEditingControlOnValueChange
{
get
{
return false;
}
}
#endregion
#endregion
#region Methods
#region Methods for implementation of IDataGridViewEditingControl interface
public virtual void ApplyCellStyleToEditingControl(DataGridViewCellStyle dataGridViewCellStyle)
{
this.Font = dataGridViewCellStyle.Font;
this.BackColor = dataGridViewCellStyle.BackColor;
this.ForeColor = dataGridViewCellStyle.ForeColor;
}
public virtual bool EditingControlWantsInputKey(Keys keyData, bool dataGridViewWantsInputKey)
{
switch (keyData & Keys.KeyCode)
{
case Keys.Right:
case Keys.Left:
case Keys.Tab:
case Keys.Enter:
dataGridViewWantsInputKey = true;
return false;
default:
dataGridViewWantsInputKey = false;
return true;
}
}
public virtual object GetEditingControlFormattedValue(DataGridViewDataErrorContexts context)
{
return this.TextValue;
}
public virtual void PrepareEditingControlForEdit(bool selectAll)
{
txtEnter.MaxLength = 5000;
}
#endregion
private void NotifyDataGridViewOfValueChange()
{
if (!this.valueChanged)
{
this.valueChanged = true;
this.dataGridView.NotifyCurrentCellDirty(true);
}
}
protected override void OnTextChanged(EventArgs e)
{
base.OnTextChanged(e);
NotifyDataGridViewOfValueChange();
}
#endregion
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.ComponentModel;
using System.Drawing;
namespace Test_Win
{
public class TextBoxButtonColumn : DataGridViewColumn
{
public TextBoxButtonColumn()
{
this.CellTemplate = new TextBoxButtonCell();
}
#region Properties
[Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public override DataGridViewCell CellTemplate
{
get
{
return base.CellTemplate;
}
set
{
TextBoxButtonCell dataGridViewCustomEditCell = value as TextBoxButtonCell;
if (value != null && dataGridViewCustomEditCell == null)
{
throw new InvalidCastException(
"Value provided for CellTemplate must be of type " +
"DataGridViewCustomEditElements.DataGridViewCustomEditCell " +
"or derive from it.");
}
base.CellTemplate = value;
}
}
private TextBoxButtonCell EllipsesTextboxCellTemplate
{
get
{
return (TextBoxButtonCell)this.CellTemplate;
}
}
[
Category("Behavior"),
DefaultValue(TextBoxButtonCell.DATAGRIDVIEWELLIPSESTEXTBOXCELL_defaultMaxLength),
Description("Indicates the maximum number of characters in the cell.")
]
public int MaxInputLength
{
get
{
if (this.EllipsesTextboxCellTemplate == null)
{
throw new InvalidOperationException(
"Operation cannot be completed because this DataGridViewColumn " +
"does not have a CellTemplate.");
}
return this.EllipsesTextboxCellTemplate.MaxInputLength;
}
set
{
if (this.EllipsesTextboxCellTemplate == null)
{
throw new InvalidOperationException(
"Operation cannot be completed because this DataGridViewColumn " +
"does not have a CellTemplate.");
}
this.EllipsesTextboxCellTemplate.MaxInputLength = value;
if (this.DataGridView != null)
{
DataGridViewRowCollection dataGridViewRows = this.DataGridView.Rows;
int rowCount = dataGridViewRows.Count;
for (int rowIndex = 0; rowIndex < rowCount; rowIndex++)
{
DataGridViewRow dataGridViewRow = dataGridViewRows.SharedRow(rowIndex);
TextBoxButtonCell dataGridViewCell = dataGridViewRow.Cells[this.Index] as TextBoxButtonCell;
if (dataGridViewCell != null)
{
dataGridViewCell.SetMaxLength(rowIndex, value);
}
}
this.DataGridView.InvalidateColumn(this.Index);
}
}
}
#endregion
#region Methods
#region Override Methods
public override object Clone()
{
TextBoxButtonColumn dataGridViewColumn = base.Clone() as TextBoxButtonColumn;
if (dataGridViewColumn != null)
{
}
return dataGridViewColumn;
}
#endregion
#endregion
}
public class TextBoxButtonCell : DataGridViewTextBoxCell
{
#region Default constants for DataGridViewEllipsesTextboxCell
internal const int DATAGRIDVIEWELLIPSESTEXTBOXCELL_defaultMaxLength = 2000;
private const int DATAGRIDVIEWELLIPSESTEXTBOXCELL_defaultRenderingBitmapWidth = 100;
private const int DATAGRIDVIEWELLIPSESTEXTBOXCELL_defaultRenderingBitmapHeight = 22;
#endregion
#region Private Properties
private static readonly DataGridViewContentAlignment anyRight = DataGridViewContentAlignment.TopRight |
DataGridViewContentAlignment.MiddleRight |
DataGridViewContentAlignment.BottomRight;
private static readonly DataGridViewContentAlignment anyCenter = DataGridViewContentAlignment.TopCenter |
DataGridViewContentAlignment.MiddleCenter |
DataGridViewContentAlignment.BottomCenter;
private static Type defaultEditType = typeof(DataGridViewButtonTextboxColumn);
[ThreadStatic]
private static Bitmap renderingBitmap;
[ThreadStatic]
private static TextBox paintingTextbox;
#endregion
public TextBoxButtonCell()
{
if (renderingBitmap == null)
{
renderingBitmap = new Bitmap(DATAGRIDVIEWELLIPSESTEXTBOXCELL_defaultRenderingBitmapWidth, DATAGRIDVIEWELLIPSESTEXTBOXCELL_defaultRenderingBitmapHeight);
}
if (paintingTextbox == null)
{
paintingTextbox = new TextBox();
paintingTextbox.BorderStyle = BorderStyle.None;
}
this.MaxInputLength = DATAGRIDVIEWELLIPSESTEXTBOXCELL_defaultMaxLength;
}
#region Overridden Properties of DataGridViewCell
[
DefaultValue(DATAGRIDVIEWELLIPSESTEXTBOXCELL_defaultMaxLength)
]
public override int MaxInputLength
{
get
{
return base.MaxInputLength;
}
set
{
if (value < 0 || value > 3000)
{
throw new ArgumentOutOfRangeException("The MaxLength property cannot be smaller than 0 or larger than 3000");
}
if (base.MaxInputLength != value)
{
SetMaxLength(this.RowIndex, value);
OnCommonChange();
}
}
}
public override Type EditType
{
get
{
return defaultEditType;
}
}
private DataGridViewButtonTextboxColumn EditingCustomEdit
{
get
{
return this.DataGridView.EditingControl as DataGridViewButtonTextboxColumn;
}
}
#endregion
public override object Clone()
{
TextBoxButtonCell dataGridViewCell = base.Clone() as TextBoxButtonCell;
if (dataGridViewCell != null)
{
dataGridViewCell.MaxInputLength = this.MaxInputLength;
}
return dataGridViewCell;
}
protected override void OnClick(DataGridViewCellEventArgs e)
{
if (base.DataGridView != null)
{
base.DataGridView.BeginEdit(true);
}
base.OnClick(e);
}
protected override void OnContentClick(DataGridViewCellEventArgs e)
{
base.OnContentClick(e);
}
public override bool KeyEntersEditMode(KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
return true;
}
else
{
return false;
}
}
public override void InitializeEditingControl(int rowIndex, object initialFormattedValue,
DataGridViewCellStyle dataGridViewCellStyle)
{
base.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle);
DataGridViewButtonTextboxColumn ellipsesTextBox = this.DataGridView.EditingControl as DataGridViewButtonTextboxColumn;
if (ellipsesTextBox != null)
{
ellipsesTextBox.MaxLength = this.MaxInputLength;
string initialFormattedValueStr = initialFormattedValue as string;
if (initialFormattedValueStr == null)
{
ellipsesTextBox.TextValue = string.Empty;
}
else
{
ellipsesTextBox.TextValue = initialFormattedValueStr;
}
}
}
public override void PositionEditingControl(bool setLocation, bool setSize,
Rectangle cellBounds, Rectangle cellClip, DataGridViewCellStyle cellStyle,
bool singleVerticalBorderAdded, bool singleHorizontalBorderAdded,
bool isFirstDisplayedColumn, bool isFirstDisplayedRow)
{
Rectangle editingControlBounds = PositionEditingPanel(cellBounds, cellClip, cellStyle,
singleVerticalBorderAdded, singleHorizontalBorderAdded, isFirstDisplayedColumn,
isFirstDisplayedRow);
editingControlBounds = GetAdjustedEditingControlBounds(editingControlBounds, cellStyle);
this.DataGridView.EditingControl.Location
= new Point(editingControlBounds.X, editingControlBounds.Y);
this.DataGridView.EditingControl.Size
= new Size(editingControlBounds.Width, editingControlBounds.Height);
}
[
EditorBrowsable(EditorBrowsableState.Advanced)
]
public override void DetachEditingControl()
{
DataGridView dataGridView = this.DataGridView;
if (dataGridView == null || dataGridView.EditingControl == null)
{
throw new InvalidOperationException(
"Cell is detached or its grid has no editing control.");
}
DataGridViewButtonTextboxColumn ellipsesTextBox = dataGridView.EditingControl as DataGridViewButtonTextboxColumn;
if (ellipsesTextBox != null)
{
DataGridViewButtonTextboxColumn textBox = ellipsesTextBox as DataGridViewButtonTextboxColumn;
}
base.DetachEditingControl();
}
protected override Size GetPreferredSize(Graphics graphics, DataGridViewCellStyle cellStyle,
int rowIndex, Size constraintSize)
{
if (this.DataGridView == null)
{
return new Size(-1, 01);
}
Size preferredSize = base.GetPreferredSize(graphics, cellStyle, rowIndex, constraintSize);
if (constraintSize.Width == 0)
{
const int ButtonWidth = 16;
const int ButtonMargin = 8;
preferredSize.Width += ButtonWidth + ButtonMargin;
}
return preferredSize;
}
protected override Rectangle GetErrorIconBounds(Graphics graphics,
DataGridViewCellStyle cellStyle, int rowIndex)
{
const int ButtonWidth = 16;
Rectangle errorIconBounds = base.GetErrorIconBounds(graphics, cellStyle, rowIndex);
if (this.DataGridView.RightToLeft == RightToLeft.Yes)
{
errorIconBounds.X = errorIconBounds.Left + ButtonWidth;
}
else
{
errorIconBounds.X = errorIconBounds.Left - ButtonWidth;
}
return errorIconBounds;
}
internal void SetMaxLength(int rowIndex, int value)
{
base.MaxInputLength = value;
}
internal void SetEnterInformationForm(int rowIndex)
{
if (OwnsEditingCustomEdit(rowIndex))
{
}
}
private Rectangle GetAdjustedEditingControlBounds(Rectangle editingControlBounds, DataGridViewCellStyle cellStyle)
{
editingControlBounds.X += 1;
editingControlBounds.Width = Math.Max(0, editingControlBounds.Width - 2);
int preferredHeight = cellStyle.Font.Height + 3;
if (preferredHeight < editingControlBounds.Height)
{
switch (cellStyle.Alignment)
{
case DataGridViewContentAlignment.MiddleLeft:
case DataGridViewContentAlignment.MiddleCenter:
case DataGridViewContentAlignment.MiddleRight:
editingControlBounds.Y += (editingControlBounds.Height - preferredHeight) / 2;
break;
case DataGridViewContentAlignment.BottomLeft:
case DataGridViewContentAlignment.BottomCenter:
case DataGridViewContentAlignment.BottomRight:
editingControlBounds.Y += editingControlBounds.Height - preferredHeight;
break;
}
}
return editingControlBounds;
}
private void OnCommonChange()
{
if (this.DataGridView != null && !this.DataGridView.IsDisposed && !this.DataGridView.Disposing)
{
if (this.RowIndex == -1)
{
this.DataGridView.InvalidateColumn(this.ColumnIndex);
}
else
{
this.DataGridView.UpdateCellValue(this.ColumnIndex, this.RowIndex);
}
}
}
private bool OwnsEditingCustomEdit(int rowIndex)
{
if (rowIndex == -1 || this.DataGridView == null)
{
return false;
}
DataGridViewButtonTextboxColumn customEditControl = this.DataGridView.EditingControl as DataGridViewButtonTextboxColumn;
return customEditControl != null
&& rowIndex == ((IDataGridViewEditingControl)customEditControl).EditingControlRowIndex;
}
}
}