如何在Datagride中设置下拉和checkbox列
如table1{
column1 varchar(20)
column2 varchar(10)
column3 char(1)
...
}
如果用table1中的数据绑定到datagride后,要其中column2显示为下拉,column3显示为复选框,要怎样做??
[解决办法]
http://www.syncfusion.com/FAQ/WindowsForms/FAQ_c44c.aspx#q754q
看这两篇文章
How can I put a combobox in a column of a datagrid?
How can I put a checkbox in a column of my DataGrid?
[解决办法]
以下仅为示例
public class DataGridComboBoxColumn : DataGridColumnStyle
{
private ComboBox MyComboBox = new ComboBox();
private bool isEditing;
public DataGridComboBoxColumn() : base()
{
MyComboBox.Visible = false;
}
protected override void Abort(int rowNum)
{
isEditing = false;
MyComboBox.SelectedIndexChanged -=
new EventHandler(ComboBoxSelectedIndexChanged);
Invalidate();
}
protected override bool Commit
(CurrencyManager dataSource, int rowNum)
{
MyComboBox.Bounds = Rectangle.Empty;
MyComboBox.SelectedIndexChanged -=
new EventHandler(ComboBoxSelectedIndexChanged);
if (!isEditing)
return true;
isEditing = false;
try
{
object value = MyComboBox.SelectedText;
SetColumnValueAtRow(dataSource, rowNum, value);
}
catch (Exception)
{
Abort(rowNum);
return false;
}
Invalidate();
return true;
}
protected override void Edit(
CurrencyManager source,
int rowNum,
Rectangle bounds,
bool readOnly,
string instantText,
bool cellIsVisible)
{
string value=(string)GetColumnValueAtRow(source, rowNum);
if (cellIsVisible)
{
MyComboBox.Bounds = new Rectangle
(bounds.X + 2, bounds.Y + 2,
bounds.Width - 4, bounds.Height - 4);
MyComboBox.SelectedText = value;
MyComboBox.Visible = true;
MyComboBox.SelectedIndexChanged +=
new EventHandler(ComboBoxSelectedIndexChanged);
}
else
{
MyComboBox.SelectedText = value;
MyComboBox.Visible = false;
}
if (MyComboBox.Visible)
DataGridTableStyle.DataGrid.Invalidate(bounds);
}
protected override Size GetPreferredSize(
Graphics g,
object value)
{
return new Size(100, MyComboBox.PreferredHeight + 4);
}
protected override int GetMinimumHeight()
{
return MyComboBox.PreferredHeight + 4;
}
protected override int GetPreferredHeight(Graphics g,
object value)
{
return MyComboBox.PreferredHeight + 4;
}
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)
{
DateTime date = (DateTime)
GetColumnValueAtRow(source, rowNum);
Rectangle rect = bounds;
g.FillRectangle(backBrush,rect);
rect.Offset(0, 2);
rect.Height -= 2;
g.DrawString(date.ToString( "d "),
this.DataGridTableStyle.DataGrid.Font,
foreBrush, rect);
}
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 ComboBoxSelectedIndexChanged(object sender, EventArgs e)
{
this.isEditing = true;
base.ColumnStartedEditing(MyComboBox);
}
}
[解决办法]
http://blog.csdn.net/tyouvivi/archive/2007/06/13/1650648.aspx
[解决办法]
用模板列吧
[解决办法]
需要重写一下。
codeproject有这方面的源码,
可以参考一下