怎樣在DATAGRID的欄中加入指定的控件,如某一欄加入combobox控件到DATAGRID中.

汽车爸爸 员工  2004-04-30 09:32:08
初學,很多都不懂,我想很多菜鳥都像我一樣想知道這些知識,請大俠們不吝指教。
菜鳥如有進步,一定是大俠們付出辛勤勞動、無私奉獻結果,如果有一天,菜鳥變大蝦,也必傾廊相授。以促進社區的繁榮!
...全文
54 8 点赞 打赏 收藏 举报
写回复
8 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
lxcc 2004-04-30
添加一个模板列
<asp:TemplateColumn HeaderText="父类别">
<ItemTemplate>
<asp:Label runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.父类别") %>' ID="Label6">
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList id="ddlParent" runat="server" Width="80"></asp:DropDownList>
</EditItemTemplate>
</asp:TemplateColumn>

后台代码:
private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.EditItem )
{
string sParentType=(string)DataBinder.Eval(e.Item.DataItem, "父类别");
DropDownList ddlParentType=(DropDownList)e.Item.FindControl("ddlParent");

ddlParentType.DataTextField="sTypeName";
ddlParentType.DataValueField="nTypeID";
//得到DropDownList的数据集
ddlParentType.DataSource=getParentType();
ddlParentType.DataBind();

ddlParentType.Items.FindByText(sParentType).Selected=true;
}
  • 打赏
  • 举报
回复
Samen168 2004-04-30
public event EventHandlerComboBoxColummDataChanging DataChanging;
/// <summary>
/// 在派生类中被重写时,将启动一个请求来完成编辑过程
/// </summary>
/// <param name="dataSource"></param>
/// <param name="rowNum"></param>
/// <returns></returns>
protected override bool Commit(CurrencyManager dataSource, int rowNum)
{
pComboBox.Bounds = Rectangle.Empty;

if (!isEditing)
return true;

isEditing = false;

try
{
try
{
if(this.DataChanging!=null)
{
this.DataChanging(this.pComboBox.SelectedValue);
}
}
catch(Exception er)
{
MessageBox.Show(er.Message);
return false;
}
SetColumnValueAtRow(dataSource, rowNum, this.pComboBox.SelectedValue );
}
catch (Exception)
{
Abort(rowNum);
return false;
}

Invalidate();
return true;
}

/// <summary>
/// 准备单元格以便编辑值
/// </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)
{
object sValue;

if(GetColumnValueAtRow(source, rowNum) is System.DBNull)
{
sValue="";
}
else
{
sValue = GetColumnValueAtRow(source, rowNum) ;
}

if (cellIsVisible && !this.ReadOnly)
{
pComboBox.Bounds = new Rectangle(bounds.X + 1, bounds.Y + 1, bounds.Width - 2, bounds.Height - 2);
pComboBox.SelectedValue = sValue ;
pComboBox.Visible = true;
pComboBox.GotFocus+=new EventHandler(pComboBox_GotFocus);
}
else
{
pComboBox.Visible = false;
}

if (pComboBox.Visible)
DataGridTableStyle.DataGrid.Invalidate(bounds);
}

/// <summary>
/// 获取指定值的宽度和高度
/// </summary>
/// <param name="g"></param>
/// <param name="value"></param>
/// <returns></returns>
protected override Size GetPreferredSize(Graphics g, object value)
{
return new Size(100, pComboBox.PreferredHeight + 2);
}

/// <summary>
/// 将获取一行的最小高度
/// </summary>
/// <returns></returns>
protected override int GetMinimumHeight()
{
return pComboBox.PreferredHeight + 2;
}

/// <summary>
/// 获取自动调整列的大小所用的高度
/// </summary>
/// <param name="g"></param>
/// <param name="value"></param>
/// <returns></returns>
protected override int GetPreferredHeight(Graphics g, object value)
{
return pComboBox.PreferredHeight + 2;
}

/// <summary>
/// 绘制列
/// </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);
}

protected override void Paint(Graphics g,Rectangle bounds,CurrencyManager source, int rowNum,bool alignToRight)
{
Paint(g,bounds, source, rowNum, Brushes.Red, Brushes.Blue, alignToRight);
}

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

if(selectedValue is System.DBNull)
{
selectedValue = "";
}
else
{
if(selectedValue is System.Boolean )
selectedValue = Convert.ToInt32(selectedValue).ToString();

string filter=string.Format("{0}='{1}'" , this.ValueMember , selectedValue);
if(this._tableSource.Columns[this.valueMember].DataType==typeof(int)
|| this._tableSource.Columns[this.valueMember].DataType==typeof(short))
{
if(selectedValue.ToString().Length==0)
selectedValue=0;
filter=string.Format("{0}={1}" , this.ValueMember , selectedValue);
}

filter+= ((this.defaultFilter.Length>0) ? " And " + this.defaultFilter : "");

DataRow[] dr = _tableSource.Select(filter);
if(dr != null && dr.Length ==1)
selectedValue = dr[0][this.DisplayMember].ToString() ;
else
selectedValue=string.Empty;
}

int rows=Convert.ToInt32(bounds.Height/this.FontHeight);
if(rows==0)
{
rows=1;
}
int dec=Convert.ToInt32((bounds.Height-this.FontHeight*rows)/2);

if(dec<0)
{
dec=0;
}

Rectangle rect = new Rectangle(bounds.X,bounds.Y+dec,bounds.Width,bounds.Height-dec*2);

g.FillRectangle(backBrush,bounds);
rect.Offset(0, 2);
rect.Height -= 2;
g.DrawString(selectedValue.ToString() ,this.DataGridTableStyle.DataGrid.Font, foreBrush,rect);
}

/// <summary>
/// 设置列的 System.Windows.Forms.DataGrid
/// </summary>
/// <param name="value"></param>
protected override void SetDataGridInColumn(DataGrid value)
{
base.SetDataGridInColumn(value);
if (pComboBox.Parent != null)
{
pComboBox.Parent.Controls.Remove
(pComboBox);
}
if (value != null)
{
value.Controls.Add(pComboBox);
}

}

private void pComboBox_GotFocus(object sender, EventArgs e)
{
this.isEditing = true;
base.ColumnStartedEditing(pComboBox);
}
}
public delegate void EventHandlerComboBoxColummDataChanging(object newValue);
}
  • 打赏
  • 举报
回复
Samen168 2004-04-30
using System;
using System.Drawing;
using System.Collections;
using System.Data;
using System.Windows.Forms;

namespace LongWin.UI.WinForm.Controls.ControlBase
{
/// <summary>
/// DataGridComboBoxColumn 的摘要说明:继承自DataGridColumnStyle,派生的类中,必须重写:Abort、Commit、Edit 和 Paint(两次)
/// 有关注释选自帮助
/// 参考帮助文档:承载DateTimePicker 控件示例
/// 位置:ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.2052/cpref/html/frlrfSystemWindowsFormsDataGridColumnStyleClassTopic.htm
/// </summary>
public class DataGridComboBoxColumn:DataGridColumnStyle
{
private ComboBox pComboBox=new ComboBox();
private bool isEditing;
private string valueMember="";
private string displayMember="";
DataTable _tableSource ;

/// <summary>
/// 构造方法
/// </summary>
/// <param name="dt"></param>
public DataGridComboBoxColumn():base()
{
pComboBox.Visible=false;
}

/// <summary>
/// 加载数据,参数:数据表
/// </summary>
/// <param name="dt"></param>
public void BindData(object dataSource)
{
BindData(dataSource,displayMember,valueMember);
}

/// <summary>
/// 加载数据,参数:数据表、显示字段名
/// </summary>
/// <param name="dt"></param>
/// <param name="sDisplayMember"></param>
public void BindData(object dataSource,string sDisplayMember)
{
BindData(dataSource,sDisplayMember,sDisplayMember);
}

string defaultFilter=string.Empty;
/// <summary>
/// 加载数据,参数:数据表、显示字段名、值字段名
/// </summary>
/// <param name="dt"></param>
/// <param name="sDisplayMember"></param>
/// <param name="sValueMember"></param>
public void BindData(object dataSource,string sDisplayMember,string sValueMember)
{
try
{
if(dataSource is DataTable)
{
_tableSource=dataSource as DataTable;
}
else if(dataSource is DataSet)
{
DataSet ds=dataSource as DataSet;
_tableSource=ds.Tables[0];
}
else if(dataSource is DataView)
{
DataView dv=dataSource as DataView;
defaultFilter=dv.RowFilter; //记录默认筛选条件

_tableSource=dv.Table;
}

if(sDisplayMember.Trim()=="")
{
throw new Exception("existingDipplay 参数不能为空!");
}
else
{
if(sValueMember.Trim()=="")
sValueMember=sDisplayMember;

this.pComboBox.DropDownStyle=ComboBoxStyle.DropDownList;
this.pComboBox.DataSource = dataSource ;
this.pComboBox.DisplayMember = this.DisplayMember ;
this.pComboBox.ValueMember = this.ValueMember ;

}
}
catch(Exception ex)
{
throw ex;
}
}

//值对应的数据字段名
public string ValueMember
{
get{ return valueMember;}
set{valueMember=value;}
}

//显示对应的数据字段名
public string DisplayMember
{
get{return displayMember;}
set{displayMember=value;}
}

/// <summary>
/// 在派生类中被重写时,将启动一个请求来中断编辑过程
/// </summary>
/// <param name="rowNum"></param>
protected override void Abort(int rowNum)
{
isEditing = false;
pComboBox.GotFocus-= new EventHandler(pComboBox_GotFocus);
Invalidate();
}
  • 打赏
  • 举报
回复
sy246 2004-04-30
接上:

System.Collections.Specialized.StringCollection initDatas = new System.Collections.Specialized.StringCollection();

DataGridComboBox ComboBoxColumn = new DataGridComboBox(initDatas);
ComboBoxColumn.HeaderText =headText;
ComboBoxColumn.MappingName = mappingName;
ComboBoxColumn.Width =width;
ComboBoxColumn.NullText="";
ComboBoxColumn.ReadOnly=readOnly;
ComboBoxColumn.ComboBoxInit+=new myCustomerEventHandler(ComboBoxColumn_ComboBoxInit);
style.GridColumnStyles.Add(ComboBoxColumn);
  • 打赏
  • 举报
回复
sy246 2004-04-30
public class DataGridComboBox : DataGridTextBoxColumn
{
public ComboBox ColumnComboBox = new ComboBox();
public event myCustomerEventHandler ComboBoxInit = null;
// private System.Windows.Forms.CurrencyManager _source = null;
// private int _rowNum;

private bool isEditing;
public DataGridComboBox(System.Collections.Specialized.StringCollection initDatas) : base()
{
for(int i=0;i< initDatas.Count; i++)
{
ColumnComboBox.Items.Add(initDatas[i]);
}
ColumnComboBox.Visible = false;
ColumnComboBox.DropDownStyle = ComboBoxStyle.DropDownList;
}
protected override void Abort(int rowNum)
{
isEditing = false;
ColumnComboBox.Visible = false;
// ColumnComboBox.SelectedIndexChanged -=
// new EventHandler(ComboBoxSelectedChanged);
Invalidate();
}
private void ComboBoxSelectedChanged(object sender, EventArgs e)
{
this.isEditing = true;
base.ColumnStartedEditing(ColumnComboBox);
}

protected override bool Commit
(CurrencyManager dataSource, int rowNum)
{
ColumnComboBox.Bounds = Rectangle.Empty;
// ColumnComboBox.SelectedIndexChanged -=
// new EventHandler(ComboBoxSelectedChanged);

if (!isEditing)
return true;
isEditing = false;
try
{
ColumnComboBox.Update();
if(ColumnComboBox.SelectedIndex < 0)
{
Abort(rowNum);
return false;
}
string value = ColumnComboBox.Items[ColumnComboBox.SelectedIndex].ToString();
SetColumnValueAtRow(dataSource, rowNum, value);
}
catch (Exception)
{
Abort(rowNum);
return false;
}

Invalidate();
return true;
}
public void InitCombox(string Para)
{

}
protected override void Edit(
CurrencyManager source,
int rowNum,
Rectangle bounds,
bool readOnly,
string instantText,
bool cellIsVisible)
{
MyEventArgs arg = new MyEventArgs(false,rowNum,"");
if(this.ComboBoxInit != null)
{
this.ComboBoxInit(this.ColumnComboBox,arg);
if(arg.MustReturn)
return;
}
if(this.ColumnComboBox.Items.Count <= 0)
return;
object obj = GetColumnValueAtRow(source,rowNum);
string value =string.Empty;
if(obj != System.DBNull.Value)
value = (string)obj;
// else return;

if (cellIsVisible)
{
ColumnComboBox.Bounds = new Rectangle
(bounds.X , bounds.Y ,
bounds.Width, bounds.Height );
ColumnComboBox.Visible = true;
ColumnComboBox.DropDownStyle = ComboBoxStyle.DropDownList;
//if(value != "")
ColumnComboBox.SelectedIndex = ColumnComboBox.FindString(value,-1);
ColumnComboBox.SelectionChangeCommitted += new EventHandler(ColumnComboBox_SelectionChangeCommitted);
// ColumnComboBox.SelectedIndexChanged += new EventHandler(ComboBoxSelectedChanged);
ColumnComboBox.Focus();
ColumnComboBox.Leave+=new EventHandler(ColumnComboBox_Leave);
if(ColumnComboBox.Text=="")
ColumnComboBox.SelectedIndex=-1;
// if( ColumnComboBox.SelectedIndex == -1)
// ColumnComboBox.SelectedIndex = 0;
isEditing = true;
}
else
{
// ColumnComboBox.Items[0] = value;
ColumnComboBox.Visible = false;
}

if (ColumnComboBox.Visible)
DataGridTableStyle.DataGrid.Invalidate(bounds);



}

protected override void SetDataGridInColumn(DataGrid value)
{//放置ComboBox控件
base.SetDataGridInColumn(value);
if(ColumnComboBox.Parent != null)
ColumnComboBox.Parent.Controls.Remove(ColumnComboBox);
if(value != null)
value.Controls.Add(ColumnComboBox);
}

protected override void Paint(Graphics g,
Rectangle bounds,
CurrencyManager source,
int rowNum)
{
Paint(g, bounds, source, rowNum, false);
}
protected override void Paint(
Graphics g,
Rectangle bounds,
CurrencyManager source,
int rowNum,
bool alignToRight)
{
Paint(
g,bounds,
source,
rowNum,
Brushes.Red,
Brushes.Blue,
alignToRight);
}
protected override void Paint(
Graphics g,
Rectangle bounds,
CurrencyManager source,
int rowNum,
Brush backBrush,
Brush foreBrush,
bool alignToRight)
{
object obj = GetColumnValueAtRow(source,rowNum);
string cbox = "";
if(obj != System.DBNull.Value)
cbox = (string)obj;
Rectangle rect= bounds;
g.FillRectangle(backBrush,rect);
rect.Offset(0, 2);
rect.Height -= 2;
g.DrawString(cbox,
this.DataGridTableStyle.DataGrid.Font,
foreBrush, rect);
}

private void ColumnComboBox_Leave(object sender, EventArgs e)
{
// if(this.ColumnComboBox.Focused == false)
// return;
// if(this.ColumnComboBox.SelectedIndex < 0)
// {
// this.ColumnComboBox.Focus();
// MessageBox.Show("请选择数据!");
// return;
// }
// if(isEditing)
// {
// SetColumnValueAtRow(_source, _rowNum, ColumnComboBox.Text);
// isEditing = false;
// Invalidate();
// }
ColumnComboBox.Hide();

}

private void ColumnComboBox_SelectionChangeCommitted(object sender, EventArgs e)
{
object obj = ColumnComboBox.SelectedItem;
if(ColumnComboBox.Items.Count > 0)
ColumnComboBox.Text = ColumnComboBox.Items[ColumnComboBox.SelectedIndex == -1 ? 0 : ColumnComboBox.SelectedIndex].ToString();
// if(obj!= null)
{
// ColumnComboBox.Text = obj.ToString();

isEditing = true;
base.ColumnStartedEditing((Control) sender);
}


}
protected override void ConcedeFocus()
{
ColumnComboBox.Visible = false;
}
protected override void UpdateUI(CurrencyManager source, int rowNum, string instantText)
{
//ColumnComboBox.Text = GetText(GetColumnValueAtRow(source, rowNum));
if(instantText!=null)
{
ColumnComboBox.Text = instantText;
}
}


}
  • 打赏
  • 举报
回复
人人哥 2004-04-30
使用模板列,然后编辑,加入自己想要得控件
  • 打赏
  • 举报
回复
Samen168 2004-04-30
是,自已搜
  • 打赏
  • 举报
回复
qifanghcy 2004-04-30
CSDN上相关的贴子很多,你用DataGrid为关健字搜索一下,就有好多了!
  • 打赏
  • 举报
回复
相关推荐
发帖
C#
加入

10.6w+

社区成员

.NET技术 C#
申请成为版主
帖子事件
创建了帖子
2004-04-30 09:32
社区公告

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