日期:2014-05-20  浏览次数:20846 次

急!!!!!! DataGridView中如何实现和ComboBox一样的功能
就在DataGridView中,点击某个单元格的时候会有和ComboBox一样的下拉菜单列出选择项,当焦点在该单元格上的时候能显示,当焦点不在该单元格上的时候不显示。谢谢了。

------解决方案--------------------
private void RefreshDataGrid()
{
if (da != null)
{
da.Dispose();
}
if (ds != null)
{
ds.Dispose();
}

SqlConnection conn = ManageConn.getConnection();
string sql = " ";
sql = "select sm.mmId,pm.mmName,sm.sId,sm.smName,sm.CreateDate,sm.Tooltips,case when sm.isUsed=1 then 'true ' else 'false ' end as isUsed,sm.SerialNo,sm.Description ";
sql += "from PGSubMenu as sm inner join PGMainMenu pm ON sm.mmId=pm.sId order by pm.mmName,sm.SerialNo ";
SqlCommand cmd = new SqlCommand(sql,conn);
da = new SqlDataAdapter(cmd);

ds = new DataSet();
da.Fill(ds, "PGSubMenu ");
SubMenuBindingSource.DataSource = ds;
SubMenuBindingSource.DataMember = "PGSubMenu ";

dgvSubMenu.AutoGenerateColumns = false;
dgvSubMenu.DataSource = SubMenuBindingSource;
ManageConn.freeConnection(conn);

sql = "select sId as mmId,mmName from PGMainMenu order by SerialNo ";
conn=ManageConn.getConnection();
cmd = new SqlCommand(sql, conn);
SqlDataAdapter da1 = new SqlDataAdapter(cmd);
da1.Fill(ds, "PGMainMenu ");

mmName.DataSource = ds.Tables[ "PGMainMenu "];
mmName.DisplayMember = "mmName ";
mmName.ValueMember = "mmId ";
mmName.DisplayStyleForCurrentCellOnly = true;

ManageConn.freeConnection(conn);

dgvSubMenu.Columns[0].DataPropertyName = "mmId ";//需對應為 combox.ValueMember屬性

dgvSubMenu.Columns[1].DataPropertyName = "sId ";
dgvSubMenu.Columns[2].DataPropertyName = "smName ";
dgvSubMenu.Columns[3].DataPropertyName = "CreateDate ";
dgvSubMenu.Columns[4].DataPropertyName = "Tooltips ";
dgvSubMenu.Columns[5].DataPropertyName = "isUsed ";
dgvSubMenu.Columns[6].DataPropertyName = "SerialNo ";
dgvSubMenu.Columns[7].DataPropertyName = "Description ";

isUsed.TrueValue = Boolean.Parse( "true ");
isUsed.FalseValue = Boolean.Parse( "false ");

dgvSubMenu.ReadOnly = true;
dgvSubMenu.Refresh();
ManageConn.freeConnection(conn);
}
------解决方案--------------------
((DataGridViewComboBoxColumn)this.dataGridView1.Columns[ "Column1 "]).DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing;