最近有个难题,比较棘手,是关于datagrid的每一行绑定的combobox的下拉列表有可能会不一样,这该如何搞定 vs2003(winform)

skylineforever 2006-06-28 09:26:41
DataGridComboBoxColumn类

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

namespace Microsoft.Msdn.Article.DataGridColumnStyles.DownloadManager {

public class DataGridComboBoxColumn : DataGridColumnStyle {

private ComboBox m_comboBox;
private int m_previouslyEditedCellRow;
private DataGridColumnStylePadding m_padding;
private string displayMember,valueMember;

public DataGridComboBoxColumn(ComboBox m_comboBox,string displayMember,string valueMember) : base() {

this.m_comboBox = m_comboBox;
this.displayMember = displayMember;
this.valueMember = valueMember;
//m_comboBox = new ComboBox();
this.m_comboBox.DropDownStyle = ComboBoxStyle.DropDownList;
this.m_comboBox.Visible = false;
this.m_comboBox.SizeChanged += new EventHandler( ComboBox_SizeChanged );

this.ControlSize = m_comboBox.Size;
this.Padding = new DataGridColumnStylePadding( 0, 0, 0, 0 );
this.Width = this.GetPreferredSize( null, null ).Width;

//this.m_comboBox.SelectedIndex = 0;

}

public ComboBox ComboBox {
get { return m_comboBox; }
}

public DataGridColumnStylePadding Padding {
get { return m_padding; }
set { m_padding = value; }
}

public Size ControlSize {
get { return m_comboBox.Size; }
set { m_comboBox.Size = value; }
}

protected override void Abort(int rowNum) {

// reset combobox
m_comboBox.Visible = false;

}

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

Debug.WriteLine( "ComboBox Edit" );

// get cursor coordinates
Point p = this.DataGridTableStyle.DataGrid.PointToClient( Cursor.Position );

// get control bounds
Rectangle controlBounds = this.GetControlBounds( bounds );

// get cursor bounds
Rectangle cursorBounds = new Rectangle( p.X, p.Y, 1, 1 );


m_comboBox.SelectedIndex = ( int ) this.GetColumnValueAtRow( source, rowNum );

Debug.WriteLine( "SelectedItem: " + m_comboBox.SelectedIndex );
m_comboBox.Location = new Point( controlBounds.X, controlBounds.Y );
m_comboBox.Visible = true;

if ( cursorBounds.IntersectsWith( controlBounds ) ) {
m_comboBox.DroppedDown = true;
}

m_previouslyEditedCellRow = rowNum;

}

protected override bool Commit( CurrencyManager dataSource, int rowNum ) {

if ( m_previouslyEditedCellRow == rowNum ) {
this.SetColumnValueAtRow( dataSource, rowNum, m_comboBox.SelectedIndex );
}

m_comboBox.Visible = false;

return true;

}

protected override void SetDataGridInColumn( DataGrid value ) {

base.SetDataGridInColumn( value );

if ( !value.Controls.Contains( m_comboBox ) ) {
value.Controls.Add( m_comboBox );
}

}

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

g.FillRectangle( new SolidBrush( Color.White ), bounds );

StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Near;
sf.LineAlignment = StringAlignment.Center;

Rectangle controlBounds = this.GetControlBounds( bounds );

int colValue = ( int ) this.GetColumnValueAtRow( source, rowNum );

string selectedItem = (((DataRowView)m_comboBox.Items[ colValue ])[displayMember]).ToString();

Rectangle textRegion = new Rectangle(
controlBounds.X + 1,
controlBounds.Y + 4,
controlBounds.Width - 3,
( int ) g.MeasureString( selectedItem, m_comboBox.Font ).Height );

g.DrawString( selectedItem, m_comboBox.Font, foreBrush, textRegion, sf );

ControlPaint.DrawBorder3D( g, controlBounds, Border3DStyle.Sunken );

Rectangle buttonBounds = controlBounds;
buttonBounds.Inflate( -2, -2 );

ControlPaint.DrawComboButton(
g,
buttonBounds.X + ( controlBounds.Width - 20 ),
buttonBounds.Y,
16,
17,
ButtonState.Normal );

}

private void ComboBox_SizeChanged(object sender, EventArgs e) {

this.ControlSize = m_comboBox.Size;
this.Width = this.GetPreferredSize( null, null ).Width;
this.Invalidate();

}

private Rectangle GetControlBounds( Rectangle cellBounds ) {

Rectangle controlBounds = new Rectangle(
cellBounds.X + this.Padding.Left,
cellBounds.Y + this.Padding.Top,
this.ControlSize.Width,
this.ControlSize.Height );

return controlBounds;

}

#region The rest of the DataGridColumnStyle methods

protected override int GetMinimumHeight() {
return GetPreferredHeight( null, null );
}

protected override int GetPreferredHeight(System.Drawing.Graphics g, object value) {
return this.ControlSize.Height + this.Padding.Top + this.Padding.Bottom;
}

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

int width = this.ControlSize.Width + this.Padding.Left + this.Padding.Right;
int height = this.ControlSize.Height + this.Padding.Top + this.Padding.Bottom;

return new Size( width, height );

}

protected override void Paint(System.Drawing.Graphics g, System.Drawing.Rectangle bounds, CurrencyManager source, int rowNum) {
this.Paint( g, bounds, source, rowNum, false );
}

protected override void Paint(System.Drawing.Graphics g, System.Drawing.Rectangle bounds, CurrencyManager source, int rowNum, bool alignToRight) {
this.Paint( g, bounds, source, rowNum, Brushes.White, Brushes.Black, false );
}

#endregion The rest of the DataGridColumnStyle methods


}

}

DataGridColumnStylePadding类

using System;
using System.ComponentModel;

namespace Microsoft.Msdn.Article.DataGridColumnStyles {

public class DataGridColumnStylePadding {

int m_left;
int m_right;
int m_top;
int m_bottom;

public int Left {
get { return m_left; }
set { m_left = value; }
}

public int Right {
get { return m_right; }
set { m_right = value; }
}

public int Top {
get { return m_top; }
set { m_top = value; }
}

public int Bottom {
get { return m_bottom; }
set { m_bottom = value; }
}

public void SetPadding( int padValue ) {

m_left = padValue;
m_right = padValue;
m_top = padValue;
m_bottom = padValue;

}

public void SetPadding( int top, int right, int bottom, int left ) {
UpdatePaddingValues( top, right, bottom, left );
}

public DataGridColumnStylePadding( int padValue ) {
this.SetPadding( padValue );
}

public DataGridColumnStylePadding( int top, int right, int bottom, int left ) {
UpdatePaddingValues( top, right, bottom, left );
}

private void UpdatePaddingValues( int top, int right, int bottom, int left ) {

m_top = top;
m_right = right;
m_bottom = bottom;
m_left = left;

}

}

}


以上是我用的微软的一个例子,且修改为可以绑定数据源的combobox

datagrid里建立一个此类型的列,将comboBox加入到此列,并绑定数据源

如果说下拉框都相同,很简单可以实现,但是事实是一个调查表,所以选项肯定可能都相同,我现在都快要放弃DataGrid了,都想用控件自动生成一张由label,和下拉的调查表了,微软的DataGrid好难实现复杂功能,由于掌握得比较肤浅,所以无法驾驭,但是DevExpress的xTraGrid又太复杂,唉,有哪位高手能帮我指点一下呢?

谢谢!
...全文
260 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
newstone25 2006-06-28
  • 打赏
  • 举报
回复
遇到同样的问题。
关注。
skylineforever 2006-06-28
  • 打赏
  • 举报
回复
我原来只能绑定一个死的DataView,我试试RowFilter。
Knight94 2006-06-28
  • 打赏
  • 举报
回复
to 获得焦点时重新绑定会影响其它的行里的combobox啊。我本来选择好的其它行的ComboBox不是跟着变化了吗?

谁获的焦点,根据条件设置新的combobox数据,没获得焦点,当然不会进行改变。
skylineforever 2006-06-28
  • 打赏
  • 举报
回复
你不是写了个了DataGridComboBoxColumn,那么在获得焦点的时候,对combobox进行重新绑定不就行了,例如:
DataView dv = new DataView( yourDataTable, rowfilter, null, ... );
//use "dv" to bind your combobox
==============
获得焦点时重新绑定会影响其它的行里的combobox啊。我本来选择好的其它行的ComboBox不是跟着变化了吗?
Knight94 2006-06-28
  • 打赏
  • 举报
回复
to DataTable中有一列是QID,如果说是直接显示选项组还是很简单的,关键怎么根据行不同,下拉列表的值也不同呢,对DataGrid了解还很肤浅,所以要提这个问题了,微软那个例子的确给我改成一个ComboBox实例来显示不同的数值肯定是不行的,那么要怎么改呢

你不是写了个了DataGridComboBoxColumn,那么在获得焦点的时候,对combobox进行重新绑定不就行了,例如:
DataView dv = new DataView( yourDataTable, rowfilter, null, ... );
//use "dv" to bind your combobox
skylineforever 2006-06-28
  • 打赏
  • 举报
回复
Knight94(愚翁)

是这样子的。

答案组表
|QID| |QName|
1 答案组1
2 答案组2

选项组表
|id| |QID| |AName| |Percent|
1 1 很好 80%
2 1 不错 60%
3 1 一般 40%
4 1 差 20%
5 2 严重 100%
6 2 还行 60%
7 2 不行 20%

DataTable中有一列是QID,如果说是直接显示选项组还是很简单的,关键怎么根据行不同,下拉列表的值也不同呢,对DataGrid了解还很肤浅,所以要提这个问题了,微软那个例子的确给我改成一个ComboBox实例来显示不同的数值肯定是不行的,那么要怎么改呢?
Knight94 2006-06-28
  • 打赏
  • 举报
回复
其实这不是微软的问题,就你所给出的例子,是把不同组合数据用同一列来表明,这首先在数据库设计上连范式一都满足不了。

如果你的combox列和某些信息与其他表有外键关联信息的话,是可以做的。
addwing 2006-06-28
  • 打赏
  • 举报
回复
关注。。。学习。。。帮你顶。。。
skylineforever 2006-06-28
  • 打赏
  • 举报
回复
楼上的哥们难道也是点击一行再对DataView重新绑定?然后把选中的值存在隐藏的Column里?

我一开始就是这么想的,但是觉得微软搞这么个半成品给我们用,是比较郁闷的。。
wlovenet 2006-06-28
  • 打赏
  • 举报
回复
一年前做过,现在记不得了,呵呵
skylineforever 2006-06-28
  • 打赏
  • 举报
回复
三只熊熊 :你的方法我想过,我传给那个列的ComboBox是一个实例,点击一个变化的话,都会变的啊,其它行的选项不就又变化了?

Knight94(愚翁) ( )
比如说第一行我的下拉应该是
|很好|
|不错|
|一般|
|差|

第二行是
|严重|
|还行|
|不行|
Knight94 2006-06-28
  • 打赏
  • 举报
回复
如果数据不相同,是否合绑定的列有什么关系
张赐 2006-06-28
  • 打赏
  • 举报
回复
可以判断一下是点击的哪一行,然后再根据条件给combbox加不同的items
例行更新,不过本次有新组件加入,感觉这次的组件早就应该有了,居然到现在才加入进来,不管怎么说有总比没有好。这次还是以改进为主,改进项占了大多数。废话不多说具体内容大家看更新说明吧!另外由于经常收到chm格式文件无法用的反馈,其实不是无法用,只是要授权。虽然已经解释多遍,但是依然有人不知道,索性就取消chm格式的文档了,今后统一采用exe+pdf格式,由于目前尚无时间制作pdf格式的api文档,所以1.5版中只有exe的,pdf格式将在下一版中提供。 jQuery EasyUI 1.5版本更新内容: Bug(修复) combobox:修复在加载包含所选项数据的时候不触发“onSelect”事件的BUG; datagrid:修复在字段设置为一个空值的时候导致在某些情况下“updateRow”方法无法正常工作的BUG。 Improvement(改进) 一个label标签可以被关联到任意表单的字段上; combobox:改进在下拉项中“select”和“unselect”的规则; combobox:添加“limitToList”属性来限制只能输入在列表项中的内容; combogrid:允许用户快速克隆组件; form:添加“dirty”属性,允许用户只发送变更的字段内容; form:添加“resetDirty”方法; datagrid:允许用户在没有数据的时候显示一条消息(比如:无记录); textbox:添加“label”、“labelWidth”、“labelPosition”和“labelAlign”属性; spinner:添加“spinAlign”属性; calendar:允许用户在日历组件上显示周数(今年的第几周); window:添加“constrain”属性。 New Plugin(新组件) passwordbox:该插件允许用户在具有更好交互功能的输入框中输入密码; combotreegrid:该插件结合了combobox和treegrid组件。
大家好,又见面了!EasyUI又更新了,这次更新内容还是不少的,具体内容请参考下面的更新说明,官方的更新说明中还少了1条,我给补上了。 jQuery EasyUI 1.3.5版本更新内容: Bug(修复) searchbox:修复“searcher”函数提供的“name”参数值错误的问题; combo:修复“isValid”方法无法返回布尔值的问题; combo:修复点击页面某一个combo组件的下拉列表时触发的“onHidePanel”事件导致页面上其他combo组件的下拉列表被关闭的问题; combogrid:修复某些从combo组件继承来的方法无法使用的问题。 Improvement(改进) datagrid:改进检查行时候的性能; menu:允许追加菜单分隔符; menu:新增“hideOnUnHover”属性用于在鼠标离开菜单的时候指示是否需要隐藏菜单; slider:新增“clear”和“reset”方法; tabs:新增“unselect”方法、“onUnselect”事件; tabs:新增“selected”属性,用于指定的默认打开的面板; tabs:Tab Panel(Tab页)新增“collapsible”属性,用于设置是否允许摺叠面板; tabs:新增“showHeader”属性、“showHeader”方法和“hideHeader”方法; combobox:允许“disabled”属性禁用下拉列表选项; tree:改进数据加载时候的性能; pagination:新增“layout”属性,用于自定义控件的样式布局; accordion:新增“unselect”方法、“onUnselect”事件; accordion:新增“select”和“multiple”属性; accordion:新增“getSelections”方法; datebox:新增“sharedCalendar”属性,允许多个datebox控件共享使用同一个calendar控件。 datebox:新增“buttons”属性,用于自定义日历下方的按钮。 (译者注:该点更新内容官方更新公告上没有注明,具体内容和用法请看datebox的API。) 历史版本: - jQuery EasyUI 1.3.4 离线API简体中文版 http://download.csdn.net/detail/richie696/6302785 - jQuery EasyUI 1.3.4 离线API简体中文版 http://download.csdn.net/detail/richie696/5363933
最近比较忙,抽空做了最新版的API,本次的主要精力就是放在了pdf版的文档上面,看了上一版好多人反应说希望保留chm格式的,所以这一版继续提供chm格式的文档给大家了,现在的版本中包含了PDF、EXE和CHM 3种格式的文档,相信应该可以满足大家的需要了。此外我个人推荐大家使用PDF格式的文档,因为PDF是全新制作的,内容进行了完整校对,所以错漏的地方比EXE和CHM格式要少很多。其它废话就不多说了。更新内容自己看更新说明吧! jQuery EasyUI 1.5.1版本更新内容: Bug(修复) datagrid:修复在调用“updateRow”方法之后选中和复选行标志丢失的问题; tabs:修复在调用“update”方法的时候导致标签栏工具错位的问题; window:修复在窗体高度设置为“auto”时,当移动窗体后窗体丢失的问题; messager:修复在现实进度消息窗口后立即关闭该窗口导致程序发生异常的问题; form:修复“clear”方法无法清除combobox组件选择的下拉项的问题。 Improvement(改进) textbox:可以用“cls”属性添加自定义样式; numberbox:允许用户使用意大利货币格式; combo:添加“multivalue”属性,允许用户决定如何提交多个值; combobox:添加“reversed”属性; combobox:添加“onClick”事件; combogrid:添加“reversed”属性; treegrid:使用Shift键启用多值选择。 New Plugin(新组件) tagbox:允许用户在表单字段上添加标签。
本次更新,我将文档从头到尾和官网发布的最新内容校对了一次,将所有的错误和遗漏的API全部补全和修复了。快半年了,EasyUI又更新了。这次依然以BUG修复和功能改进为主,EasyUI发展至今,主体功能已经基本完善。即便以后还有新组件,那也是在现有功能完善的基础上新增一些小组件了,类似Datagrid这类的大型复合组件应该比较少了,相比新组件我更期待官方能对EasyUI的性能优化多下下功夫。 jQuery EasyUI 1.4.5版本更新内容: Bug(修复) datagrid:修复在调用updateRow方法之后使用getChanges方法无法返回被更新的行的BUG; treegrid:修复在追加或插入新行的时候触发onLoadSuccess事件的BUG; tree:修复在追加或插入新节点的时候触发onLoadSuccess事件的BUG。 Improvement(改进) window:可以自定义显示样式了; window:新增“border”属性允许用户设置不同的边框样式; navpanel:新增“href”属性用以从远程服务器加载显示内容; combotree:“setValue”和“setValues”方法增加“id”和“text”形参; combobox:新增“showItemIcon”属性; combobox:在“groupPosition”属性值设置为“sticky”时,将将选项分组标签固顶在下拉栏中; messager:当敲击回车键时将默认触发消息框的第一个按钮; validatebox:新增“editable”、“disabled”、“readonly”、“validateOnCreate”和“validateOnBlur”属性; validatebox:新增“enable”、“disable”、“readonly”和“resetValidation”方法; validatebox:允许用户来决定如何显示错误消息; filebox:新增“accept”和“multiple”属性; treegrid:新增复选框的选择; treegrid:新增“getCheckedNodes”、“checkNode”和“uncheckNode”方法; form:新增“iframe”属性;(官方未提及) form:新增“onProgress”事件;(官方未提及) form:新增“resetValidation”方法。(官方未提及)

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧