自己写一个类啊,继承DataGridTextBoxColumn,然后,你再在你的应用程序中修改你的DataGrid里面的列,就是定义为MyDataGridComboBoxColumn,给你看看这个代码,不知道对你有没有用: 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); }
<asp:TemplateColumn HeaderText="用户状态">
<ItemTemplate>
<%# DataBinder.Eval(Container,"DataItem.zt")%>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList id=ddlSelectState SelectedIndex='<%# DataBinder.Eval(Container,"DataItem.zt")%>' Runat="server">
<asp:ListItem Value="0" text="验证前"></asp:ListItem>
<asp:ListItem Value="1" text="正式"></asp:ListItem>
<asp:ListItem Value="2" text="停止"></asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateColumn>//更新数据
protected void OnUpdate(object send ,DataGridCommandEventArgs e)
{
SqlConnection sqlConn=new SqlConnection(strConn);
sqlConn.Open();
DataSet ds =new DataSet();
ds=this.ReadTeacher();
int row=Convert.ToInt32 (this.dgrConfirm.CurrentPageIndex)*10+Convert.ToInt32(e.Item.ItemIndex );
DropDownList ddl;
ddl=(DropDownList)e.Item.FindControl("ddlSelectState");
string strUserName=ds.Tables[0].Rows[row]["yhm"].ToString();
string strUpdate="Update jcjsqd set zt="+Convert.ToInt32(ddl.SelectedValue)+" where yhm='"+strUserName+"'";
SqlCommand sqlComm=new SqlCommand(strUpdate,sqlConn);
sqlComm.ExecuteNonQuery();
sqlConn.Close();
ds=this.ReadTeacher();
this.dgrConfirm.EditItemIndex=-1;
this.dgrConfirm.DataSource=ds;
this.dgrConfirm.DataBind();
}
保存时,保存SelectItem.Value即可
在其间设置listitem属性就可以了
这个好像是用vs.net 2003 开发的。
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();
}
。
。
。
public event EventHandler EnterKeyPress; private void Text_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if(e.KeyChar==(char)13)
{
if(EnterKeyPress!=null)
{
EnterKeyPress(ColumnComboBox,new System.EventArgs());
}
}
} public Color Column_BackColor
{
get
{
return ColumnComboBox.BackColor;
}
set
{
ColumnComboBox.BackColor=value;
}
} public Color Column_ForeColor
{
get
{
return ColumnComboBox.ForeColor;
}
set
{
ColumnComboBox.ForeColor=value;
}
}
}
public class NoKeyUpCombo : ComboBox
{
const int WM_KEYUP = 0x101;
protected override void WndProc(ref System.Windows.Forms.Message m)
{
if(m.Msg == WM_KEYUP)
{
return;
}
base.WndProc(ref m);
}
}