im希望在DataGridView
中的任何行的特定列中放置一个DataGridViewComboBoxCell
。现在我有个问题。。。我想在我放置DataGridViewComboBoxCell
的特定列中显示一个默认值,但没有显示任何内容。。
String[] options = new String[] {"On", "Off" };
for(int i = 0; i< rows.Count; i++)
{
// lets say one row looks like this {"1","On"}, {"2", "Off"}
MydataGridView.Rows.Add(rows[i]);
DataGridViewComboBoxCell MyComboBox= new DataGridViewComboBoxCell();
MyComboBox.Items.AddRange(options);
MydataGridView.Rows[i].Cells[1] = MyComboBox;
}
在我可以通过DataGridViewComboBoxCell
选择{"On","Off"}
状态之前,我想将我添加的行显示为默认行。
我希望这已经足够清楚了
这个(DataGridView.CellFormatting Event
(怎么办:
public Form1()
{
InitializeComponent();
MydataGridView.CellFormatting += MydataGridView_CellFormatting;
}
private void MydataGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (e.ColumnIndex == 1) //Index of your DataGridViewComboBoxCell
{
e.Value = "yourDefaultValue";
}
}
此外,我还看到了以下事件,它也用于设置默认值(DataGridView.DefaultValuesNeeded Event
(:
public Form1()
{
InitializeComponent();
MydataGridView.DefaultValuesNeeded += MydataGridView_DefaultValuesNeeded;
}
private void MydataGridView_DefaultValuesNeeded(object sender, DataGridViewRowEventArgs e)
{
e.Row.Cells[1].Value = "yourDefaultValue";
}