如何在运行时更改DataGridViewComboBoxColumn的下列内容:
原因:
我这么做的原因是因为我的Enum有Status{New=1,Stop=2,Temp=3}。当我想注册一个学生时,状态总是被设置为New。所以当我保存时,它将自动保存Status = 1。
发布于 2011-03-21 03:34:58
下面是如何设置默认值和禁用单元格:
using System;
using System.Windows.Forms;
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
Column1.DataSource = new int[] { 1, 2, 3 };
Column1.DataPropertyName = "Number";
dataGridView1.DataSource = new[]
{
new { Number=1 },
new { Number=2 },
new { Number=3 },
new { Number=1 }
};
}
private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (e.ColumnIndex == Column1.Index && e.RowIndex == (dataGridView1.Rows.Count - 1))
{
DataGridViewComboBoxCell cell = (DataGridViewComboBoxCell)dataGridView1[e.ColumnIndex, e.RowIndex];
cell.Value = 2;
cell.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing;
cell.ReadOnly = true;
}
}
}
}发布于 2011-03-21 04:03:51
有SelectedIndex for DataGridView组合框控件,如下文所示:
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.editingcontrolshowing.aspx
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcomboboxeditingcontrol(v=vs.80).aspx
1.我就是这样做的:
private void dgv_EditingControlShowing(object sender, System.Windows.Forms.DataGridViewEditingControlShowingEventArgs e)
{
if (e.Control is DataGridViewComboBoxEditingControl) {
DataGridViewComboBoxEditingControl control = e.Control as DataGridViewComboBoxEditingControl;
BindingSource bs = control.DataSource as BindingSource;
if (!IsNothing(bs)) {
// set the filteredChildBS as the DataSource of the editing control
((ComboBox)e.Control).DataSource = filteredChildBS;
//Set the dgv's combobox to the first item
((ComboBox)e.Control).SelectedIndex = 1
}
}}
filtereredChildBS是一个绑定源,如果您需要澄清,请告诉我?
2.禁用datagridview可以控制其更复杂的功能。我使用这个示例禁用DataGridView复选框:http://social.msdn.microsoft.com/forums/en-US/winformsdatacontrols/thread/988c7e3f-c172-467d-89b7-b80a60b7f24f/,但是对于组合框,更容易的方法是禁用列带:
foreach (DataGridViewBand band in dgvTransactions.Columns) { if (i !=7) band.ReadOnly = (bool)i == 0; i+= 1; band.Frozen = false; }如果你需要进一步澄清的话请告诉我?
https://stackoverflow.com/questions/5373548
复制相似问题