Groups | Blog | Home
all groups > dotnet windows forms databinding > november 2005 >

dotnet windows forms databinding : combobox inside datagrid


Rick
11/24/2005 12:43:57 PM
hi group!!

my question is in a datagrid i have 3 columns, 2 columns are just text,
third is type combobox, can i load in that combobox values from other table
that is not the same the grid displays? (if true, how can i do it?)

Thanks!!

Bart Mermuys
11/25/2005 12:00:00 AM
Hi,

[quoted text, click to view]

Are you using DataGrid or DataGridView (NET2.0).
A DataGrid doesn't come with a DataGridComboBoxColumnStyle (but you can find
sourcecode on the net).
A DataGridView comes with a DataGridViewComboBoxColumn

In both cases it should be possible to assign data from another DataTable.
Set ComboBox(Column) DataSource/DataMember to another table and set
ValueMember to a column that has the same data as the master column.


HTH,
Greetings


[quoted text, click to view]


Cyr1dian
11/27/2005 12:00:00 AM
I don't know about .Net FW2.0, but if ur using an older version (as most=
=

of us still are) there's a plethora of solution out there... most of whi=
ch =

don't seem to work for most people (including myself). Here's one that =

worked for me - I DID NOT WRITE IT (kudos to the person who did, but I =

can't remember where I got it):

This solution let's you add a combobox that is filled by a datasource, i=
t =

is commonly used for foreign key fields where it is convenient to displa=
y =

text where number are actually inserted. I don't know what you exactly =

wish to achieve but this is can be adapted to suit most situations I thi=
nk.

There are 3 parts to this listed below in this order:
1) DataGridComboBoxColumn.cs
2) myComboBox.cs
3) implementation code


=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D
DataGridComboBoxColumn.cs
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D

using System;
using System.Data;
using System.Drawing;
using System.Windows.Forms;

namespace YourNameSpace
{
public class DataGridComboBoxColumn : DataGridTextBoxColumn
{
public myComboBox myComboBox;
private System.Windows.Forms.CurrencyManager _currencyManager;
private int _rowNum;
private bool _Editing;

// Constructor, create our own customized Combobox
public DataGridComboBoxColumn()
{
_currencyManager =3D null;
_Editing =3D false;

// Create our own customized Combobox, which is used in the DataGrid
// DropDownList: The user cannot directly edit the text portion.
// The user must click the arrow button to display the
// list portion.
// DropDown: The text portion is editable. The user must click
// the arrow button to display the list portion.
// Simple: The text portion is editable. The list portion is
// always visible.
myComboBox =3D new myComboBox();
myComboBox.DropDownStyle =3D ComboBoxStyle.DropDownList;

// My own Combobox subscribes to the Leave Event. It occurs when the
// input focus leaves the ComboBox.
this.myComboBox.Leave +=3D
new System.EventHandler(LeaveComboBox);

// My own Combobox subscribes to the SelectionChangeCommitted Event.
// It occurs when the selected item has changed and that change
// is committed (save the changed data to the DataGrid TextBox).
this.myComboBox.SelectionChangeCommitted +=3D
new System.EventHandler(SelectionChangeCommit);
}

// Make current Combobox invisible when user scrolls
// the DataGrid control using the ScrollBar.
private void HandleScroll(Object sender, EventArgs e)
{
if (myComboBox.Visible)
{
myComboBox.Hide();
}
}

// The ColumnStartedEditing method allows the DataGrid
// to show a pencil in the row header indicating the row
// is being edited. (base is the parent DataGridTextBoxColumn)
private void SelectionChangeCommit(Object sender, EventArgs e)
{
_Editing =3D true;
base.ColumnStartedEditing((System.Windows.Forms.Control) sender);
}

// Handle Combobox Behaviour when Focus leaves the Combobox.
private void LeaveComboBox(Object sender, EventArgs e)
{
if (_Editing)
{
// Set the Combobox ValueMember to the current RowColumn
// when the Focus leaves the Combobox.
SetColumnValueAtRow(_currencyManager, _rowNum, myComboBox.Text);
_Editing =3D false;

// Redraws the column
Invalidate();
}
// Hide the current Combobox when Focus on Combobox is loosen
myComboBox.Hide();

// Let current Combobox visible when user scrolls
// the DataGrid control using the ScrollBar.
this.DataGridTableStyle.DataGrid.Scroll +=3D new =

System.EventHandler(HandleScroll);
}

// The SetColumnValueAtRow method updates the bound
// DataTable "Titles" with the ValueMember
// for a given DisplayMember =3D myComboBox.Text from the Combobox.
protected override void SetColumnValueAtRow
(CurrencyManager source, int rowNum, Object value)
{
Object tbDisplay =3D value;
DataView dv =3D (DataView)this.myComboBox.DataSource;
int rowCount =3D dv.Count;
int i =3D 0;
Object cbDisplay;
Object cbValue;

// Loop through the Combobox DisplayMember values
// until you find the selected value, then read the
// ValueMember from the Combobox and update it in the
// DataTable "Titles"
while (i < rowCount)
{
cbDisplay =3D dv[i][this.myComboBox.DisplayMember];

if ((cbDisplay !=3D DBNull.Value) &&
(tbDisplay.Equals(cbDisplay)))
{
break;
}
i +=3D 1;
}
if (i < rowCount)
{
cbValue =3D dv[i][this.myComboBox.ValueMember];
}
else
{
cbValue =3D DBNull.Value;
}
base.SetColumnValueAtRow(source, rowNum, cbValue);
}

// The GetColumnValueAtRow method updates the bound
// Combobox with the DisplayMember
// for a given Row Number =3D rowNum from the DataTable "Titles".
protected override Object GetColumnValueAtRow
(CurrencyManager source, int rowNum)
{
// Get the ValueMember from the DataTable "Titles"
Object tbValue =3D base.GetColumnValueAtRow(source, rowNum);

// Associate a DataView to the Combox, so we can search
// the DisplayMember in the Combox corresponding to the
// ValueMember from the DataTable "Titles"
DataView dv =3D (DataView)this.myComboBox.DataSource;
int rowCount =3D dv.Count;
int i =3D 0;
Object cbValue;

// Loop through the Combox Entries and search the DisplayMember
while (i < rowCount)
{
cbValue =3D dv[i][this.myComboBox.ValueMember];
if ((cbValue !=3D DBNull.Value) &&
(tbValue !=3D DBNull.Value) &&
(tbValue.Equals(cbValue)))
{
break; // We found the DisplayMember - exit the Loop
}
i +=3D 1;
}

// If we are within the Combox Entries, return now the DisplayMember
// for the found ValueMember above. If we are at the End of the Combo=
x
// Entries, return NULL
if (i < rowCount)
{
return dv[i][this.myComboBox.DisplayMember];
}
else
{
return DBNull.Value;
}
}

// The Edit event is raised when the user sets the focus to the cell
// containing the combobox. In this event the dimensions of the combob=
ox
// are set and an event handler is assigned to handle scrolling of the=
=

combobox.
protected override void Edit(
CurrencyManager source,
int rowNum,
Rectangle bounds,
bool readOnly,
string instantText,
bool cellIsVisible)
{
base.Edit(source, rowNum, bounds, readOnly, instantText, cellIsVisibl=
e);

// Set current Rownum and Postion Manager
_rowNum =3D rowNum;
_currencyManager =3D source;

// Calculate Location of the Combox relative to the TextBox
Rick
11/28/2005 12:25:52 PM
Thanks a lot!!!
it works!!!

=)


"Rick" <zatankloz@gmail.com> escribió en el mensaje
news:exioGcS8FHA.3544@TK2MSFTNGP09.phx.gbl...
[quoted text, click to view]

AddThis Social Bookmark Button