Show message in empty cells in GridView
I am importing GridView
from excel I need to show a message next to every blank cell to give the user information about what he should write.
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 a variable msg[]
in a specific cell with a small image or number "!"
or maybe I can color the cell
source to share
You can color the XtraGrid cells using Conditional Formatting :
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:
Related links:
Customizing the display of individual lines and cells
Style formatting conditions
Custom painting (swatches)
source to share
Maybe you can use ToolTip
to show your warning:
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 DataGridView
.
ToolTip
is in the namespace System.Windows.Forms
.
source to share
Help: A Practical Guide. Providing custom text for data cells
To provide custom display text for data cells via the ColumnView.CustomColumnDisplayText Event . For more information regarding customdrawing and cell styles, see the Custom Painting Swatches , Customizing the Display of Individual Rows and Cells section of the documentation.
check example: blank rows are displayed in the cells of the "Discount" column if they contain null 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
, then you need to handle the GridView.CustomDrawCell event of your GridView, here is a snippet of code that changes the color of the Name column based on another valoe column (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);
}
}
source to share