how to change text in datagridview text on condition .

后端 未结 2 1891
离开以前
离开以前 2021-01-20 03:29

I am using a datagridview and I want to display do conditional formatting means when I get for a cell M then I want to display Married.I try this b

2条回答
  •  没有蜡笔的小新
    2021-01-20 03:58

    You're trying to use the .CellFormatting event with for loop statement which is not the proper way.

    The CellFormatting event occurs every time each cell is painted, so you should avoid lengthy processing when handling this event. This event also occurs when the cell FormattedValue is retrieved or its GetFormattedValue method is called.

    So every time the cell is painted, the for loop is running.

    Try this:

    private void masterDataGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
    {
        try
        {
            if (e.ColumnIndex >= 17 && e.ColumnIndex <= 24)
            {
                if (e.Value == "M")
                    e.Value = "Married";
                else
                    e.Value = "Not Married";
            }
        }
        catch (Exception ex)
        {
    
        }
    }
    

提交回复
热议问题