Add new column to GridView DevExpress
I am importing a sheet from Excel. I need to add a new cell at the end of the grid containing empty cell messages where I have stored them in an array named msg;
private void simpleButton1_Click(object sender, System.EventArgs e)
{
try
{
OleDbConnection con = new OleDbConnection();
con.ConnectionString = "Provider=Microsoft.Ace.OLEDB.12.0;Data Source=C:\\Users\\pc\\Documents\\Emp.xlsx;Extended Properties=\"Excel 12.0;HDR=Yes\"";
con.Open();
DataTable dtSchema;
dtSchema = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
OleDbCommand Command = new OleDbCommand ("select * FROM [" + dtSchema.Rows[0]["TABLE_NAME"].ToString() + "]", con);
OleDbDataAdapter da = new OleDbDataAdapter(Command);
DataSet ds = new DataSet ();
da.Fill(ds);
dataGrid1.DataSource = ds.Tables[0];
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
string[] msg = new string[50];
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'";
}
cellvalue = Rows[1].ToString();
if (cellvalue == "")
{
msg[1] = "Missing 'Father Name'";
}
cellvalue = Rows[2].ToString();
if (cellvalue == "")
{
msg[2] = "Missing 'Last Name'";
}
}
I am working with XtraGrid Control .. How to add this column a add Sorry I am new to DevExpress Thanks ..
+3
source to share
1 answer
I suggest you use XtraGrid Unbound Columns where you can write your own text.
Sample code snippet for adding UnBound Column to Xtragrid:
using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraGrid.Columns;
private void Form1_Load(object sender, System.EventArgs e) {
// ...
gridControl1.ForceInitialize();
// Create an unbound column.
GridColumn unbColumn = gridView1.Columns.AddField("Total");
unbColumn.VisibleIndex = gridView1.Columns.Count;
unbColumn.UnboundType = DevExpress.Data.UnboundColumnType.Decimal;
// Disable editing.
unbColumn.OptionsColumn.AllowEdit = false;
// Specify format settings.
unbColumn.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
unbColumn.DisplayFormat.FormatString = "c";
// Customize the appearance settings.
unbColumn.AppearanceCell.BackColor = Color.LemonChiffon;
}
// Returns the total amount for a specific row.
decimal getTotalValue(int listSourceRowIndex) {
DataRow row = nwindDataSet.Tables["Order Details"].Rows[listSourceRowIndex];
decimal unitPrice = Convert.ToDecimal(row["UnitPrice"]);
decimal quantity = Convert.ToDecimal(row["Quantity"]);
decimal discount = Convert.ToDecimal(row["Discount"]);
return unitPrice * quantity * (1 - discount);
}
// Provides data for the Total column.
private void gridView1_CustomUnboundColumnData(object sender, CustomColumnDataEventArgs e) {
if (e.Column.FieldName == "Total" && e.IsGetData) e.Value =
getTotalValue(e.ListSourceRowIndex);
}
or you can use CustomColumnDisplayText to display custom Xtragrid text.
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 = "";
}
To find out how to create columns and bind them to data fields , refer to the documentation:
Map Columns and Fields Overview
using DevExpress.XtraGrid.Views.Base;
ColumnView View = gridControl1.MainView as ColumnView;
DialogResult answer = MessageBox.Show("Do you want to create columns for all fields?",
"Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (answer == DialogResult.Yes)
View.PopulateColumns();
else {
string[] fieldNames = new string[] {"ProductID", "ProductName", "QuantityPerUnit", "UnitPrice"};
DevExpress.XtraGrid.Columns.GridColumn column;
View.Columns.Clear();
for (int i = 0; i < fieldNames.Length; i++) {
column = View.Columns.AddField(fieldNames[i]);
column.VisibleIndex = i;
}
}
Hope for this help.
+6
source to share