tylike(天外来客),可以具体些吗? 在代码的什么地方改?
解决方案 »
- C#读取XML文件问题?
- c#配置问题,请好心的人帮帮忙看下
- wm_copydata解决进程间通信问题
- .net远程调用用C#写的COM+组件的问题,如何调用?
- 寻求一本好的有关设计模式的书
- 请问怎么在程序运行过程中,执行临时读取的一个文本文件中的sql语句?
- 关于picturebox中画点连线的问题
- datagrid和combo的一个问题!!!!!!救命
- C#程序安装必须要求和相应的.NET framework版本对应吗?
- WebClient上传文件用户名密码出错
- 怎样在水晶报表中显示饼状图,其中的dataset是动态的,没有固定字段?在线等啊!!!
- SqlCommand中的SqlDataType怎么用?!
flexcell,本身就提供这样的功能,但是我看了演示,也不是真接显示的
exgrid2000功能也不错.但我还没搞懂,只能帮你这么多了
{
daEmp.Fill(ds, "Employees");
DataGrid1.DataSource = ds;
DataGrid1.DataMember = "Employees";
for(int i = 0; i < this.DataGrid1.VisibleRowCount - 1; i++)
{ //把MyCombo做成一个局部变量就可以了
//其它相应的变化自己搞定吧:)
ComboBox MyCombo=new ComboBox();
MyCombo.Name = "MyCombo";
MyCombo.Items.Add("Sales Representative");
MyCombo.Items.Add("Inside Sales Coordinator");
MyCombo.Items.Add("Vice President, Sales");
MyCombo.Items.Add("Sales Manager");
MyCombo.Items.Add("Flunky");
MyCombo.TextChanged+=new EventHandler(Ctrls_TextChanged);
MyCombo.Left = DataGrid1.GetCellBounds(i,3).Left;
MyCombo.Top = DataGrid1.GetCellBounds(i,3).Top;
MyCombo.Width = DataGrid1.GetCellBounds(i,3).Width;
MyCombo.Text = DataGrid1[i,3].ToString();
//还可以设置MyCombo的Anchor等属性
MyCombo.Visible = true; DataGrid1.Controls.Add(MyCombo);
}
}
try:ArrayList ComboBoxes = new ArrayList();private void Form1_Load(object sender, System.EventArgs e)
{
///.....这些不变
ComboBoxes.Add(MyCombo);//添加这句
DataGrid1.Controls.Add(MyCombo);
}private void DataGrid1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
for(int i = 0; i < this.DataGrid1.VisibleRowCount - 1; i++)
{
ComboBox MyCombo = ComboBoxes[i] as ComboBox;//取第i行的ComboBox
if(MyCombo != null)
{
MyCombo.Left = DataGrid1.GetCellBounds(i,3).Left;
MyCombo.Top = DataGrid1.GetCellBounds(i,3).Top;
MyCombo.Width = DataGrid1.GetCellBounds(i,3).Width;
}
}
}
int Rows;//新增变量,用来记录当前DataGrid的行数
==========================================================
private void Form1_Load(object sender, System.EventArgs e)
{
daEmp.Fill(ds, "Employees");
Rows = ds.Tables[0].Rows.Count;//其实,行数是这样取得的
DataGrid1.DataSource = ds;
DataGrid1.DataMember = "Employees"; for(int i = 0; i < Rows; i++)//不像原来那样,只对当前可见行添加ComboBox,而是对所有行如此
{
ComboBox MyCombo=new ComboBox();
MyCombo.Name = "MyCombo";
MyCombo.Items.Add("Sales Representative");
MyCombo.Items.Add("Inside Sales Coordinator");
MyCombo.Items.Add("Vice President, Sales");
MyCombo.Items.Add("Sales Manager");
MyCombo.Items.Add("Flunky");
MyCombo.TextChanged+=new EventHandler(Ctrls_TextChanged);
MyCombo.Left = DataGrid1.GetCellBounds(i,3).Left;
MyCombo.Top = DataGrid1.GetCellBounds(i,3).Top;
MyCombo.Width = DataGrid1.GetCellBounds(i,3).Width;
MyCombo.Text = DataGrid1[i,3].ToString();
MyCombo.Visible = true;
ComboBoxes.Add(MyCombo);
DataGrid1.Controls.Add(MyCombo);
}
}=============================================================
private void DataGrid1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
for(int i = firstRow; i < this.DataGrid1.VisibleRowCount + firstRow; i++)
if(i < Rows)
{
ComboBox MyCombo = ComboBoxes[i] as ComboBox;
if(MyCombo != null)
{
MyCombo.Left = DataGrid1.GetCellBounds(i,3).Left;
MyCombo.Top = DataGrid1.GetCellBounds(i,3).Top;
MyCombo.Width = DataGrid1.GetCellBounds(i,3).Width;
MyCombo.Visible = this.DataGrid1.FirstVisibleColumn <= 3;
}
}
}========================================================
DataGrid1_CurrentCellChanged 去掉
int firstRow = 0;//新增变量,用来存储当前DataGrid中的首行(我们看到的首行)
private void DataGrid1_Scroll(object sender, System.EventArgs e)
{
int y = 40;//当ColumnHeaderVisible为false的情况下40表示的是Caption部分高度与ColumnHeader高度之和,不知道有没有什么函数可以获取到这两个值.同时,这个程序不能在ColumnHeaderVisible为true的情况下运行,因为在此情况下测试老是失败:-(,如果有人知道如何处理此种情况,请给我发个短消息,谢谢~~
for(;;)
if(this.DataGrid1.GetCellBounds(firstRow,3).Y < y)
{//表示向下滚动了
ComboBox MyCombo = ComboBoxes[firstRow++] as ComboBox;
if(MyCombo != null)
MyCombo.Visible = false;
}
else if(this.DataGrid1.GetCellBounds(firstRow,3).Y > y)
{//表示向上滚动了
ComboBox MyCombo = ComboBoxes[firstRow--] as ComboBox;
if(MyCombo != null)
MyCombo.Visible = false;
}
else
break;//从死循环中跳出
}//在Scroll中我们并没有处理水平滚动,但程序对此依然有效,因为水平滚动没有改变firstRow的值.=================================================
除了上述改动,其余部分不变
{
int i = this.DataGrid1.VisibleRowCount + firstRow - 1;
if(i < Rows)
{
ComboBox MyCombo = ComboBoxes[i] as ComboBox;
if(MyCombo != null)
MyCombo.Visible = false;
}
firstRow--;
}
public class MyDataGridComboBoxColumn : DataGridTextBoxColumn
{
public NoKeyUpCombo ColumnComboBox = null;
private System.Windows.Forms.CurrencyManager _source = null;
private int _rowNum;
private bool _isEditing = false;
public MyDataGridComboBoxColumn()
{
ColumnComboBox = new NoKeyUpCombo();
ColumnComboBox.KeyPress+=new KeyPressEventHandler(Text_KeyPress);
ColumnComboBox.Leave += new EventHandler(LeaveComboBox);
ColumnComboBox.SelectedIndexChanged += new System.EventHandler(ComboIndexChanged);
ColumnComboBox.SelectionChangeCommitted += new System.EventHandler(ComboStartEditing);
}
public void MyComboValueChanged(int rowChanging, object newValue)
{
Console.WriteLine("index changed {0} {1}", rowChanging, newValue);
} private void ComboStartEditing(object sender, EventArgs e)
{
_isEditing = true;
base.ColumnStartedEditing((Control) sender);
}
private void ComboIndexChanged(object sender, EventArgs e)
{
MyComboValueChanged(_rowNum , ColumnComboBox.Text);
} private void LeaveComboBox(object sender, EventArgs e)
{
if(_isEditing)
{
SetColumnValueAtRow(_source, _rowNum, ColumnComboBox.Text);
_isEditing = false;
Invalidate();
}
ColumnComboBox.Hide();
} protected override void Edit(System.Windows.Forms.CurrencyManager source, int rowNum, System.Drawing.Rectangle bounds, bool readOnly, string instantText, bool cellIsVisible)
{
base.Edit(source,rowNum, bounds, readOnly, instantText , cellIsVisible); _rowNum = rowNum;
_source = source;
ColumnComboBox.Parent = this.TextBox.Parent;
ColumnComboBox.Location = this.TextBox.Location;
ColumnComboBox.Size = new Size(this.TextBox.Size.Width, ColumnComboBox.Size.Height);
ColumnComboBox.SelectedIndexChanged -= new System.EventHandler(ComboIndexChanged);
ColumnComboBox.Text = this.TextBox.Text;
ColumnComboBox.SelectedIndexChanged += new System.EventHandler(ComboIndexChanged); this.TextBox.Visible = false;
ColumnComboBox.Visible = true;
ColumnComboBox.BringToFront();
ColumnComboBox.Focus();
}protected override bool Commit(System.Windows.Forms.CurrencyManager dataSource, int rowNum)
{
// if(_isEditing)
// {
// _isEditing = false;
// SetColumnValueAtRow(dataSource, rowNum, ColumnComboBox.Text);
// }
// return true;
if(!_isEditing)
{
ColumnComboBox.Visible=false;
return true;
}
try
{
SetColumnValueAtRow(dataSource, rowNum, ColumnComboBox.Text);
}
catch
{
return false;
}
finally
{
_isEditing=false;
ColumnComboBox.Visible=false;
}
return true;
}
.
.
.
.
.
接下来的一些事件和方法属性什么的,你可以自己去定义然后,你用这个类
MyDataGridSearchComboBoxColumnS col11=new MyDataGridSearchComboBoxColumnS(dg);
col11.MappingName="Cause";
col11.HeaderText="原因";
col11.Width=100;
col11.EnterKeyPress +=new EventHandler(this.CellEnterPress);
public NoKeyUpCombo ColumnComboBox = null;
ColumnComboBox = new NoKeyUpCombo();
是我自己写的另外一个类
public class NoKeyUpCombo : ComboBox
{
const int WM_KEYUP = 0x101;
protected override void WndProc(ref System.Windows.Forms.Message m)
{
if(m.Msg == WM_KEYUP)
{
//ignore keyup to avoid problem with tabbing & dropdownlist;
return;
}
base.WndProc(ref m);
}
}我觉得,你还是把上面的那,改为:
public ComboBoxColumnComboBox = null;
ColumnComboBox = new ComboBox();
要让DataGrid或DataList或Repeater等对象中的ComboBox、DropDownList等对象绑定数据,你必须给你的DataGrid等对象添加事件:OnItemDataBind。然后在相应的事件处理代码中对ComboBox等对象进行其数据的绑定。
Hexudong1979(何须懂何必懂),你那个类也是要点击才可以看到 combobox的吧?
[email protected]