怎样在DataGrid中使用下拉框等其他控件? 下拉做過,日期沒做過,研究一下應該可以.沒做過win的,如果是web,哪就用模板列來實現. 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 将要在编辑时是下拉的普通列转化成模板列,将里面的textbox设成不可视,再放个DropDownList进去,跟着在DataGird的ItemDataBound事件中写if(e.Item.ItemIndex >= 0){ if(e.Item.ItemIndex == dgbaojia.EditItemIndex) { string a = ((TextBox)e.Item.FindControl("textbox的ID")).Text; DropDownList DDL = (DropDownList)e.Item.FindControl("DropDownList的ID"); DDL.SelectedValue = a; }} using System;using System.Data;using System.Windows.Forms;using System.Drawing;namespace myControls{ public class DataGridComboBoxColumn : DataGridColumnStyle { private System.Windows.Forms.ComboBox myComboBox = new ComboBox(); private bool isEditing; private bool internalRaise=false; public DataGridComboBoxColumn() : base() { myComboBox.Visible = false; myComboBox.DropDownStyle =ComboBoxStyle.DropDownList ; //默认是不可编辑,只可拉,如果真要编辑,则另外设置 isEditing=false; myComboBox.Leave +=new EventHandler(myComboBox_Leave); } public ComboBox ComboBox { get{return myComboBox;} set{myComboBox=value;} } protected override void Abort(int rowNum) { myComboBox.SelectionChangeCommitted -= new EventHandler(ValueChanged); isEditing = false; myComboBox.Hide (); Invalidate(); } protected override bool Commit (CurrencyManager dataSource, int rowNum) { myComboBox.SelectionChangeCommitted -= new EventHandler(ValueChanged); myComboBox.Bounds = Rectangle.Empty; if (!isEditing) return true; isEditing = false; try { object value = myComboBox.SelectedValue; SetColumnValueAtRow(dataSource, rowNum, value); } catch (Exception ex) { Abort(rowNum); return false; } Invalidate(); return true; } protected override void Edit(CurrencyManager source, int rowNum,Rectangle bounds, bool readOnly,string instantText, bool cellIsVisible) { object value = GetColumnValueAtRow(source, rowNum); if (cellIsVisible) { myComboBox.Parent =this.DataGridTableStyle.DataGrid; myComboBox.Bounds =bounds; myComboBox.Size = new Size(this.Width, this.myComboBox.PreferredHeight); if (!this.internalRaise ) myComboBox.SelectedValue = value; //防止在事件中再次调用 myComboBox.Visible = (cellIsVisible == true) && (readOnly == false); myComboBox.BringToFront(); myComboBox.Focus (); myComboBox.SelectionChangeCommitted += new EventHandler(ValueChanged); } else { myComboBox.SelectedValue = value; myComboBox.Visible = false; } if (myComboBox.Visible) DataGridTableStyle.DataGrid.Invalidate(bounds); } 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 data = (object) GetColumnValueAtRow(source, rowNum); Rectangle rect = bounds; g.FillRectangle(backBrush,rect);// rect.Offset(0, 2);// rect.Height -= 2; g.DrawString(data.ToString(), this.DataGridTableStyle.DataGrid.Font, foreBrush, rect); } protected override Size GetPreferredSize(Graphics g, object value) { return new Size(100, myComboBox.PreferredHeight); } protected override int GetMinimumHeight() { return myComboBox.PreferredHeight ; } protected override int GetPreferredHeight(Graphics g,object value) { return myComboBox.PreferredHeight; } protected override void SetDataGridInColumn(DataGrid value) { //base.SetDataGridInColumn(value); if (myComboBox.Parent != null) { myComboBox.Parent.Controls.Remove (myComboBox); } if (value != null) { value.Controls.Add(myComboBox); } } private void ValueChanged(object sender, EventArgs e) { this.isEditing = true; this.internalRaise = true; base.ColumnStartedEditing(myComboBox); this.internalRaise = false; } private void myComboBox_Leave(object sender, EventArgs e) { myComboBox.Hide (); } }} C#中的小问题 大家帮忙啊 ComboBox的问题 概念性问题 C# ftp客户端和服务器,我这个有问题希望高手帮我改一下。 我使用c#当网页,如何在URL中加入用户和密码信息 遇到一个窗体公共类传值操作的问题。问下。。。 ASP.NET中调用Crystal report(C#),如何实现printtoprinter?急,代码详见内容 数据库连接出错,请问怎么回事? C++基础比较好,现在想学c#,向请教怎么转型比较好 About SqlDataAdapter 求助各位大虾!急!!! c#关于ASCII的问题
if(e.Item.ItemIndex >= 0)
{
if(e.Item.ItemIndex == dgbaojia.EditItemIndex)
{
string a = ((TextBox)e.Item.FindControl("textbox的ID")).Text;
DropDownList DDL = (DropDownList)e.Item.FindControl("DropDownList的ID");
DDL.SelectedValue = a;
}
}
using System.Data;
using System.Windows.Forms;
using System.Drawing;
namespace myControls
{
public class DataGridComboBoxColumn : DataGridColumnStyle
{
private System.Windows.Forms.ComboBox myComboBox = new ComboBox();
private bool isEditing;
private bool internalRaise=false; public DataGridComboBoxColumn() : base()
{
myComboBox.Visible = false;
myComboBox.DropDownStyle =ComboBoxStyle.DropDownList ; //默认是不可编辑,只可拉,如果真要编辑,则另外设置
isEditing=false;
myComboBox.Leave +=new EventHandler(myComboBox_Leave); } public ComboBox ComboBox
{
get{return myComboBox;}
set{myComboBox=value;}
} protected override void Abort(int rowNum)
{
myComboBox.SelectionChangeCommitted -= new EventHandler(ValueChanged);
isEditing = false;
myComboBox.Hide ();
Invalidate();
} protected override bool Commit (CurrencyManager dataSource, int rowNum)
{
myComboBox.SelectionChangeCommitted -= new EventHandler(ValueChanged);
myComboBox.Bounds = Rectangle.Empty;
if (!isEditing)
return true; isEditing = false; try
{
object value = myComboBox.SelectedValue;
SetColumnValueAtRow(dataSource, rowNum, value);
}
catch (Exception ex)
{
Abort(rowNum);
return false;
} Invalidate();
return true;
} protected override void Edit(CurrencyManager source, int rowNum,Rectangle bounds, bool readOnly,string instantText, bool cellIsVisible)
{
object value = GetColumnValueAtRow(source, rowNum);
if (cellIsVisible)
{
myComboBox.Parent =this.DataGridTableStyle.DataGrid;
myComboBox.Bounds =bounds;
myComboBox.Size = new Size(this.Width, this.myComboBox.PreferredHeight);
if (!this.internalRaise )
myComboBox.SelectedValue = value; //防止在事件中再次调用
myComboBox.Visible = (cellIsVisible == true) && (readOnly == false);
myComboBox.BringToFront();
myComboBox.Focus ();
myComboBox.SelectionChangeCommitted += new EventHandler(ValueChanged);
}
else
{
myComboBox.SelectedValue = value;
myComboBox.Visible = false;
} if (myComboBox.Visible)
DataGridTableStyle.DataGrid.Invalidate(bounds);
}
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 data = (object) GetColumnValueAtRow(source, rowNum);
Rectangle rect = bounds;
g.FillRectangle(backBrush,rect);
// rect.Offset(0, 2);
// rect.Height -= 2;
g.DrawString(data.ToString(), this.DataGridTableStyle.DataGrid.Font, foreBrush, rect);
}
protected override Size GetPreferredSize(Graphics g, object value)
{
return new Size(100, myComboBox.PreferredHeight);
} protected override int GetMinimumHeight()
{
return myComboBox.PreferredHeight ;
} protected override int GetPreferredHeight(Graphics g,object value)
{
return myComboBox.PreferredHeight;
}
protected override void SetDataGridInColumn(DataGrid value)
{
//base.SetDataGridInColumn(value);
if (myComboBox.Parent != null)
{
myComboBox.Parent.Controls.Remove (myComboBox);
}
if (value != null)
{
value.Controls.Add(myComboBox);
}
} private void ValueChanged(object sender, EventArgs e)
{
this.isEditing = true;
this.internalRaise = true;
base.ColumnStartedEditing(myComboBox);
this.internalRaise = false;
} private void myComboBox_Leave(object sender, EventArgs e)
{
myComboBox.Hide ();
}
}
}