C# 填充DataGridView时调用CellFormatting
我需要在DataGridView中填充数据时修剪单元格 我不知道如何从DataBindingComplete事件中调用CellFormatting事件。当然,这很简单C# 填充DataGridView时调用CellFormatting,c#,winforms,datagridview,C#,Winforms,Datagridview,我需要在DataGridView中填充数据时修剪单元格 我不知道如何从DataBindingComplete事件中调用CellFormatting事件。当然,这很简单 private void iCBOMHDataGridView_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e) { iCBOMHDataGridView_CellFormatting();
private void iCBOMHDataGridView_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
iCBOMHDataGridView_CellFormatting();
}
我正在做类似的事情,可能会有所帮助。在我的例子中,我将非活动用户的整行设置为灰色文本,但它仍然是单元格格式:
dgvUsers.CellFormatting += new System.Windows.Forms.DataGridViewCellFormattingEventHandler(this.dgvUsers_CellFormatting);
this.Controls.Add(dgvUsers);
以上几行出现在DGVuser之前。Enabled=true;网格已加载,但未显示。那么被调用的处理程序是:
private void dgvUsers_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
DataRowView row = dgvUsers.Rows[e.RowIndex].DataBoundItem as DataRowView;
if (row != null && row.Row.ItemArray[7].Equals("N"))
{
e.CellStyle.ForeColor = Color.Gray;
dgvUsers.Invalidate();
}
}
我使用的单元格样式与您使用的单元格样式之间的主要区别在于,我使用的不是单元格中的实际值,而是它们的样式。您不需要自己调用事件处理程序方法。只需将处理程序添加到事件中,事件引发时将调用该处理程序。我正在数据网格中搜索特定字符串。填充后,每个单元格的末尾都包含空格。因此,即使我可以在单元格中看到值,它也找不到字符串。一旦搜索了datagrid,它就会运行trim,但我需要在填充datagrid时进行trim谢谢您的帮助。当在DataGrid中找到一个值时,我可以让CellFormatting工作,但这需要我在查找该值的按键事件之前编辑单元格。存在的问题是我的CellFormatting的时间,它需要在填充datagridview时发生,以便可以找到值