哪里可以找到这样一个Grid,就像数据库中设计表的那样 但dataGrid不能编辑组合框之类的控件呀,我指的是桌面应用程序,不是web上的 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 http://www.codeproject.com/useritems/DataGridComboBoxColumn.asp 请问 dy_2000_abc(芝麻开门) DataGridComboBoxColumn类从那里来呢,需要从它那里下载吗? 我给你贴出来吧:using System;using System.Windows.Forms;using System.Drawing;using System.Data;using System.Collections;namespace Hamster.Common{ /// <summary> /// Implementation of a ComboBox as a column in a DataGrid /// </summary> public class DataGridComboBoxColumn : DataGridColumnStyle { private int _xMargin = 2; private int _yMargin = 1; private ComboBox _comboBox; private string _oldValue=""; private bool _inEdit= false; private DataTable _dataTable; private string _displayMember, _valueMember; /// <summary> /// The only creator ;) /// </summary> /// <param name="colName">The name of the column</param> /// <param name="dataSource">The datasource that contains the lookup table</param> /// <param name="displayMember">The member of the lookuptable to display</param> /// <param name="valueMember">The member of the lookuptable with the value</param> /// <param name="dataGrid">The datagrid parent of this column</param> public DataGridComboBoxColumn(string colName, DataTable dataSource, string displayMember, string valueMember, DataGrid dataGrid) { _comboBox = new ComboBox(); _comboBox.Visible = false; _comboBox.DataSource = dataSource; _dataTable = dataSource; _comboBox.DisplayMember = displayMember; _displayMember=displayMember; _valueMember=valueMember; _comboBox.ValueMember = valueMember; _comboBox.DropDownStyle = ComboBoxStyle.DropDownList; Graphics _graphicsContext=dataGrid.CreateGraphics(); float _widest=0; SizeF _stringSize=new SizeF(0,0); foreach (DataRow dr in dataSource.Rows) { _stringSize=_graphicsContext.MeasureString(dr[displayMember].ToString(), dataGrid.Font); if (_stringSize.Width>_widest) { _widest=_stringSize.Width; } } _comboBox.DropDownWidth=(int)Math.Ceiling(_widest); this.Width=_comboBox.DropDownWidth+25; // Add the space for the dropdown arrow this.MappingName=colName; this.HeaderText=colName; dataGrid.Controls.Add(_comboBox); } /// <summary> /// Standard override /// </summary> /// <param name="rowNum"></param> protected override void Abort(int rowNum) { _inEdit=false; _comboBox.Hide(); } /// <summary> /// Standard override /// </summary> /// <param name="dataSource"></param> /// <param name="rowNum"></param> /// <returns></returns> protected override bool Commit(CurrencyManager dataSource,int rowNum) { if(!_inEdit) { return true; } try { object _value = _comboBox.SelectedValue; if(NullText.Equals(_value)) { _value = System.Convert.DBNull; } this.SetColumnValueAtRow(dataSource, rowNum, _value); } catch { return false; } finally { _inEdit=false; _comboBox.Hide(); } return true; } /// <summary> /// Standard override /// </summary> /// <param name="source"></param> /// <param name="rowNum"></param> /// <param name="bounds"></param> /// <param name="readOnly"></param> /// <param name="instantText"></param> /// <param name="cellIsVisible"></param> protected override void Edit(CurrencyManager source, int rowNum, Rectangle bounds, bool readOnly, string instantText, bool cellIsVisible) { _comboBox.Text = ""; Rectangle _originalBounds = bounds; _oldValue = _comboBox.Text; if(!cellIsVisible) return; bounds.Offset(_xMargin, _yMargin); bounds.Width -= _xMargin * 2; bounds.Height -= _yMargin; _comboBox.Bounds = bounds; _comboBox.Visible = true; //_comboBox.BringToFront(); //_comboBox.Focus(); _comboBox.SelectedValue = GetText(GetColumnValueAtRow(source, rowNum)); if(instantText!=null) { _comboBox.SelectedValue = instantText; int End = _comboBox.Text.Length; _comboBox.Select(End, 0); } else { _comboBox.SelectAll(); } //this.DataGridTableStyle.DataGrid.Invalidate(OriginalBounds); //_comboBox.BringToFront(); _inEdit = true; } /// <summary> /// Standard override /// </summary> /// <returns></returns> protected override int GetMinimumHeight() { return _comboBox.PreferredHeight + _yMargin; } /// <summary> /// Standard override /// </summary> /// <param name="g"></param> /// <param name="val"></param> /// <returns></returns> protected override int GetPreferredHeight(Graphics g ,object val) { return FontHeight + _yMargin; } /// <summary> /// Standard override /// </summary> /// <param name="g"></param> /// <param name="val"></param> /// <returns></returns> protected override Size GetPreferredSize(Graphics g, object val) { Size _extents = Size.Ceiling(g.MeasureString(GetText(val), this.DataGridTableStyle.DataGrid.Font)); _extents.Width += _xMargin * 2 ; _extents.Height += _yMargin; return _extents; } /// <summary> /// Standard override /// </summary> /// <param name="g"></param> /// <param name="bounds"></param> /// <param name="source"></param> /// <param name="rowNum"></param> protected override void Paint(Graphics g,Rectangle bounds,CurrencyManager source,int rowNum) { Paint(g, bounds, source, rowNum, false); } /// <summary> /// Standard override /// </summary> /// <param name="g"></param> /// <param name="bounds"></param> /// <param name="source"></param> /// <param name="rowNum"></param> /// <param name="alignToRight"></param> protected override void Paint(Graphics g,Rectangle bounds,CurrencyManager source,int rowNum, bool alignToRight) { string _text = GetText(GetColumnValueAtRow(source, rowNum)); foreach (DataRow dr in _dataTable.Rows) { if (dr[_valueMember].ToString()==_text) { _text=dr[_displayMember].ToString(); break; } } PaintText(g, bounds, _text, alignToRight); } // Helper functions private void PaintText(Graphics g ,Rectangle bounds, string text,bool alignToRight) { Brush _backBrush = new SolidBrush(this.DataGridTableStyle.BackColor); Brush _foreBrush= new SolidBrush(this.DataGridTableStyle.ForeColor); Rectangle _rect = bounds; RectangleF _rectF = _rect; StringFormat _format = new StringFormat(); if(alignToRight) { _format.FormatFlags = StringFormatFlags.DirectionRightToLeft; } switch(this.Alignment) { case HorizontalAlignment.Left: _format.Alignment = StringAlignment.Near; break; case HorizontalAlignment.Right: _format.Alignment = StringAlignment.Far; break; case HorizontalAlignment.Center: _format.Alignment = StringAlignment.Center; break; } _format.FormatFlags =StringFormatFlags.NoWrap; g.FillRectangle(_backBrush, _rect); _rect.Offset(0, _yMargin); _rect.Height -= _yMargin; g.DrawString(text, this.DataGridTableStyle.DataGrid.Font, _foreBrush, _rectF, _format); _format.Dispose(); } private string GetText(object val) { if(val==System.DBNull.Value) { return this.NullText; } if(val!=null) { return val.ToString(); } else { return string.Empty; } } }} dy_2000_abc(芝麻开门):我将上面的派生类加入工程中,但界面出来后没任何效果我的步骤是1.生成一个DataGridTableStyle实例,如td2.生成一个DataGridComboBoxColumn实例,如tc,并加入到td中,这样这个组合框列应该是第一列吧3.将td加入到datagrid中但grid中仍没出现comboBox. dy_2000_abc(芝麻开门)兄:在上面的重载函数Commit和Edit中,从组合框选择的是Value而不是它的Text,这样最后选完后赋给Column的是Value而非Text,是不是要Value和text都要相同才行. ultraISO 多个ISO文件设置成U盘启动 IE提示组织不安全方式的activex控件。,怎么能允许用该控件呢? 复合控件没有显示下划线 100急求,如何代码生成强类型DataSet? 如何实现手绘的打印预览??? 问一个API函数,如何获取当前电脑中所有窗体的句柄(50) 如何跟踪物体的运动? 请大家帮帮忙,如何读取ftp服务器上的图片啊???急!!! 无重复查询用SQL语句如何表达? 那里有Visual Studio .net 7.0 Beta1 C# IDE开发环境? 旧问题!--如何判断所有的mdi子窗体都已经关闭?? Grid子表中删除选定行的记录?
DataGridComboBoxColumn类从那里来呢,需要从它那里下载吗?
using System;
using System.Windows.Forms;
using System.Drawing;
using System.Data;
using System.Collections;namespace Hamster.Common
{
/// <summary>
/// Implementation of a ComboBox as a column in a DataGrid
/// </summary>
public class DataGridComboBoxColumn : DataGridColumnStyle {
private int _xMargin = 2;
private int _yMargin = 1;
private ComboBox _comboBox;
private string _oldValue="";
private bool _inEdit= false;
private DataTable _dataTable;
private string _displayMember, _valueMember; /// <summary>
/// The only creator ;)
/// </summary>
/// <param name="colName">The name of the column</param>
/// <param name="dataSource">The datasource that contains the lookup table</param>
/// <param name="displayMember">The member of the lookuptable to display</param>
/// <param name="valueMember">The member of the lookuptable with the value</param>
/// <param name="dataGrid">The datagrid parent of this column</param>
public DataGridComboBoxColumn(string colName, DataTable dataSource, string displayMember, string valueMember, DataGrid dataGrid) {
_comboBox = new ComboBox();
_comboBox.Visible = false;
_comboBox.DataSource = dataSource;
_dataTable = dataSource;
_comboBox.DisplayMember = displayMember;
_displayMember=displayMember;
_valueMember=valueMember;
_comboBox.ValueMember = valueMember;
_comboBox.DropDownStyle = ComboBoxStyle.DropDownList;
Graphics _graphicsContext=dataGrid.CreateGraphics();
float _widest=0;
SizeF _stringSize=new SizeF(0,0);
foreach (DataRow dr in dataSource.Rows) {
_stringSize=_graphicsContext.MeasureString(dr[displayMember].ToString(), dataGrid.Font);
if (_stringSize.Width>_widest) {
_widest=_stringSize.Width;
}
}
_comboBox.DropDownWidth=(int)Math.Ceiling(_widest);
this.Width=_comboBox.DropDownWidth+25; // Add the space for the dropdown arrow
this.MappingName=colName;
this.HeaderText=colName;
dataGrid.Controls.Add(_comboBox);
} /// <summary>
/// Standard override
/// </summary>
/// <param name="rowNum"></param>
protected override void Abort(int rowNum) {
_inEdit=false;
_comboBox.Hide();
}
/// <summary>
/// Standard override
/// </summary>
/// <param name="dataSource"></param>
/// <param name="rowNum"></param>
/// <returns></returns>
protected override bool Commit(CurrencyManager dataSource,int rowNum) {
if(!_inEdit) {
return true;
}
try {
object _value = _comboBox.SelectedValue;
if(NullText.Equals(_value)) {
_value = System.Convert.DBNull;
}
this.SetColumnValueAtRow(dataSource, rowNum, _value);
}
catch {
return false;
}
finally {
_inEdit=false;
_comboBox.Hide();
}
return true;
}
/// <summary>
/// Standard override
/// </summary>
/// <param name="source"></param>
/// <param name="rowNum"></param>
/// <param name="bounds"></param>
/// <param name="readOnly"></param>
/// <param name="instantText"></param>
/// <param name="cellIsVisible"></param>
protected override void Edit(CurrencyManager source, int rowNum, Rectangle bounds, bool readOnly, string instantText, bool cellIsVisible) {
_comboBox.Text = "";
Rectangle _originalBounds = bounds;
_oldValue = _comboBox.Text;
if(!cellIsVisible) return; bounds.Offset(_xMargin, _yMargin);
bounds.Width -= _xMargin * 2;
bounds.Height -= _yMargin;
_comboBox.Bounds = bounds;
_comboBox.Visible = true;
//_comboBox.BringToFront();
//_comboBox.Focus();
_comboBox.SelectedValue = GetText(GetColumnValueAtRow(source, rowNum));
if(instantText!=null) {
_comboBox.SelectedValue = instantText;
int End = _comboBox.Text.Length;
_comboBox.Select(End, 0);
} else {
_comboBox.SelectAll();
} //this.DataGridTableStyle.DataGrid.Invalidate(OriginalBounds);
//_comboBox.BringToFront();
_inEdit = true;
}
/// <summary>
/// Standard override
/// </summary>
/// <returns></returns>
protected override int GetMinimumHeight() {
return _comboBox.PreferredHeight + _yMargin;
}
/// <summary>
/// Standard override
/// </summary>
/// <param name="g"></param>
/// <param name="val"></param>
/// <returns></returns>
protected override int GetPreferredHeight(Graphics g ,object val) {
return FontHeight + _yMargin;
}
/// <summary>
/// Standard override
/// </summary>
/// <param name="g"></param>
/// <param name="val"></param>
/// <returns></returns>
protected override Size GetPreferredSize(Graphics g, object val) {
Size _extents = Size.Ceiling(g.MeasureString(GetText(val), this.DataGridTableStyle.DataGrid.Font));
_extents.Width += _xMargin * 2 ;
_extents.Height += _yMargin;
return _extents;
}
/// <summary>
/// Standard override
/// </summary>
/// <param name="g"></param>
/// <param name="bounds"></param>
/// <param name="source"></param>
/// <param name="rowNum"></param>
protected override void Paint(Graphics g,Rectangle bounds,CurrencyManager source,int rowNum) {
Paint(g, bounds, source, rowNum, false);
} /// <summary>
/// Standard override
/// </summary>
/// <param name="g"></param>
/// <param name="bounds"></param>
/// <param name="source"></param>
/// <param name="rowNum"></param>
/// <param name="alignToRight"></param>
protected override void Paint(Graphics g,Rectangle bounds,CurrencyManager source,int rowNum, bool alignToRight) {
string _text = GetText(GetColumnValueAtRow(source, rowNum));
foreach (DataRow dr in _dataTable.Rows) {
if (dr[_valueMember].ToString()==_text) {
_text=dr[_displayMember].ToString();
break;
}
}
PaintText(g, bounds, _text, alignToRight);
} // Helper functions
private void PaintText(Graphics g ,Rectangle bounds, string text,bool alignToRight) {
Brush _backBrush = new SolidBrush(this.DataGridTableStyle.BackColor);
Brush _foreBrush= new SolidBrush(this.DataGridTableStyle.ForeColor);
Rectangle _rect = bounds;
RectangleF _rectF = _rect;
StringFormat _format = new StringFormat();
if(alignToRight) {
_format.FormatFlags = StringFormatFlags.DirectionRightToLeft;
}
switch(this.Alignment) {
case HorizontalAlignment.Left:
_format.Alignment = StringAlignment.Near;
break;
case HorizontalAlignment.Right:
_format.Alignment = StringAlignment.Far;
break;
case HorizontalAlignment.Center:
_format.Alignment = StringAlignment.Center;
break;
}
_format.FormatFlags =StringFormatFlags.NoWrap;
g.FillRectangle(_backBrush, _rect);
_rect.Offset(0, _yMargin);
_rect.Height -= _yMargin;
g.DrawString(text, this.DataGridTableStyle.DataGrid.Font, _foreBrush, _rectF, _format);
_format.Dispose();
} private string GetText(object val) {
if(val==System.DBNull.Value) {
return this.NullText;
}
if(val!=null) {
return val.ToString();
}
else {
return string.Empty;
}
}
}
}
我将上面的派生类加入工程中,但界面出来后没任何效果
我的步骤是
1.生成一个DataGridTableStyle实例,如td
2.生成一个DataGridComboBoxColumn实例,如tc,并加入到td中,这样这个组合框列应该是第一列吧
3.将td加入到datagrid中
但grid中仍没出现comboBox.
在上面的重载函数Commit和Edit中,从组合框选择的是Value而不是它的Text,这样最后选完后赋给Column的是Value而非Text,是不是要Value和text都要相同才行.