473,241 Members | 1,539 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,241 software developers and data experts.

DataGrid/ComboBox custom column problems

Hello,

I know this has been discussed a lot already because I've been searching
around for information the last few weeks.

I'm trying to implement a DataGridComboBoxColumn class. I've found
several examples on the web. They all seem to have problems, though
that I've been unable to resolve.

The most promising example I have found is at:

http://msdn.microsoft.com/msdnmag/is...s/default.aspx

However, it has the following problems:

1) combobox selection works fine but then when you leave the cell
whatever you just selected goes away.
2) moving up and down in the data grid w/ arrow keys causes the
following exception to be thrown:

System.ArgumentException: The ListManager's position must be equal to rowNum.
Parameter name: rowNum

I've seen some comments about adding an event handler for
SelectionChangeCommited so I added the following:

private void comboBox_SelectionChangeCommitted(object sender, EventArgs e)
{
DataRowView rowView = (DataRowView) this.comboBox.SelectedItem;
string s = (string) rowView.Row[this.comboBox.ValueMember];
this.ColumnStartedEditing(this.comboBox);
this.SetColumnValueAtRow(this.cm, this.iCurrentRow, s);
}

This fixes problem #1 above but now it has a problem where the first
time you select something from the ComboBox it always selects the first
thing in the ComboBox list even if you clicked on the 2nd, 3rd or whatever.
Also, the arrow key problems are still there.

Does anybody know if there are more complete examples out there? Is
there a third party class that anybody is selling?

Also, I'm somewhat of a novice at this so if anybody could point to
some good books that have discussions of the relevant topics for
figuring this out, I'd really appreciate it.

thanks,

Bill
Nov 15 '05 #1
3 4213
Hi.
I already invest a lot o time in doing a such thing for me. You have to
derive a new class from System.Windows.Forms.DataGridComboBoxColumn. Try
this code:

using System;
using System.Drawing;

using System.Windows.Forms;

namespace RHSoft.UI

{

public class DataGridComboBoxColumn :
System.Windows.Forms.DataGridColumnStyle {

private System.Windows.Forms.ComboBox cboEdit;

private RHSoft.Data.Comparacoes.IComparable comparacao = null;

private System.Data.DataView view = null;

private bool blnLockEvent = false;

public DataGridComboBoxColumn() {

cboEdit = new System.Windows.Forms.ComboBox();

cboEdit.Visible = cboEdit.TabStop = false;

cboEdit.DropDownStyle = ComboBoxStyle.DropDownList;

cboEdit.DataSourceChanged += new EventHandler(EventComboDataSourceChanged);

cboEdit.SelectedIndexChanged += new
EventHandler(this.EventComboTextChanged);

}

public DataGridComboBoxColumn(System.Data.DataView view) : this() {

Init(view);

}

public void Init(System.Data.DataView view) {

this.view = view;

cboEdit.DataSource = this.view;

if (view.Table.Columns.Count > 0) {

if ((view.Count) > 0) {

comparacao =
RHSoft.Data.Comparacoes.IComparable.ConstructBased On(view[0][0].GetType(),
null);

}

else {

comparacao = new RHSoft.Data.Comparacoes.ComparableString(null);

}

//comparacao = (view.Count) > 0 ?
RHSoft.Data.Comparacoes.IComparable.ConstructBased On(view[0][0].GetType(),
null) : new RHSoft.Data.Comparacoes.ComparableString(null);

cboEdit.ValueMember = view.Table.Columns[0].ColumnName;

cboEdit.DisplayMember = view.Table.Columns[0].ColumnName;

}

}

protected override void ConcedeFocus() {

cboEdit.Hide();

}

protected override void Abort(int rowNum) {

}

// initiates a request to complete an editing procedure.

protected override bool Commit(System.Windows.Forms.CurrencyManager source,
int rowNum) {

cboEdit.Hide();

return true;

}

// Prepares the cell for editing a value.

protected override void Edit(System.Windows.Forms.CurrencyManager source,
int rowNum, Rectangle bounds, bool readOnly) {

}

// Prepares the cell for editing using the specified CurrencyManager, row
number, and Rectangle parameters.

protected override void Edit(System.Windows.Forms.CurrencyManager source,
int rowNum, Rectangle bounds, bool readOnly, string instantText) {

}

protected override void Edit(System.Windows.Forms.CurrencyManager source,
int rowNum, Rectangle bounds, bool readOnly, string instantText, bool
cellIsVisible) {

if (!cellIsVisible) {

cboEdit.Hide();

return;

}

try {

object valor = this.GetColumnValueAtRow(source, rowNum);

string strValue = valor.ToString();

blnLockEvent = true;

cboEdit.Enabled = !readOnly;

cboEdit.SetBounds(bounds.X, bounds.Y, bounds.Width, bounds.Height);

cboEdit.Show();

cboEdit.Focus();

cboEdit.SelectedValue = strValue;

if (cboEdit.SelectedIndex >= 0 && cboEdit.SelectedValue.ToString() !=
strValue) {

cboEdit.SelectedIndex = -1;

cboEdit.SelectedItem = null;

}

}

catch(Exception ex) {

Console.WriteLine(ex.Message);

}

finally {

blnLockEvent = false;

}

}

protected override int GetMinimumHeight() {

return 20;

}

protected override int GetPreferredHeight(Graphics g, object value) {

return this.DataGridTableStyle.PreferredRowHeight;

}

protected override Size GetPreferredSize(Graphics g, object value) {

return new Size(this.DataGridTableStyle.PreferredColumnWidth,
this.DataGridTableStyle.PreferredRowHeight);

}

protected override void Paint(Graphics g, Rectangle bounds, CurrencyManager
source, int rowNum, Brush backBrush, Brush foreBrush, bool alignToRight) {

try {

g.FillRectangle(backBrush, bounds);

object data = this.GetColumnValueAtRow(source, rowNum);

if (data != System.DBNull.Value) {

if (cboEdit.ValueMember != cboEdit.DisplayMember) {

this.comparacao.Value = data;

int selectedIndex = RHSoft.Data.DataView.FindFirst(view, comparacao,
cboEdit.ValueMember);

if (selectedIndex >= 0)

data = view[selectedIndex][cboEdit.DisplayMember];

}

g.DrawString(data.ToString(), this.DataGridTableStyle.DataGrid.Font,
foreBrush, bounds.X, bounds.Y+2);

}

if (rowNum == this.DataGridTableStyle.DataGrid.CurrentRowIndex &&
this.DataGridTableStyle.GridColumnStyles.IndexOf(t his) ==
this.DataGridTableStyle.DataGrid.CurrentCell.Colum nNumber) {

g.DrawRectangle(SystemPens.ControlDark, bounds.Left, bounds.Top,
bounds.Width-1, bounds.Height-1);

}

}

catch(Exception ex) {

g.DrawString(ex.Message, this.DataGridTableStyle.DataGrid.Font, foreBrush,
bounds.X, bounds.Y+2);

}

}

public bool IsOpen() {

return cboEdit.DroppedDown;

}

protected override void Paint(Graphics g, Rectangle bounds,
System.Windows.Forms.CurrencyManager source, int rowNum, bool alignToRight)
{

Paint(g, bounds, source, rowNum, new System.Drawing.SolidBrush(((rowNum % 2)
0) ? this.DataGridTableStyle.AlternatingBackColor :

this.DataGridTableStyle.BackColor),
System.Drawing.SystemBrushes.ControlText, alignToRight);

}

protected override void Paint(Graphics g, Rectangle bounds,
System.Windows.Forms.CurrencyManager source, int rowNum) {

Paint(g, bounds, source, rowNum, false);

}

protected override void SetDataGridInColumn(System.Windows.Forms.DataGrid
dataGrid) {

if (this.DataGridTableStyle != null &&
!this.DataGridTableStyle.DataGrid.Controls.Contain s(cboEdit))

this.DataGridTableStyle.DataGrid.Controls.Add(cboE dit);

}

private void EventComboTextChanged(object sender, EventArgs e) {

if (blnLockEvent || !cboEdit.Focused ||
this.DataGridTableStyle.DataGrid.CurrentRowIndex < 0) {

return;

}

try {

if (cboEdit.SelectedItem == null) {

this.SetColumnValueAtRow((System.Windows.Forms.Cur rencyManager)this.DataGrid
TableStyle.DataGrid.BindingContext[this.DataGridTableStyle.DataGrid.DataSour
ce], this.DataGridTableStyle.DataGrid.CurrentRowIndex, System.DBNull.Value);

}

else {

this.SetColumnValueAtRow((System.Windows.Forms.Cur rencyManager)this.DataGrid
TableStyle.DataGrid.BindingContext[this.DataGridTableStyle.DataGrid.DataSour
ce], this.DataGridTableStyle.DataGrid.CurrentRowIndex,
cboEdit.SelectedValue);

}

bool dropped = this.cboEdit.DroppedDown;

object selectedItem = this.cboEdit.SelectedItem;

this.ColumnStartedEditing(cboEdit);

if (dropped && !this.cboEdit.DroppedDown) {

this.cboEdit.SelectedItem = selectedItem;

this.cboEdit.DroppedDown = true;

}

}

catch(Exception ex) {

Console.WriteLine(ex.Message);

}

}

private void EventComboDataSourceChanged(object sender, EventArgs e) {

if (cboEdit.DataSource is System.Data.DataTable) {

this.view = ((System.Data.DataTable)cboEdit.DataSource).Defaul tView;

}

else {

this.view = (System.Data.DataView)cboEdit.DataSource;

}

if (view.Table.Columns.Count > 0) {

comparacao = (view.Count) > 0 ?
RHSoft.Data.Comparacoes.IComparable.ConstructBased On(view[0][0].GetType(),
null) : new RHSoft.Data.Comparacoes.ComparableString(null);

}

}

public System.Windows.Forms.ComboBox ComboBox {

get {

return cboEdit;

}

}

protected override void Dispose(bool disposing) {

base.Dispose (disposing);

cboEdit.DataSourceChanged -= new EventHandler(EventComboDataSourceChanged);

cboEdit.SelectedIndexChanged -= new
EventHandler(this.EventComboTextChanged);

if (view != null) {

view.Dispose();

}

if (disposing) {

cboEdit.Dispose();

cboEdit = null;

view = null;

comparacao = null;

}

}

}

}
Nov 15 '05 #2
hi Sergio...

what does this code (the class) do?
RHSoft.Data.Comparacoes.IComparable comparacao
you seem to reference it quite a lot...

Saso

"Sérgio" <se*****@netvisao.pt> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Hi.
I already invest a lot o time in doing a such thing for me. You have to
derive a new class from System.Windows.Forms.DataGridComboBoxColumn. Try
this code:

using System;
using System.Drawing;

using System.Windows.Forms;

namespace RHSoft.UI

{

public class DataGridComboBoxColumn :
System.Windows.Forms.DataGridColumnStyle {

private System.Windows.Forms.ComboBox cboEdit;

private RHSoft.Data.Comparacoes.IComparable comparacao = null;

private System.Data.DataView view = null;

private bool blnLockEvent = false;

public DataGridComboBoxColumn() {

cboEdit = new System.Windows.Forms.ComboBox();

cboEdit.Visible = cboEdit.TabStop = false;

cboEdit.DropDownStyle = ComboBoxStyle.DropDownList;

cboEdit.DataSourceChanged += new EventHandler(EventComboDataSourceChanged);
cboEdit.SelectedIndexChanged += new
EventHandler(this.EventComboTextChanged);

}

public DataGridComboBoxColumn(System.Data.DataView view) : this() {

Init(view);

}

public void Init(System.Data.DataView view) {

this.view = view;

cboEdit.DataSource = this.view;

if (view.Table.Columns.Count > 0) {

if ((view.Count) > 0) {

comparacao =
RHSoft.Data.Comparacoes.IComparable.ConstructBased On(view[0][0].GetType(),
null);

}

else {

comparacao = new RHSoft.Data.Comparacoes.ComparableString(null);

}

//comparacao = (view.Count) > 0 ?
RHSoft.Data.Comparacoes.IComparable.ConstructBased On(view[0][0].GetType(),
null) : new RHSoft.Data.Comparacoes.ComparableString(null);

cboEdit.ValueMember = view.Table.Columns[0].ColumnName;

cboEdit.DisplayMember = view.Table.Columns[0].ColumnName;

}

}

protected override void ConcedeFocus() {

cboEdit.Hide();

}

protected override void Abort(int rowNum) {

}

// initiates a request to complete an editing procedure.

protected override bool Commit(System.Windows.Forms.CurrencyManager source, int rowNum) {

cboEdit.Hide();

return true;

}

// Prepares the cell for editing a value.

protected override void Edit(System.Windows.Forms.CurrencyManager source,
int rowNum, Rectangle bounds, bool readOnly) {

}

// Prepares the cell for editing using the specified CurrencyManager, row
number, and Rectangle parameters.

protected override void Edit(System.Windows.Forms.CurrencyManager source,
int rowNum, Rectangle bounds, bool readOnly, string instantText) {

}

protected override void Edit(System.Windows.Forms.CurrencyManager source,
int rowNum, Rectangle bounds, bool readOnly, string instantText, bool
cellIsVisible) {

if (!cellIsVisible) {

cboEdit.Hide();

return;

}

try {

object valor = this.GetColumnValueAtRow(source, rowNum);

string strValue = valor.ToString();

blnLockEvent = true;

cboEdit.Enabled = !readOnly;

cboEdit.SetBounds(bounds.X, bounds.Y, bounds.Width, bounds.Height);

cboEdit.Show();

cboEdit.Focus();

cboEdit.SelectedValue = strValue;

if (cboEdit.SelectedIndex >= 0 && cboEdit.SelectedValue.ToString() !=
strValue) {

cboEdit.SelectedIndex = -1;

cboEdit.SelectedItem = null;

}

}

catch(Exception ex) {

Console.WriteLine(ex.Message);

}

finally {

blnLockEvent = false;

}

}

protected override int GetMinimumHeight() {

return 20;

}

protected override int GetPreferredHeight(Graphics g, object value) {

return this.DataGridTableStyle.PreferredRowHeight;

}

protected override Size GetPreferredSize(Graphics g, object value) {

return new Size(this.DataGridTableStyle.PreferredColumnWidth,
this.DataGridTableStyle.PreferredRowHeight);

}

protected override void Paint(Graphics g, Rectangle bounds, CurrencyManager source, int rowNum, Brush backBrush, Brush foreBrush, bool alignToRight) {

try {

g.FillRectangle(backBrush, bounds);

object data = this.GetColumnValueAtRow(source, rowNum);

if (data != System.DBNull.Value) {

if (cboEdit.ValueMember != cboEdit.DisplayMember) {

this.comparacao.Value = data;

int selectedIndex = RHSoft.Data.DataView.FindFirst(view, comparacao,
cboEdit.ValueMember);

if (selectedIndex >= 0)

data = view[selectedIndex][cboEdit.DisplayMember];

}

g.DrawString(data.ToString(), this.DataGridTableStyle.DataGrid.Font,
foreBrush, bounds.X, bounds.Y+2);

}

if (rowNum == this.DataGridTableStyle.DataGrid.CurrentRowIndex &&
this.DataGridTableStyle.GridColumnStyles.IndexOf(t his) ==
this.DataGridTableStyle.DataGrid.CurrentCell.Colum nNumber) {

g.DrawRectangle(SystemPens.ControlDark, bounds.Left, bounds.Top,
bounds.Width-1, bounds.Height-1);

}

}

catch(Exception ex) {

g.DrawString(ex.Message, this.DataGridTableStyle.DataGrid.Font, foreBrush,
bounds.X, bounds.Y+2);

}

}

public bool IsOpen() {

return cboEdit.DroppedDown;

}

protected override void Paint(Graphics g, Rectangle bounds,
System.Windows.Forms.CurrencyManager source, int rowNum, bool alignToRight) {

Paint(g, bounds, source, rowNum, new System.Drawing.SolidBrush(((rowNum % 2)
0) ? this.DataGridTableStyle.AlternatingBackColor :

this.DataGridTableStyle.BackColor),
System.Drawing.SystemBrushes.ControlText, alignToRight);

}

protected override void Paint(Graphics g, Rectangle bounds,
System.Windows.Forms.CurrencyManager source, int rowNum) {

Paint(g, bounds, source, rowNum, false);

}

protected override void SetDataGridInColumn(System.Windows.Forms.DataGrid
dataGrid) {

if (this.DataGridTableStyle != null &&
!this.DataGridTableStyle.DataGrid.Controls.Contain s(cboEdit))

this.DataGridTableStyle.DataGrid.Controls.Add(cboE dit);

}

private void EventComboTextChanged(object sender, EventArgs e) {

if (blnLockEvent || !cboEdit.Focused ||
this.DataGridTableStyle.DataGrid.CurrentRowIndex < 0) {

return;

}

try {

if (cboEdit.SelectedItem == null) {

this.SetColumnValueAtRow((System.Windows.Forms.Cur rencyManager)this.DataGrid TableStyle.DataGrid.BindingContext[this.DataGridTableStyle.DataGrid.DataSour ce], this.DataGridTableStyle.DataGrid.CurrentRowIndex, System.DBNull.Value);
}

else {

this.SetColumnValueAtRow((System.Windows.Forms.Cur rencyManager)this.DataGrid TableStyle.DataGrid.BindingContext[this.DataGridTableStyle.DataGrid.DataSour ce], this.DataGridTableStyle.DataGrid.CurrentRowIndex,
cboEdit.SelectedValue);

}

bool dropped = this.cboEdit.DroppedDown;

object selectedItem = this.cboEdit.SelectedItem;

this.ColumnStartedEditing(cboEdit);

if (dropped && !this.cboEdit.DroppedDown) {

this.cboEdit.SelectedItem = selectedItem;

this.cboEdit.DroppedDown = true;

}

}

catch(Exception ex) {

Console.WriteLine(ex.Message);

}

}

private void EventComboDataSourceChanged(object sender, EventArgs e) {

if (cboEdit.DataSource is System.Data.DataTable) {

this.view = ((System.Data.DataTable)cboEdit.DataSource).Defaul tView;

}

else {

this.view = (System.Data.DataView)cboEdit.DataSource;

}

if (view.Table.Columns.Count > 0) {

comparacao = (view.Count) > 0 ?
RHSoft.Data.Comparacoes.IComparable.ConstructBased On(view[0][0].GetType(),
null) : new RHSoft.Data.Comparacoes.ComparableString(null);

}

}

public System.Windows.Forms.ComboBox ComboBox {

get {

return cboEdit;

}

}

protected override void Dispose(bool disposing) {

base.Dispose (disposing);

cboEdit.DataSourceChanged -= new EventHandler(EventComboDataSourceChanged);
cboEdit.SelectedIndexChanged -= new
EventHandler(this.EventComboTextChanged);

if (view != null) {

view.Dispose();

}

if (disposing) {

cboEdit.Dispose();

cboEdit = null;

view = null;

comparacao = null;

}

}

}

}

Nov 15 '05 #3
Sérgio

Thanks for the code. Unfortunately, your class uses classes
that I don't have, (i.e. RHSoft...).

I've made some progress using the example at the link in my
original post but have discovered other problems as well.

thanks,

Bill
--
"Sérgio" <se*****@netvisao.pt> wrote in message news:<#3**************@TK2MSFTNGP09.phx.gbl>...
Hi.
I already invest a lot o time in doing a such thing for me. You have to
derive a new class from System.Windows.Forms.DataGridComboBoxColumn. Try
this code:

using System;
using System.Drawing;

using System.Windows.Forms;

namespace RHSoft.UI

{

public class DataGridComboBoxColumn :
System.Windows.Forms.DataGridColumnStyle {

private System.Windows.Forms.ComboBox cboEdit;

private RHSoft.Data.Comparacoes.IComparable comparacao = null;

private System.Data.DataView view = null;

private bool blnLockEvent = false;

public DataGridComboBoxColumn() {

cboEdit = new System.Windows.Forms.ComboBox();

cboEdit.Visible = cboEdit.TabStop = false;

cboEdit.DropDownStyle = ComboBoxStyle.DropDownList;

cboEdit.DataSourceChanged += new EventHandler(EventComboDataSourceChanged);

cboEdit.SelectedIndexChanged += new
EventHandler(this.EventComboTextChanged);

}

public DataGridComboBoxColumn(System.Data.DataView view) : this() {

Init(view);

}

public void Init(System.Data.DataView view) {

this.view = view;

cboEdit.DataSource = this.view;

if (view.Table.Columns.Count > 0) {

if ((view.Count) > 0) {

comparacao =
RHSoft.Data.Comparacoes.IComparable.ConstructBased On(view[0][0].GetType(),
null);

}

else {

comparacao = new RHSoft.Data.Comparacoes.ComparableString(null);

}

//comparacao = (view.Count) > 0 ?
RHSoft.Data.Comparacoes.IComparable.ConstructBased On(view[0][0].GetType(),
null) : new RHSoft.Data.Comparacoes.ComparableString(null);

cboEdit.ValueMember = view.Table.Columns[0].ColumnName;

cboEdit.DisplayMember = view.Table.Columns[0].ColumnName;

}

}

protected override void ConcedeFocus() {

cboEdit.Hide();

}

protected override void Abort(int rowNum) {

}

// initiates a request to complete an editing procedure.

protected override bool Commit(System.Windows.Forms.CurrencyManager source,
int rowNum) {

cboEdit.Hide();

return true;

}

// Prepares the cell for editing a value.

protected override void Edit(System.Windows.Forms.CurrencyManager source,
int rowNum, Rectangle bounds, bool readOnly) {

}

// Prepares the cell for editing using the specified CurrencyManager, row
number, and Rectangle parameters.

protected override void Edit(System.Windows.Forms.CurrencyManager source,
int rowNum, Rectangle bounds, bool readOnly, string instantText) {

}

protected override void Edit(System.Windows.Forms.CurrencyManager source,
int rowNum, Rectangle bounds, bool readOnly, string instantText, bool
cellIsVisible) {

if (!cellIsVisible) {

cboEdit.Hide();

return;

}

try {

object valor = this.GetColumnValueAtRow(source, rowNum);

string strValue = valor.ToString();

blnLockEvent = true;

cboEdit.Enabled = !readOnly;

cboEdit.SetBounds(bounds.X, bounds.Y, bounds.Width, bounds.Height);

cboEdit.Show();

cboEdit.Focus();

cboEdit.SelectedValue = strValue;

if (cboEdit.SelectedIndex >= 0 && cboEdit.SelectedValue.ToString() !=
strValue) {

cboEdit.SelectedIndex = -1;

cboEdit.SelectedItem = null;

}

}

catch(Exception ex) {

Console.WriteLine(ex.Message);

}

finally {

blnLockEvent = false;

}

}

protected override int GetMinimumHeight() {

return 20;

}

protected override int GetPreferredHeight(Graphics g, object value) {

return this.DataGridTableStyle.PreferredRowHeight;

}

protected override Size GetPreferredSize(Graphics g, object value) {

return new Size(this.DataGridTableStyle.PreferredColumnWidth,
this.DataGridTableStyle.PreferredRowHeight);

}

protected override void Paint(Graphics g, Rectangle bounds, CurrencyManager
source, int rowNum, Brush backBrush, Brush foreBrush, bool alignToRight) {

try {

g.FillRectangle(backBrush, bounds);

object data = this.GetColumnValueAtRow(source, rowNum);

if (data != System.DBNull.Value) {

if (cboEdit.ValueMember != cboEdit.DisplayMember) {

this.comparacao.Value = data;

int selectedIndex = RHSoft.Data.DataView.FindFirst(view, comparacao,
cboEdit.ValueMember);

if (selectedIndex >= 0)

data = view[selectedIndex][cboEdit.DisplayMember];

}

g.DrawString(data.ToString(), this.DataGridTableStyle.DataGrid.Font,
foreBrush, bounds.X, bounds.Y+2);

}

if (rowNum == this.DataGridTableStyle.DataGrid.CurrentRowIndex &&
this.DataGridTableStyle.GridColumnStyles.IndexOf(t his) ==
this.DataGridTableStyle.DataGrid.CurrentCell.Colum nNumber) {

g.DrawRectangle(SystemPens.ControlDark, bounds.Left, bounds.Top,
bounds.Width-1, bounds.Height-1);

}

}

catch(Exception ex) {

g.DrawString(ex.Message, this.DataGridTableStyle.DataGrid.Font, foreBrush,
bounds.X, bounds.Y+2);

}

}

public bool IsOpen() {

return cboEdit.DroppedDown;

}

protected override void Paint(Graphics g, Rectangle bounds,
System.Windows.Forms.CurrencyManager source, int rowNum, bool alignToRight)
{

Paint(g, bounds, source, rowNum, new System.Drawing.SolidBrush(((rowNum % 2)
0) ? this.DataGridTableStyle.AlternatingBackColor :

this.DataGridTableStyle.BackColor),
System.Drawing.SystemBrushes.ControlText, alignToRight);

}

protected override void Paint(Graphics g, Rectangle bounds,
System.Windows.Forms.CurrencyManager source, int rowNum) {

Paint(g, bounds, source, rowNum, false);

}

protected override void SetDataGridInColumn(System.Windows.Forms.DataGrid
dataGrid) {

if (this.DataGridTableStyle != null &&
!this.DataGridTableStyle.DataGrid.Controls.Contain s(cboEdit))

this.DataGridTableStyle.DataGrid.Controls.Add(cboE dit);

}

private void EventComboTextChanged(object sender, EventArgs e) {

if (blnLockEvent || !cboEdit.Focused ||
this.DataGridTableStyle.DataGrid.CurrentRowIndex < 0) {

return;

}

try {

if (cboEdit.SelectedItem == null) {

this.SetColumnValueAtRow((System.Windows.Forms.Cur rencyManager)this.DataGrid
TableStyle.DataGrid.BindingContext[this.DataGridTableStyle.DataGrid.DataSour
ce], this.DataGridTableStyle.DataGrid.CurrentRowIndex, System.DBNull.Value);

}

else {

this.SetColumnValueAtRow((System.Windows.Forms.Cur rencyManager)this.DataGrid
TableStyle.DataGrid.BindingContext[this.DataGridTableStyle.DataGrid.DataSour
ce], this.DataGridTableStyle.DataGrid.CurrentRowIndex,
cboEdit.SelectedValue);

}

bool dropped = this.cboEdit.DroppedDown;

object selectedItem = this.cboEdit.SelectedItem;

this.ColumnStartedEditing(cboEdit);

if (dropped && !this.cboEdit.DroppedDown) {

this.cboEdit.SelectedItem = selectedItem;

this.cboEdit.DroppedDown = true;

}

}

catch(Exception ex) {

Console.WriteLine(ex.Message);

}

}

private void EventComboDataSourceChanged(object sender, EventArgs e) {

if (cboEdit.DataSource is System.Data.DataTable) {

this.view = ((System.Data.DataTable)cboEdit.DataSource).Defaul tView;

}

else {

this.view = (System.Data.DataView)cboEdit.DataSource;

}

if (view.Table.Columns.Count > 0) {

comparacao = (view.Count) > 0 ?
RHSoft.Data.Comparacoes.IComparable.ConstructBased On(view[0][0].GetType(),
null) : new RHSoft.Data.Comparacoes.ComparableString(null);

}

}

public System.Windows.Forms.ComboBox ComboBox {

get {

return cboEdit;

}

}

protected override void Dispose(bool disposing) {

base.Dispose (disposing);

cboEdit.DataSourceChanged -= new EventHandler(EventComboDataSourceChanged);

cboEdit.SelectedIndexChanged -= new
EventHandler(this.EventComboTextChanged);

if (view != null) {

view.Dispose();

}

if (disposing) {

cboEdit.Dispose();

cboEdit = null;

view = null;

comparacao = null;

}

}

}

}

Nov 15 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Bill C. | last post by:
Hi, I'm trying to implement a ComboBox drop-down column for a DataGrid. When a cell is selected in the ComboBox column I overlay a ComboBox over the cell and call: this.comboBox.Show();...
2
by: Jax | last post by:
Is it possible to place a comboBox/DropDownList inside a datagrid? I notice there are textbox column and bool columns anyone know about comboBoxes? Thanks go to anyone who is kind enough to...
2
by: john sutor | last post by:
Does anyone know how to create a combobox in a standard datagrid? I can create check boxes , but not the combobox
0
by: RFickling | last post by:
Hi, I have a DataGrid with custom ComboBox and DataTimePicker controls in column styles. When I add a new row, typing values into all columns, then close the form, I get a...
0
by: metamedia | last post by:
How do I get a datagrid to register a data change from a custom control cell (combobox) I've got a Windows Application with a Windows Form Datagrid that has a custom combobox column. When the user...
3
by: PeterZ | last post by:
G'day, After doing much searching and pinching bits of ideas from here there and everywhere I came up with a fairly 'clean' solution of including a comboBox into a dataGrid column. You can...
3
by: TT (Tom Tempelaere) | last post by:
Hay there, I'm writing my own DataGridComboBoxColumn because .NET 1.1 does not have one (I hope .NET 2.0 supplies one). I based it on this article:...
6
by: Doug Bell | last post by:
Hi I have a datagrid with a combo box, I need to populate the combo with data dependant on the record value. eg for record 1, field Warehouse = 2R so combo would allow selection of locations...
0
by: simchajoy2000 | last post by:
Hi, I am trying to design a custom datagrid column that will allow me to put a combobox of colors in it. Well actually I can get that far but I can't figure out a way to save the color in the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.