本文介绍了datacridumn datagridview替换特定值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个datagridview在我的C#程序。我绑定到一个数据源后,我想替换一些特定的列中的一些值在自动生成的行。例如我有列有0或1的值,而我要取代真或假。我应该怎么办?

解决方案

请参阅事件。



此事件被称为什么时候格式化单元格。在这个事件中你可以做的更有用的事情之一就是根据单元格的值改变单元格的内容和外观。



你将要学习代码在文档中,但我有一个下面的代码段,显示一点你可以做的。下面的CellFormatting事件检查它是否在一个名为Column3的列中格式化一个单元格,如果是单元格的值为0(作为一个字符串),它将单元格的值更改为False,单元格的BackColor更改为红色。 / p>

  private void dataGridView1_CellFormatting(object sender,DataGridViewCellFormattingEventArgs e){
if(dataGridView1.Columns [e.ColumnIndex] .Name ==Column3){
if(e.Value.ToString()==0){
e.CellStyle.BackColor = Color.Red;
e.Value =False;
}
}
}


i have a datagridview in my C# program.after i bind it to a datasource , i want to replace some value in a specific column in automatically generated rows.for example i have columns in which have 0 or 1 values , and i want to replace true or false . what should i do ?

解决方案

See the DataGridView.CellFormatting Event.

This event is called when it's time to format a cell. One of the more useful things you can do in this event is change the content and appearance of a cell based on the cell's value.

You'll want to study the code in the documentation but I have a snippet below that shows a little you can do. The CellFormatting event below checks if it's formatting a cell in a column called "Column3" and if it is and the cell's value is 0 (as a string) ,it changes the cell's value to "False" and the cell's BackColor to red.

private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) {
    if (dataGridView1.Columns[e.ColumnIndex].Name == "Column3") {
        if (e.Value.ToString() == "0") {
            e.CellStyle.BackColor = Color.Red;
            e.Value = "False";
        }
    }
}

这篇关于datacridumn datagridview替换特定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 18:21