C# DataGridView单元格在指定行和单元格中的格式设置
我试图格式化DataGridView中指定的行,但它会继续格式化DataGridView中的所有行。这就是我正在做的:C# DataGridView单元格在指定行和单元格中的格式设置,c#,visual-studio-2010,datagridview,formatting,C#,Visual Studio 2010,Datagridview,Formatting,我试图格式化DataGridView中指定的行,但它会继续格式化DataGridView中的所有行。这就是我正在做的: private void dgwParti_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) { foreach (DeParti tmp in bnsParti) { if (tmp.Arti.Type == ArtiType
private void dgwParti_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
foreach (DeParti tmp in bnsParti)
{
if (tmp.Arti.Type == ArtiType.Fast)
{
if (e.ColumnIndex == 0 || e.ColumnIndex == 3 ||
e.ColumnIndex == 8 || e.ColumnIndex == 9)
{
e.Value = "";
}
}
}
}
使用这种类型的代码,它会在所有行中将单元格值设置为“”,但我只希望在Arti类型快速时,该值为“”。任何想法。提前感谢。如果必须格式化指定的行,为什么要检查列 访问DataBoundItem(与正在格式化的行关联的对象),并根据逻辑修改值。不要直接访问绑定源。 你的代码应该是
private void dgwParti_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if ((Rows[e.RowIndex].DataBoundItem as DeParti).Arti.Type == ArtiType.Fast)
{
e.Value = "";
}
}
这将“清理”行中的所有单元格,例如,如果您希望仅在某些列上设置值=”,则可以添加检查
private void dgwParti_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if ((Rows[e.RowIndex].DataBoundItem as DeParti).Arti.Type == ArtiType.Fast
&& e.ColumnIndex == 8)
{
e.Value = "";
}
}
也许首先检查e.ColumnIndex属性会快一点。