3

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..

void simpleButton1_Click(object sender, System.EventArgs e)
{
    string[] msg = new string[60];
    string[] error = new string[400];
    for (int i = 0; i < gridView3.RowCount ; i++)
    {
        System.Data.DataRow Rows = gridView3.GetDataRow(i);
        string cellvalue = Rows[0].ToString();
        if (cellvalue == "")
        {
            msg[0] = "Missing 'First Name'";
            error[i] = msg[0] + " - ";  
        }   
        cellvalue = Rows[1].ToString();
        if (cellvalue == "")
        {
            msg[1] = "Missing 'Last Name'";
            error[i] += msg[1] + " - ";
        }
        //...
    }
}

How can I put the variable msg[] to the specific cell with a little image or "!" figure or maybe I can color the cell

DmitryG
  • 17,677
  • 1
  • 30
  • 53
Nejthe
  • 580
  • 5
  • 10
  • 21
  • 1
    Maybe you can use a `ToolTip` – SysDragon Jan 30 '13 at 12:00
  • ASPxGridView? XtraGrid? DXGrid? have you tried posting this question in [Devex Support](http://www.devexpress.com/Support/Center) ? – Abdusalam Ben Haj Jan 30 '13 at 12:07
  • No I didn't.. I'm using XtraGrid.. not ASP.net – Nejthe Jan 30 '13 at 12:13
  • The `System.Data.DataRow` already contains a property which indicates that there's an error with the row (`System.Data.DataRow.RowError`) which is usually blank. If you would like to indicate that there's an error with the row, you can change this to the error's text. When this property is not blank, an exclamation mark should appear next to the row. Have a nice day :) – Picrofo Software Jan 30 '13 at 13:15

4 Answers4

2

To change the color of cell

Rows[1].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red);
  • I'm searching for any solution to be able to get to my point.. I need to be able to let the user know which cell need to be modified and show a message concerning this empty cell @Arun C.B you're right about what you wrote.. Thank You But i still need the message – Nejthe Jan 30 '13 at 12:10
2

You can color the XtraGrid cells using Conditional Formatting feature:

gridControl1.DataSource = new List<Person> { 
    new Person(){ Name = "John", Age = 25 },
    new Person(){ Name = "Mary", Age = 17 },
    new Person(){ Age = 17  },
    new Person(){ Name = "Ann" },
    new Person(){ Name = "Pit", Age = 5 },
};
StyleFormatCondition nameCondition = new StyleFormatCondition();
nameCondition.Column = gridView1.Columns["Name"];
nameCondition.Condition = FormatConditionEnum.Expression;
nameCondition.Expression = "IsNullOrEmpty([Name])";
nameCondition.Appearance.BackColor = Color.Red;
nameCondition.Appearance.Options.UseBackColor = true;

StyleFormatCondition ageCondition = new StyleFormatCondition();
ageCondition.Column = gridView1.Columns["Age"];
ageCondition.Condition = FormatConditionEnum.Expression;
ageCondition.Expression = "[Age]<10";
ageCondition.Appearance.BackColor = Color.Maroon;
ageCondition.Appearance.Options.UseBackColor = true;

gridView1.FormatConditions.AddRange(new StyleFormatCondition[] { 
    nameCondition, ageCondition
});

Result:
XtraGrid Conditional Formatting

Related Links:
Customizing Appearances of Individual Rows and Cells
Style Format Conditions
Custom Painting (Samples)

DmitryG
  • 17,677
  • 1
  • 30
  • 53
1

Maybe you can use a ToolTip to show your alert:

toolTip1.ToolTipIcon = ToolTipIcon.Warning;
toolTip1.ToolTipTitle = "Warning!";
toolTip1.Show("Missing 'First Name'", x, y);

You only need to guess the location of the cell based on the sizes of the rows and columns of the DataGridView.

Example1 Example2

ToolTip is in System.Windows.Forms namespace.

SysDragon
  • 9,692
  • 15
  • 60
  • 89
  • `ToolTip` is in `System.Windows.Forms` namespace. – SysDragon Jan 30 '13 at 12:31
  • I think it's a bad idea.. you know I have 50 columns I can't know the location of the cell.. But I really appreciate your help – Nejthe Jan 30 '13 at 12:35
  • You can calculate the location of the cell based on the values you can get from your `DataGridView`. You can make a function to tell you the locations. – SysDragon Jan 30 '13 at 12:40
1

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);
        }
    }
Niranjan Singh
  • 18,017
  • 2
  • 42
  • 75