在数据网格中插入组合框,每行中都有特定项 [英] Inserting ComboBoxes in a DataGrid with specific items in each row

查看:77
本文介绍了在数据网格中插入组合框,每行中都有特定项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 

Hi,

我需要知道如何填充一个包含诸如ComboBox之类的控件的DataGrid,而不要使用:

I need to know How can I fill a DataGrid , that contains controls like ComboBox , row by row instead of using :

tableStyle.GridColumnStyles.Add(...);

tableStyle.GridColumnStyles.Add(...);

因为我需要用不同的项目填充每一行的comboBox控件.

Since I need to fill the comboBox control of each row with different items.

我已经写了下面的代码,但是结果是该列中的空单元格应该是ComboBoxes !!.

I have wrote the follwing code but the result is empty cells in the column that expected to be ComboBoxes !!.

//***************************

//***************************

DataSet myDataSet = 数据集 ();

DataSet myDataSet = new DataSet();

//带有comboBox的标签表

//Tags Table with comboBox

DataTable tagsTableComboBox = myDataSet.Tables.Add( 带有组合框的标签表" );

DataTable tagsTableComboBox = myDataSet.Tables.Add("Tags Table with ComboBox");

DataColumn tagID_cb = tagsTableComboBox.Columns.Add( "quota_id" typeof ( Int32 ));

DataColumn tagID_cb = tagsTableComboBox.Columns.Add("tag_id", typeof(Int32));

DataColumn tag_cb = tagsTableComboBox.Columns.Add( " Tag" typeof ( 字符串 ));

DataColumn tag_cb = tagsTableComboBox.Columns.Add("Tag", typeof(string));

DataColumn similars_cb = tagsTableComboBox.Columns.Add( 相似" typeof ( 组合框 ));

DataColumn similars_cb = tagsTableComboBox.Columns.Add("Similars", typeof(ComboBox));

DataGridViewComboBoxColumn c =( DataGridViewComboBoxColumn )resultsDataGridView.Columns [0];

DataGridViewComboBoxColumn c = (DataGridViewComboBoxColumn)resultsDataGridView.Columns[0];

组合框 combobox1 = 组合框 ();

ComboBox combobox1 = new ComboBox();

( int i = 0; i< tpp.tagsTable.Count; i ++)

for (int i = 0; i < tpp.tagsTable.Count; i++)

{

标签 x =( Tag )(tpp.tagsTable);

Tag x = (Tag)(tpp.tagsTable);

combobox1.DataSource =(x.similars).ToArray();

combobox1.DataSource = (x.similars).ToArray();

DataRow myDataRow = tagsTableComboBox.Rows.Add(i,(( string )x.similars [0],combobox1);

DataRow myDataRow = tagsTableComboBox.Rows.Add(i, (string)x.similars[0],combobox1);

}

CurrencyManager cm =( CurrencyManager )resultsDataGridView.BindingContext [myDataSet ];

CurrencyManager cm = (CurrencyManager)resultsDataGridView.BindingContext[myDataSet];

resultsDataGridView.DataSource = myDataSet;

resultsDataGridView.DataMember = 带有组合框的标签表" ;

resultsDataGridView.DataMember = "Tags Table with ComboBox";

//******************************************* *

注意:

Note:

我做过另一次尝试:

1-使用DataGridView控件

1-将DataGridView控件插入到我的表单中

1- Inserting a DataGridView control into my form

2-使用DataGridView任务对话框->添加列-> DataGridViewComboBox类型,

2-using DataGridView Tasks Dialog --> Add column --> DataGridViewComboBox Type ,

我已经在网格中添加了一个comboBox列.

I have added a comboBox column to the grid.

现在我需要用与该行相关的特定值填充每一行中的每个comboBox,我的问题是该列所有行中的所有comboBox都填充有一个值.

Now I need to fill each comboBox in each Row with specific value related to that row, my problem is that all comboBoxes in all rows in that column are filled with one value.

遵循代码片段.

//********************************* ************

//带有comboBox的标签表

//Tags Table with comboBox

DataTable tagsTableComboBox = myDataSet.Tables.Add( 带有CpmboBox的标签表" );

DataTable tagsTableComboBox = myDataSet.Tables.Add("Tags Table with CpmboBox");

DataColumn tagID_cb = tagsTableComboBox.Columns.Add( "quota_id" typeof ( Int32 ));

DataColumn tagID_cb = tagsTableComboBox.Columns.Add("tag_id", typeof(Int32));

DataColumn tag_cb = tagsTableComboBox.Columns.Add( " Tag" typeof ( 字符串 ));

DataColumn tag_cb = tagsTableComboBox.Columns.Add("Tag", typeof(string));

DataGridViewComboBoxColumn c =( DataGridViewComboBoxColumn )resultsDataGridView.Columns [0];

DataGridViewComboBoxColumn c = (DataGridViewComboBoxColumn)resultsDataGridView.Columns[0];

( int i = 0; i< tpp.tagsTable.Count; i ++)

for (int i = 0; i < tpp.tagsTable.Count; i++)

{

标签 x =( Tag )(tpp.tagsTable);

Tag x = (Tag)(tpp.tagsTable);

c.DataSource =(x.similars).ToArray();

c.DataSource = (x.similars).ToArray();

DataRow myDataRow = tagsTableComboBox.Rows.Add(i,(( string )x.similars [0]);

DataRow myDataRow = tagsTableComboBox.Rows.Add(i, (string)x.similars[0]);

}

CurrencyManager cm =( CurrencyManager )resultsDataGridView.BindingContext [myDataSet];

CurrencyManager cm = (CurrencyManager)resultsDataGridView.BindingContext[myDataSet];

resultsDataGridView.DataSource = myDataSet;

resultsDataGridView.DataSource = myDataSet;

resultsDataGridView.DataMember = 带有CpmboBox的标签表"; ;

resultsDataGridView.DataMember = "Tags Table with CpmboBox";

//********************************************* *******************

//****************************************************************

2-在DataGrid中使用表格样式

//**************

//**************

DataGridTableStyle tableStyle = DataGridTableStyle ();

DataGridTableStyle tableStyle = new DataGridTableStyle();

DataGridTextBoxColumn columnTextBox;

DataGridTextBoxColumn columnTextBox;

X. DataGridComboBoxColumn columnComboBox;

X.DataGridComboBoxColumn columnComboBox;

DataGridViewComboBoxColumn cv;

DataGridViewComboBoxColumn cv ;

columnTextBox = DataGridTextBoxColumn ();

columnTextBox = new DataGridTextBoxColumn();

columnTextBox.MappingName = tagsTableComboBox.Columns [0] .ColumnName;

columnTextBox.MappingName = tagsTableComboBox.Columns[0].ColumnName;

columnTextBox.HeaderText = tagsTableComboBox.Columns [0] .Caption;

columnTextBox.HeaderText = tagsTableComboBox.Columns[0].Caption;

columnTextBox.Width = 80;

columnTextBox.Width = 80;

columnTextBox.Alignment = Horizo​​ntalAlignment .Left;

columnTextBox.Alignment = HorizontalAlignment.Left;

columnTextBox.ReadOnly = true ;

columnTextBox.ReadOnly = true;

tableStyle.GridColumnStyles.Add(columnTextBox);

tableStyle.GridColumnStyles.Add(columnTextBox);

columnTextBox = DataGridTextBoxColumn ();

columnTextBox = new DataGridTextBoxColumn();

columnTextBox.MappingName = tagsTableComboBox.Columns [1] .ColumnName;

columnTextBox.MappingName = tagsTableComboBox.Columns[1].ColumnName;

columnTextBox.HeaderText = tagsTableComboBox.Columns [1] .Caption;

columnTextBox.HeaderText = tagsTableComboBox.Columns[1].Caption;

columnTextBox.Width = 80;

columnTextBox.Width = 80;

columnTextBox.Alignment = Horizo​​ntalAlignment .Right;

columnTextBox.Alignment = HorizontalAlignment.Right;

columnTextBox.ReadOnly = true ;

columnTextBox.ReadOnly = true;

tableStyle.GridColumnStyles.Add(columnTextBox);

tableStyle.GridColumnStyles.Add(columnTextBox);

columnComboBox = X. DataGridComboBoxColumn ();

columnComboBox = new X.DataGridComboBoxColumn();

columnComboBox.MappingName = tagsTableComboBox.Columns [2] .ColumnName;

columnComboBox.MappingName = tagsTableComboBox.Columns[2].ColumnName;

columnComboBox.HeaderText = tagsTableComboBox.Columns [2] .Caption;

columnComboBox.HeaderText = tagsTableComboBox.Columns[2].Caption;

columnComboBox.Width = 80;

columnComboBox.Width = 80;

columnComboBox.Alignment = Horizo​​ntalAlignment .对;

columnComboBox.Alignment = HorizontalAlignment.Right;

columnComboBox.ReadOnly = true ;

columnComboBox.ReadOnly = true;

tableStyle.GridColumnStyles.Add(columnComboBox);

tableStyle.GridColumnStyles.Add(columnComboBox);

tableStyle.MappingName = myDataSet.Tables [2] .TableName;

tableStyle.MappingName = myDataSet.Tables[2].TableName;

tableStyle.AlternatingBackColor = 颜色 .Gainsboro;

tableStyle.AlternatingBackColor = Color.Gainsboro;

tableStyle.AllowSorting = ;

tableStyle.AllowSorting = false;

tableStyle.ReadOnly = true ;

tableStyle.ReadOnly = true;

dataGrid1.TableStyles.Clear();

dataGrid1.TableStyles.Clear();

dataGrid1.TableStyles.Add(tableStyle);

dataGrid1.TableStyles.Add(tableStyle);

dataGrid1.DataSource = myDataSet;

dataGrid1.DataSource = myDataSet;

dataGrid1.DataMember = 带有组合框的标签表"; ;

dataGrid1.DataMember = "Tags Table with ComboBox";

tableStyle.MappingName = dataGrid1.DataSource.GetType().Name;

tableStyle.MappingName = dataGrid1.DataSource.GetType().Name;

//*************

//*************

其中X名称空间具有 DataGridComboBoxColumn 类的定义,如下所示:

where X namespace has the definition of the DataGridComboBoxColumn  class as follows:

//----------------

//----------------

使用 系统;

using System;

使用 System.Drawing;

using System.Drawing;

使用 System.Collections;

using System.Collections;

使用 System.ComponentModel;

using System.ComponentModel;

使用 System.Windows.Forms;

using System.Windows.Forms;

使用 System.Data;

using System.Data;

命名空间 X

namespace X

{

#region DataGridComboBoxColumn

#region DataGridComboBoxColumn

//********************************* ****************************************************** ***************

//**********************************************************************************************

//DataGridComboBoxColumn

// DataGridComboBoxColumn

//********************************* ****************************************************** ***************

//**********************************************************************************************

公共 DataGridComboBoxColumn : DataGridColumnStyle

public class DataGridComboBoxColumn : DataGridColumnStyle

{

私有 DataGridComboBox 组合框;

private DataGridComboBox combobox;

私有 布尔型 编辑;

private bool edit;

//----------------------------- -------------------------------------------------- ------------

//-------------------------------------------------------------------------------------------

//构造函数和析构函数

// Constructors and destructors

//----------------------------- -------------------------------------------------- ------------

//-------------------------------------------------------------------------------------------

公共 DataGridComboBoxColumn()

public DataGridComboBoxColumn()

{

combobox = DataGridComboBox ();

combobox = new DataGridComboBox();

combobox.Visible = ;

combobox.Visible = false;

combobox.DropDownStyle = ComboBoxStyle .DropDownList;

combobox.DropDownStyle = ComboBoxStyle.DropDownList;

combobox.保留+ = EventHandler (ComboHide);

combobox.Leave += new EventHandler(ComboHide);

combobox.SelectionChangeCommitted + = EventHandler (ComboStartEditing);

combobox.SelectionChangeCommitted += new EventHandler(ComboStartEditing);

edit = ;

edit = false;

} //DataGridComboBoxColumn

} // DataGridComboBoxColumn

//----------------------------- -------------------------------------------------- ------------

//-------------------------------------------------------------------------------------------

//属性

// Properties

//----------------------------- -------------------------------------------------- ------------

//-------------------------------------------------------------------------------------------

公共 组合框 comboBox

public ComboBox comboBox

{

获取

get

{

返回 组合框;

return combobox;

}

} //comboBox

} // comboBox

//----------------------------- -------------------------------------------------- ------------

//-------------------------------------------------------------------------------------------

//ComboBox事件处理程序

// ComboBox event handlers

//----------------------------- -------------------------------------------------- ------------

//-------------------------------------------------------------------------------------------

私有 无效 ComboHide( 对象 发件人, EventArgs e)

private void ComboHide(object sender, EventArgs e)

{

//当ComboBox失去焦点时,只需将其隐藏即可.

// When the ComboBox looses focus, then simply hide it.

combobox.Hide();

combobox.Hide();

} //ComboHide

} // ComboHide

私有 无效 ComboStartEditing( 对象 发件人, EventArgs e)

private void ComboStartEditing(object sender, EventArgs e)

{

//进入编辑模式.

// Enter edit mode.

edit = true ;

edit = true;

base .ColumnStartedEditing((( 控制 )发送方);

base.ColumnStartedEditing((Control)sender);

} //ComboStartEditing

} // ComboStartEditing

//----------------------------- -------------------------------------------------- ------------

//-------------------------------------------------------------------------------------------

//覆盖DataGridColumnStyle

// Override DataGridColumnStyle

//----------------------------- -------------------------------------------------- ------------

//-------------------------------------------------------------------------------------------

受保护的 覆盖 无效 SetDataGridInColumn( DataGrid 值)

protected override void SetDataGridInColumn(DataGrid value)

{

//将ComboBox添加到DataGrids控件集合.

// Add the ComboBox to the DataGrids controls collection.

//这样可以确保正确地滚动DataGrid.

// This ensures correct DataGrid scrolling.

value.Controls.Add(combobox);

value.Controls.Add(combobox);

基本 .SetDataGridInColumn(value);

base.SetDataGridInColumn(value);

} //SetDataGridInColumn

} // SetDataGridInColumn

受保护的 覆盖 无效 中止( int rowNum)

protected override void Abort(int rowNum)

{

//放弃编辑模式,放弃更改并隐藏ComboBox.

// Abort edit mode, discard changes and hide the ComboBox.

edit = ;

edit = false;

Invalidate();

Invalidate();

combobox.Hide();

combobox.Hide();

} //中止

} // Abort

受保护的 覆盖 无效 编辑(System.Windows.Forms. CurrencyManager 源, int rowNum, System.Drawing. 矩形 边界, bool readOnly, 字符串 InstantText, bool cellIsVisible)

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

{

//设置组合框以进行操作.

// Setup the ComboBox for action.

//这包括放置组合框并显示它.

// This includes positioning the ComboBox and showing it.

//在显示组合框之前,还要选择正确的项目.

// Also select the correct item in the ComboBox before it is shown.

combobox.Parent = .DataGridTableStyle.DataGrid;

combobox.Parent = this.DataGridTableStyle.DataGrid;

combobox.Bounds =界限;

combobox.Bounds = bounds;

combobox.Size = 大小 ( this .Width, this .comboBox.Height);

combobox.Size = new Size(this.Width, this.comboBox.Height);

comboBox.SelectedValue = base .GetColumnValueAtRow(source,rowNum).ToString();

comboBox.SelectedValue = base.GetColumnValueAtRow(source, rowNum).ToString();

combobox.Visible =(cellIsVisible == true )&& (readOnly == false );

combobox.Visible = (cellIsVisible == true) && (readOnly == false);

combobox.BringToFront();

combobox.BringToFront();

combobox.Focus();

combobox.Focus();

} //编辑

} // Edit

受保护的 覆盖 布尔型 提交(System.Windows.Forms. CurrencyManager 源, int rowNum)

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

{

//将选定的值从ComboBox提交到DataGrid.

// Commit the selected value from the ComboBox to the DataGrid.

如果 (编辑== true )

if (edit == true)

{

edit = ;

edit = false;

this .SetColumnValueAtRow(source,rowNum,combobox.SelectedValue);

this.SetColumnValueAtRow(source, rowNum, combobox.SelectedValue);

}

返回 true ;

return true;

} //提交

} // Commit

受保护的 覆盖 对象 GetColumnValueAtRow(System.Windows.Forms. CurrencyManager 源, int rowNum)

protected override object GetColumnValueAtRow(System.Windows.Forms.CurrencyManager source, int rowNum)

{

//返回与数据关联的显示文本,由

// Return the display text associated with the data, insted of the

//来自DataGrid数据源的数据.

// data from the DataGrid datasource.

返回 combobox.GetDisplayText( 基本 .GetColumnValueAtRow(source,rowNum));

return combobox.GetDisplayText(base.GetColumnValueAtRow(source, rowNum));

} //GetColumnValueAtRow

} // GetColumnValueAtRow

受保护的 覆盖 无效 SetColumnValueAtRow( CurrencyManager 源, int rowNum, 对象 值)

protected override void SetColumnValueAtRow(CurrencyManager source, int rowNum, object value)

{

//将数据(值)保存到DataGrid数据源.

// Save the data (value) to the DataGrid datasource.

//我尝试了几种不同的类型,因为我经常将GUID用作键

// I try a few different types, because I often uses GUIDs as keys in my

//数据.

// data.

//字符串.

// String.

尝试

try

{

base .SetColumnValueAtRow(source,rowNum,value.ToString());

base.SetColumnValueAtRow(source, rowNum, value.ToString());

返回 ;

return;

}

捕获 {}

catch { }

//指导.

// Guid.

尝试

try

{

base .SetColumnValueAtRow(source,rowNum, 向导 (value.ToString()));

base.SetColumnValueAtRow(source, rowNum, new Guid(value.ToString()));

返回 ;

return;

}

捕获 {}

catch { }

//对象(默认).

// Object (default).

base .SetColumnValueAtRow(source,rowNum,value);

base.SetColumnValueAtRow(source, rowNum, value);

} //SetColumnValueAtRow

} // SetColumnValueAtRow

受保护的 覆盖 int GetMinimumHeight()

protected override int GetMinimumHeight()

{

//返回ComboBox的首选高度,再加上几个像素.

// Return the ComboBox preferred height, plus a few pixels.

返回 组合框.PreferredHeight+ 2;

return combobox.PreferredHeight + 2;

} //GetMinimumHeight

} // GetMinimumHeight

受保护的 覆盖 int GetPreferredHeight( 图形 g, 对象 val)

protected override int GetPreferredHeight(Graphics g, object val)

{

//返回字体高度,加上几个像素.

// Return the font height, plus a few pixels.

返回 FontHeight + 2;

return FontHeight + 2;

} //GetPreferredHeight

} // GetPreferredHeight

受保护的 覆盖 大小 GetPreferredSize( 图形 g, 对象 val)

protected override Size GetPreferredSize(Graphics g, object val)

{

//返回首选宽度.

// Return the preferred width.

//遍历下拉列表中的所有显示文本,并测量每个文本

// Iterate through all display texts in the dropdown, and measure each

//文本宽度.

// text width.

int 最宽= 0;

int widest = 0;

SizeF stringSize = new SizeF (0,0);

SizeF stringSize = new SizeF(0, 0);

foreach ( 字符串 文本 combobox.GetDisplayText())

foreach (string text in combobox.GetDisplayText())

{

stringSize = g.MeasureString(text, base .DataGridTableStyle.DataGrid.Font);

stringSize = g.MeasureString(text, base.DataGridTableStyle.DataGrid.Font);

如果 (stringSize.Width>最宽)

if (stringSize.Width > widest)

{

最宽=( int ) 数学 .Ceiling(stringSize.Width);

widest = (int)Math.Ceiling(stringSize.Width);

}

}

返回 大小 (最宽+ 25,combobox.PreferredHeight + 2);

return new Size(widest + 25, combobox.PreferredHeight + 2);

} //GetPreferredSize

} // GetPreferredSize

受保护的 覆盖 无效 绘画( 图形 g, 矩形 边界, CurrencyManager int rowNum)

protected override void Paint(Graphics g, Rectangle bounds, CurrencyManager source, int rowNum)

{

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

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

} //绘画

} // Paint

受保护的 覆盖 无效 绘画( 图形 g, 矩形 边界, CurrencyManager int rowNum, bool AlignToRight)

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

{

字符串 文本= GetColumnValueAtRow(source,rowNum).ToString();

string text = GetColumnValueAtRow(source, rowNum).ToString();

画笔 backBrush = SolidBrush ( 基本 .DataGridTableStyle.BackColor);

Brush backBrush = new SolidBrush(base.DataGridTableStyle.BackColor);

画笔 foreBrush = SolidBrush ( 基本 .DataGridTableStyle.ForeColor);

Brush foreBrush = new SolidBrush(base.DataGridTableStyle.ForeColor);

矩形 rect =界限;

Rectangle rect = bounds;

StringFormat format = StringFormat ();

StringFormat format = new StringFormat();

//处理可以选择的行.

// Handle that the row can be selected.

如果 ( 基本 .DataGridTableStyle.DataGrid.IsSelected(rowNum)== true )

if (base.DataGridTableStyle.DataGrid.IsSelected(rowNum) == true)

{

backBrush = SolidBrush ( base .DataGridTableStyle.SelectionBackColor);

backBrush = new SolidBrush(base.DataGridTableStyle.SelectionBackColor);

foreBrush = SolidBrush ( 基本 .DataGridTableStyle.SelectionForeColor);

foreBrush = new SolidBrush(base.DataGridTableStyle.SelectionForeColor);

}

//手柄向右对齐.

// Handle align to right.

如果 (alignToRight == true )

if (alignToRight == true)

{

format.FormatFlags = StringFormatFlags .DirectionRightToLeft;

format.FormatFlags = StringFormatFlags.DirectionRightToLeft;

}

//手柄对齐.

// Handle alignment.

开关 ( .Alignment)

switch (this.Alignment)

{

保护套 Horizo​​ntalAlignment > .左:

case HorizontalAlignment.Left:

format.Alignment = StringAlignment .Near;

format.Alignment = StringAlignment.Near;

break ;

break;

保护套 Horizo​​ntalAlignment > .右:

case HorizontalAlignment.Right:

format.Alignment = StringAlignment .Far;

format.Alignment = StringAlignment.Far;

break ;

break;

保护套 Horizo​​ntalAlignment > .Center:

case HorizontalAlignment.Center:

format.Alignment = StringAlignment .Center;

format.Alignment = StringAlignment.Center;

break ;

break;

}

//绘画.

// Paint.

format.FormatFlags = StringFormatFlags .NoWrap;

format.FormatFlags = StringFormatFlags.NoWrap;

g.FillRectangle(backBrush,rect);

g.FillRectangle(backBrush, rect);

rect.Offset(0,2);

rect.Offset(0, 2);

rect.Height-= 2;

rect.Height -= 2;

g.DrawString(text, this.DataGridTableStyle.DataGrid.Font, foreBrush, rect, format);

g.DrawString(text, this.DataGridTableStyle.DataGrid.Font, foreBrush, rect, format);

format.Dispose();

format.Dispose();

} //PaintText

} // PaintText

} //DataGridComboBoxColumn

} // DataGridComboBoxColumn

#endregion

#endregion

#region DataGridComboBox

#region DataGridComboBox

//**********************************************************************************************

//**********************************************************************************************

//DataGridComboBox

// DataGridComboBox

//**********************************************************************************************

//**********************************************************************************************

public class DataGridComboBox : ComboBox

public class DataGridComboBox : ComboBox

{

private const int WM_KEYUP = 0x101;

private const int WM_KEYUP = 0x101;

protected override void WndProc(ref System.Windows.Forms.Message message)

protected override void WndProc(ref System.Windows.Forms.Message message)

{

//Ignore keyup to avoid problem with tabbing and dropdown list.

// Ignore keyup to avoid problem with tabbing and dropdown list.

if (message.Msg == WM_KEYUP)

if (message.Msg == WM_KEYUP)

{

return;

return;

}

base.WndProc(ref message);

base.WndProc(ref message);

} //WndProc

} // WndProc

public string GetValueText(int index)

public string GetValueText(int index)

{

//Validate the index.

// Validate the index.

if ((index < 0) && (index >= base.Items.Count))

if ((index < 0) && (index >= base.Items.Count))

throw new IndexOutOfRangeException("Invalid index.");

throw new IndexOutOfRangeException("Invalid index.");

//Get the text.

// Get the text.

string text = string.Empty;

string text = string.Empty;

int memIndex = -1;

int memIndex = -1;

try

try

{

base.BeginUpdate();

base.BeginUpdate();

memIndex = base.SelectedIndex;

memIndex = base.SelectedIndex;

base.SelectedIndex = index;

base.SelectedIndex = index;

text = base.SelectedValue.ToString();

text = base.SelectedValue.ToString();

base.SelectedIndex = memIndex;

base.SelectedIndex = memIndex;

}

catch

catch

{

}

finally

finally

{

base.EndUpdate();

base.EndUpdate();

}

return text;

return text;

} //GetValueText

} // GetValueText

public string GetDisplayText(int index)

public string GetDisplayText(int index)

{

//Validate the index.

// Validate the index.

if ((index < 0) && (index >= base.Items.Count))

if ((index < 0) && (index >= base.Items.Count))

throw new IndexOutOfRangeException("Invalid index.");

throw new IndexOutOfRangeException("Invalid index.");

//Get the text.

// Get the text.

string text = string.Empty;

string text = string.Empty;

int memIndex = -1;

int memIndex = -1;

try

try

{

base.BeginUpdate();

base.BeginUpdate();

memIndex = base.SelectedIndex;

memIndex = base.SelectedIndex;

base.SelectedIndex = index;

base.SelectedIndex = index;

text = base.SelectedItem.ToString();

text = base.SelectedItem.ToString();

base.SelectedIndex = memIndex;

base.SelectedIndex = memIndex;

}

catch

catch

{

}

finally

finally

{

base.EndUpdate();

base.EndUpdate();

}

return text;

return text;

} //GetDisplayText

} // GetDisplayText

public string GetDisplayText(object value)

public string GetDisplayText(object value)

{

//Get the text.

// Get the text.

string text = string.Empty;

string text = string.Empty;

int memIndex = -1;

int memIndex = -1;

try

try

{

base.BeginUpdate();

base.BeginUpdate();

memIndex = base.SelectedIndex;

memIndex = base.SelectedIndex;

base.SelectedValue = value.ToString();

base.SelectedValue = value.ToString();

text = base.SelectedItem.ToString();

text = base.SelectedItem.ToString();

base.SelectedIndex = memIndex;

base.SelectedIndex = memIndex;

}

catch

catch

{

}

finally

finally

{

base.EndUpdate();

base.EndUpdate();

}

return text;

return text;

} //GetDisplayText

} // GetDisplayText

public string[] GetDisplayText()

public string[] GetDisplayText()

{

//Get the text.

// Get the text.

string[] text = new string[base.Items.Count];

string[] text = new string[base.Items.Count];

int memIndex = -1;

int memIndex = -1;

try

try

{

base.BeginUpdate();

base.BeginUpdate();

memIndex = base.SelectedIndex;

memIndex = base.SelectedIndex;

for (int index = 0; index < base.Items.Count; index++)

for (int index = 0; index < base.Items.Count; index++)

{

base.SelectedIndex = index;

base.SelectedIndex = index;

text[index] = base.SelectedItem.ToString();

text[index] = base.SelectedItem.ToString();

}

base.SelectedIndex = memIndex;

base.SelectedIndex = memIndex;

}

catch

catch

{

}

finally

finally

{

base.EndUpdate();

base.EndUpdate();

}

return text;

return text;

} //GetDisplayText

} // GetDisplayText

} //DataGridComboBox

} // DataGridComboBox

#endregion

}

//----------------

//----------------

also the result is empty cells in the column that expected to be ComboBoxes !!.

also the result is empty cells in the column that expected to be ComboBoxes !!.

So, How can I solve that .

So, How can I solve that .

Thanks in advance for any help,

Aya.

Aya.

 

推荐答案

Please see this article:

Please see this article:

http://www.codeproject.com/cs/miscctrl/RenDataGridComboBoxColumn.asp

欢呼

Smitha


这篇关于在数据网格中插入组合框,每行中都有特定项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆