Show message in empty cells in GridView

后端 未结 4 2014
野性不改
野性不改 2021-01-06 14:03

I\'m importing GridView from excel I need to show a message near every empty cell to give the user information about what it should be writing..



        
4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-06 14:22

    Reference: How to: Provide Custom Display Text for Data Cells

    To provide custom display text for data cells via the ColumnView.CustomColumnDisplayText event. To more information regarding customdrawing and cell styling go through Custom Painting Samples, Customizing Appearances of Individual Rows and Cells documentation section.

    check the example empty strings are displayed within the "Discount" column's cells if they contain zero values.

    using DevExpress.XtraGrid.Views.Base;
    
    private void gridView1_CustomColumnDisplayText(object sender, 
    CustomColumnDisplayTextEventArgs e) {
       if(e.Column.FieldName == "Discount")
          if(Convert.ToDecimal(e.Value) == 0) e.DisplayText = "";
    }
    

    If you want to show Image and text both then you need to handle the GridView.CustomDrawCell event of your GridView, here is a snip of code that change the color of the Name column, based on an other column valoe (age column)

    private void gridView_CustomDrawCell(object sender, RowCellCustomDrawEventArgs e)
        {
            if (e.Column == colName)
            {
                var age = Convert.ToInt32(gridView.GetRowCellValue(e.RowHandle, colAge));
                if (age < 18)
                    e.Appearance.BackColor = Color.FromArgb(0xFE, 0xDF, 0x98);
                else
                    e.Appearance.BackColor = Color.FromArgb(0xD2, 0xFD, 0x91);
            }
        }
    

提交回复
热议问题