Hi KenFirstly thanks for replying for my question. What you have told is a decent idea. But I really wanted an user control (a combination of text box + combo) as part of the datagridview cell. The eg of DatePicker in MSDN is helpful but I am struggling a bit when I am having an UserControl.
My UserControl basically is going to be a textbox + drop-down, where the user types in a value and the combo then gets filled with the result values.
I have got the new column type working down now to where I can see my user control in the datagridview where in the user can type the value in the text box of the user control and see the matching values in the combo of the user control. But, what I am not getting is the value doesn't get set in the cell when I move to the next row or do something else on the grid. I lose the value which I have searched in the text box and selected the desired value from the drop down.
Pasted is below my code...for User control. The stored proc spGetCompany is just retrieving the descriptions of companies which matches the value entered in the text box.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using FNF.Data;
namespace WindowsApplication1
{
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
}
private string textBoxValue = string.Empty;
public string ComboValue
{
get
{
if (this.comboBox1.SelectedValue != null)
{
return this.comboBox1.SelectedValue.ToString();
}
else
{
return string.Empty;
}
}
}
public string TextBoxValue
{
get
{
return this.textBox1.Text.ToString();
}
set
{
textBox1.Text = value;
}
}
public ComboBox ComboControl
{
get
{
return comboBox1;
}
}
public TextBox TextBoxControl
{
get
{
return textBox1;
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
if (textBox1.Text.Length > 0)
{
DataSet ds = GetSelectedCompanies(textBox1.Text);
if (ds != null)
{
if (ds.Tables[0].Rows.Count < 100)
{
comboBox1.DisplayMember = "name";
comboBox1.ValueMember = "agentno";
comboBox1.DataSource = ds.Tables[0];
}
else
{
return;
}
}
}
}
private DataSet GetSelectedCompanies(string companyDescription)
{
return DBClient.RunStoredProcDatasetWithParms("spGetCompany", "companyDesc", companyDescription);
}
private void comboBox1_SelectedValueChanged(object sender, System.EventArgs e)
{
textBox1.Text = comboBox1.Text;
if (comboBox1.Text.Length > 0)
{
int found = comboBox1.Text.ToString().IndexOf("[", 1);
if (found != -1)
{
textBox1.Text = comboBox1.Text.ToString().Substring(0, found-1);
}
}
}
protected override void OnTextChanged(EventArgs e)
{
base.OnTextChanged(e);
}
}
}
And here is the actual implementation of the customcell which has the type of UserControl in the datagridview
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication1
{
public class CustomColumn : DataGridViewColumn
{
public CustomColumn(): base(new CustomControlCell())
{
}
public override DataGridViewCell CellTemplate
{
get
{
return base.CellTemplate;
}
set
{
// Ensure that the cell used for the template is a CalendarCell.
if (value != null &&
!value.GetType().IsAssignableFrom(typeof(CustomControlCell)))
{
throw new InvalidCastException("Must be a CustomControlCell");
}
base.CellTemplate = value;
}
}
private void InitializeComponent()
{
}
}
public class CustomControlCell : DataGridViewTextBoxCell
{
public CustomControlCell()
: base()
{
}
public override void InitializeEditingControl(int rowIndex, object
initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
{
// Set the value of the editing control to the current cell value.
base.InitializeEditingControl(rowIndex, initialFormattedValue,
dataGridViewCellStyle);
CustomEditingControl ctl = DataGridView.EditingControl as CustomEditingControl;
ctl.TextBoxControl.Text = this.Value.ToString();
//ctl.Value = (DateTime)this.Value;
}
public override Type EditType
{
get
{
// Return the type of the editing contol that CustomControlCell uses.
return typeof(CustomEditingControl);
}
}
public override Type ValueType
{
get
{
// Return the type of the value that CustomControlCell contains.
return typeof(String);
}
}
public override object DefaultNewRowValue
{
get
{
// Use the current date and time as the default value.
return string.Empty;
}
}
}
class CustomEditingControl : UserControl1, IDataGridViewEditingControl
{
DataGridView dataGridView;
private bool valueChanged = false;
int rowIndex;
public CustomEditingControl()
{
}
// Implements the IDataGridViewEditingControl.EditingControlFormattedValue
// property.
public object EditingControlFormattedValue
{
get
{
return this.TextBoxValue;
//return this.ComboValue;
//this.Value.ToShortDateString();
}
set
{
this.TextBoxValue = value.ToString();
//if (value is String)
//{
// this.Value = DateTime.Parse((String)value);
//}
}
}
// Implements the
// IDataGridViewEditingControl.GetEditingControlFormattedValue method.
public object GetEditingControlFormattedValue(
DataGridViewDataErrorContexts context)
{
return EditingControlFormattedValue;
}
// Implements the
// IDataGridViewEditingControl.ApplyCellStyleToEditingControl method.
public void ApplyCellStyleToEditingControl(
DataGridViewCellStyle dataGridViewCellStyle)
{
this.ComboControl.Font = dataGridViewCellStyle.Font;
this.ComboControl.ForeColor = dataGridViewCellStyle.ForeColor;
this.ComboControl.BackColor = dataGridViewCellStyle.BackColor;
}
// Implements the IDataGridViewEditingControl.EditingControlRowIndex
// property.
public int EditingControlRowIndex
{
get
{
return rowIndex;
}
set
{
rowIndex = value;
}
}
// Implements the IDataGridViewEditingControl.EditingControlWantsInputKey
// method.
public bool EditingControlWantsInputKey(Keys key, bool dataGridViewWantsInputKey)
{
// Let the DateTimePicker handle the keys listed.
switch (key & Keys.KeyCode)
{
case Keys.Left:
case Keys.Up:
case Keys.Down:
case Keys.Right:
case Keys.Home:
case Keys.End:
case Keys.PageDown:
case Keys.PageUp:
return true;
default:
return false;
}
}
// Implements the IDataGridViewEditingControl.PrepareEditingControlForEdit
// method.
public void PrepareEditingControlForEdit(bool selectAll)
{
// No preparation needs to be done.
}
// Implements the IDataGridViewEditingControl
// .RepositionEditingControlOnValueChange property.
public bool RepositionEditingControlOnValueChange
{
get
{
return false;
}
}
// Implements the IDataGridViewEditingControl
// .EditingControlDataGridView property.
public DataGridView EditingControlDataGridView
{
get
{
return dataGridView;
}
set
{
dataGridView = value;
}
}
// Implements the IDataGridViewEditingControl
// .EditingControlValueChanged property.
public bool EditingControlValueChanged
{
get
{
return valueChanged;
}
set
{
valueChanged = value;
}
}
// Implements the IDataGridViewEditingControl
// .EditingPanelCursor property.
public Cursor EditingPanelCursor
{
get
{
return base.Cursor;
}
}
protected override void OnTextChanged(EventArgs eventargs)
{
// notify the datagridview that the contents of the cell
// have changed.
valueChanged = true;
this.EditingControlDataGridView.NotifyCurrentCellDirty(true);
base.OnTextChanged(eventargs);
// base.OnValueChanged(eventargs);
}
}
public partial class Form2 : Form
{
private DataGridView dataGridView1 = new DataGridView();
public Form2()
{
InitializeComponent();
this.dataGridView1.Dock = DockStyle.Fill;
this.Controls.Add(this.dataGridView1);
this.Load += new EventHandler(Form2_Load);
this.Text = "DataGridView calendar column demo";
}
private void Form2_Load(object sender, System.EventArgs e)
{
//CalendarColumn col = new CalendarColumn();
CustomColumn col = new CustomColumn();
this.dataGridView1.Columns.Add(col);
this.dataGridView1.RowCount = 5;
foreach (DataGridViewRow row in this.dataGridView1.Rows)
{
row.Cells[0].Value = "Beavo, LLC";
}
}
}
}