当发生DataGrid的Scroll事件的时候你让你的ComboBox隐藏就可以了,事件执行完以后再让他到具体的位置显示
解决方案 »
- 关于nh对多数据库支持的。
- 子窗体怎样分别获取来自两个不同父窗体中DataGridView的值,急!
- C# 初用oracle 问题
- 关于DataGridVie问题?
- 求COM入门书,要求例子是C#的。
- 同步方法和异步方法的区别分享
- 服务器文件下载
- 原来连接MSSQL,现在改access数据库~~帮忙改改我的连接啊!在线等~~急,解决给分。
- 希望帮忙
- 主从表中如何获得子表当前记录
- 用SqlDataAdapter fill 一个dataset 怎么取得dataset里的值?
- 我前兩天以前程式一直還可以正常運行,可是現在居然kill()掉excel.exe時說我存取被拒,不知是為何,請教高手幫忙,在線等,急!!!!!!!
2。try grid update
combox
numberupdown等等
Public Class ComboBoxColumn
Inherits DataGridColumnStyle Private cboSelect As ComboBox
Private isEditing As Boolean Public Sub New()
cboSelect = New ComboBox
cboSelect.Visible = False
End Sub Public Sub New(ByVal cbobox As ComboBox)
cboSelect = cbobox
cboSelect.Visible = False
End Sub Protected Overrides Sub Abort(ByVal rowNum As Integer)
isEditing = False
RemoveHandler cboSelect.SelectedValueChanged, AddressOf ComboxValueChanged
Invalidate()
End Sub Protected Overrides Function Commit _
(ByVal dataSource As CurrencyManager, ByVal rowNum As Integer) _
As Boolean
cboSelect.Bounds = Rectangle.Empty AddHandler cboSelect.SelectedValueChanged, AddressOf ComboxValueChanged If Not isEditing Then
Return True
End If
isEditing = False Try
Dim value As String = cboSelect.SelectedItem SetColumnValueAtRow(dataSource, rowNum, value)
Catch
MessageBox.Show("Error", "")
End Try Invalidate()
Return True
End Function Protected Overloads Overrides Sub Edit( _
ByVal [source] As CurrencyManager, _
ByVal rowNum As Integer, _
ByVal bounds As Rectangle, _
ByVal [readOnly] As Boolean, _
ByVal instantText As String, _
ByVal cellIsVisible As Boolean) Dim value As String = GetColumnValueAtRow([source], rowNum) If cellIsVisible Then
cboSelect.Bounds = New Rectangle _
(bounds.X, bounds.Y, bounds.Width, bounds.Height) '
'cboSelect.SelectedText = value cboSelect.Text = value
cboSelect.Visible = True
AddHandler cboSelect.SelectedValueChanged, AddressOf ComboxValueChanged
Else
cboSelect.SelectedValue = value
cboSelect.Visible = False
End If If cboSelect.Visible Then
DataGridTableStyle.DataGrid.Invalidate(bounds)
End If
End Sub Protected Overrides Function GetPreferredSize( _
ByVal g As Graphics, _
ByVal value As Object) As Size Return New Size(100, cboSelect.PreferredHeight) End Function Protected Overrides Function GetMinimumHeight() As Integer Return cboSelect.PreferredHeight End Function
Protected Overrides Function GetPreferredHeight(ByVal g As Graphics, ByVal value As Object) As Integer Return cboSelect.PreferredHeight End Function
Protected Overloads Overrides Sub Paint(ByVal g As Graphics, ByVal bounds As Rectangle, ByVal [source] As CurrencyManager, ByVal rowNum As Integer)
Paint(g, bounds, [source], rowNum, False)
End Sub Protected Overloads Overrides Sub Paint(ByVal g As Graphics, ByVal bounds As Rectangle, ByVal [source] As CurrencyManager, ByVal rowNum As Integer, ByVal alignToRight As Boolean)
Paint(g, bounds, [source], rowNum, Brushes.Red, Brushes.Blue, alignToRight)
End Sub Protected Overloads Overrides Sub Paint(ByVal g As Graphics, ByVal bounds As Rectangle, ByVal [source] As CurrencyManager, ByVal rowNum As Integer, ByVal backBrush As Brush, ByVal foreBrush As Brush, ByVal alignToRight As Boolean)
Dim [date] As String = GetColumnValueAtRow([source], rowNum)
Dim rect As Rectangle = bounds
g.FillRectangle(backBrush, rect)
rect.Offset(0, 2)
rect.Height -= 2
g.DrawString([date], Me.DataGridTableStyle.DataGrid.Font, foreBrush, RectangleF.FromLTRB(rect.X, rect.Y, rect.Right, rect.Bottom))
End Sub Protected Overrides Sub SetDataGridInColumn(ByVal value As DataGrid)
MyBase.SetDataGridInColumn(value)
If Not (cboSelect.Parent Is Nothing) Then
cboSelect.Parent.Controls.Remove(cboSelect)
End If
If Not (value Is Nothing) Then
value.Controls.Add(cboSelect)
End If
End Sub Private Sub ComboxValueChanged(ByVal sender As Object, ByVal e As EventArgs)
Me.isEditing = True MyBase.ColumnStartedEditing(cboSelect)
End Sub
Protected Overrides Function GetColumnValueAtRow(ByVal source As System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer) As Object
Dim objReturn As Object = MyBase.GetColumnValueAtRow(source, rowNum)
If IsNothing(objReturn) Then
Return ""
Else
Return objReturn.ToString
End If
End Function
http://www.codeproject.com/useritems/datagrid_combobox.aspand saucer():
http://msdn.microsoft.com/msdnmag/issues/03/08/DataGrids/default.aspx
2、从其他列中按TAB或SHIFT TAB进入有自定义控件的列,会跳过一列!但用左右光标键移入,或用鼠标点入的又很正常了!
3、当有光标键将输入焦点转入有自定义控件新增行的时候会触发datagrid的leave事件!
但如果自定义控件列在DG中是第一列的时候,又不会的!
期待的高手的解答!
-------------------------------------
前几天写了一段代码,你可以试试;
public class ComboBoxColumn {
: DataGridColumnStyle private ComboBox cboSelect;
private bool isEditing; public void new() {
cboSelect = new ComboBox;
cboSelect.Visible = false;
} public void new( ComboBox cbobox) {
cboSelect = cbobox;
cboSelect.Visible = false;
} protected override void Abort( int rowNum) {
isEditing = false;
RemoveHandler cboSelect.SelectedValueChanged, delegate(GAIS) ComboxValueChanged;
Invalidate();
} protected override bool Commit ( CurrencyManager dataSource, int rowNum) {
cboSelect.Bounds = Rectangle.Empty; AddHandler cboSelect.SelectedValueChanged, delegate(GAIS) ComboxValueChanged; if (! isEditing ) {
return true;
}
isEditing = false; try {
string value = cboSelect.SelectedItem; SetColumnValueAtRow(dataSource, rowNum, value)
} catch (GAIS) {
MessageBox.Show("Error", "");
} Invalidate();
return true;
} protected override void Edit( CurrencyManager [source], int rowNum, Rectangle bounds, bool [readOnly], string instantText, bool cellIsVisible) { string value = GetColumnValueAtRow([source], rowNum); if ( cellIsVisible ) {
cboSelect.Bounds = new Rectangle (bounds.X, bounds.Y, bounds.Width, bounds.Height); //
//cboSelect.SelectedText = value cboSelect.Text = value;
cboSelect.Visible = true;
AddHandler cboSelect.SelectedValueChanged, delegate(GAIS) ComboxValueChanged;
} else {
cboSelect.SelectedValue = value;
cboSelect.Visible = false;
} if ( cboSelect.Visible ) {
DataGridTableStyle.DataGrid.Invalidate(bounds);
}
} protected override Size GetPreferredSize( Graphics g, object value) { return new Size(100, cboSelect.PreferredHeight); } protected override int GetMinimumHeight() { return cboSelect.PreferredHeight; }
protected override int GetPreferredHeight( Graphics g, object value) { return cboSelect.PreferredHeight; }
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) {
string [date] = GetColumnValueAtRow([source], rowNum);
Rectangle rect = bounds;
g.FillRectangle(backBrush, rect);
rect.Offset(0, 2);
rect.Height -= 2;
g.DrawString([date], this.DataGridTableStyle.DataGrid.Font, foreBrush, RectangleF.FromLTRB(rect.X, rect.Y, rect.Right, rect.Bottom));
} protected override void SetDataGridInColumn( DataGrid value) {
base.SetDataGridInColumn(value);
if (! (cboSelect.Parent == null) ) {
cboSelect.Parent.Controls.Remove(cboSelect);
}
if (! (value == null) ) {
value.Controls.Add(cboSelect);
}
} private void ComboxValueChanged( object sender, EventArgs e) {
this.isEditing = true; base.ColumnStartedEditing(cboSelect);
}
protected override object GetColumnValueAtRow( System.Windows.Forms.CurrencyManager source, int rowNum) {
object objReturn = base.GetColumnValueAtRow(source, rowNum);
if ( IsNothing(objReturn) ) {
return "";
} else {
return objReturn.ToString;
}
}
private void dgdFunctionArea_GotFocus(object o, EventArgs e)
{
//Create the combo control to be added and set its properties
comboControl = new ComboBox();
comboControl.Cursor = System.Windows.Forms.Cursors.Arrow;
comboControl.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDown;
comboControl.Dock = DockStyle.Fill;
comboControl.Items.AddRange(new string[5]{"","Information Technology","Computer Science","Bio Technology","Electrical Engg"}); //Create the date time picker control to be added and set its properties
DateTimePicker dtp = new DateTimePicker();
dtp.Dock = DockStyle.Fill;
dtp.Cursor = Cursors.Arrow; //Create the check box control to be added and set its properties
CheckBox chk = new CheckBox();
chk.Dock = DockStyle.Fill;
chk.Cursor = Cursors.Arrow; //Create the radio button control to be added and set its properties
RadioButton rb = new RadioButton();
rb.Dock = DockStyle.Fill;
rb.Cursor = Cursors.Arrow;
//Add the controls to the respective columns in the data grid
for(int i = 0 ;i < dataTable.Rows.Count ; i++)
{
//if the data in the first column is date time, add a date time control to the grid
if(dataGrid1[i,0].ToString().Equals("DateTime") && hitTestGrid != null && hitTestGrid.Row == i)
{
datagridtextBox.TextBox.Controls.Add(dtp);
comboControl.SendToBack();
chk.SendToBack();
rb.SendToBack();
dtp.BringToFront();
}
//if the data in the first column is combo box, add a combo box control to the grid
else if(dataGrid1[i,0].ToString().Equals("ComboBox") && hitTestGrid != null && hitTestGrid.Row == i)
{
datagridtextBox.TextBox.Controls.Add(comboControl);
chk.SendToBack();
dtp.SendToBack();
rb.SendToBack();
comboControl.BringToFront();
}
//if the data in the first column is check box, add a check box control to the grid
else if(dataGrid1[i,0].ToString().Equals("CheckBox") && hitTestGrid != null && hitTestGrid.Row == i)
{
datagridtextBox.TextBox.Controls.Add(chk);
comboControl.SendToBack();
dtp.SendToBack();
rb.SendToBack();
chk.BringToFront();
}
//if the data in the first column is radio button, add a radio button control to the grid
if(dataGrid1[i,0].ToString().Equals("Radio Button") && hitTestGrid != null && hitTestGrid.Row == i)
{
datagridtextBox.TextBox.Controls.Add(rb);
comboControl.SendToBack();
chk.SendToBack();
dtp.SendToBack();
rb.BringToFront();
}
datagridtextBox.TextBox.BackColor = Color.White;
}
}
从其他列中按TAB或SHIFT TAB进入有自定义控件的列,会跳过一列!但用左右光标键移入,或用鼠标点入的又很正常了!有解了:This behavior can be seen when you embedded a control like a textbox or combobox in your derived GridColumnStyle. If you press the tabkey slowly, you may see the cell get focus on the downkey and the cell lose focus on the upkey. One way to avoid this problem is to subclass the embedded control, and override its WndProc method, ignoring the KeyUp.
对你会有所帮助的
我还在考虑怎么把combox加到datagrid中呢