Reputation: 4540
Can someone please help me with this? Why is this so hard when it is so simple for the regular GridView.
I am trying to add rows programmatically. In the normal GridView I would just call the gridView.Rows and add from there, but I can't find that option here.
I tried creating a DataTable and then bind it to it, like so:
DataTable dt = new DataTable();
dt.Columns.Add("IP", Type.GetType("System.String"));
dt.Columns.Add("Port", Type.GetType("System.String"));
dt.Columns.Add("Username", Type.GetType("System.String"));
dt.Columns.Add("Password", Type.GetType("System.String"));
dt.Columns.Add("Working?", Type.GetType("System.Boolean"));
for (int i = 0; i < 20; i++)
{
DataRow dr = dt.NewRow();
dr[0] = "Test";
dr[1] = "Test";
dr[2] = "Test";
dr[3] = "Test";
dr[4] = true;
dt.Rows.Add(dr);
}
gcProxies.DataSource = dt;
All this did was add empty rows the the GridControl.
Any ideas?
Upvotes: 2
Views: 27637
Reputation: 1072
I have faced a problem similar to yours and I have resolved it by adding
gcProxies.PopulateColumns();
And one more thing the DataSource is depreciated so kindly switch to ItemSource instead.
Upvotes: 4
Reputation: 6026
Your problem was that you created a column in the designer and then your code behind didn't override it.
In addition to data binding your DataTable to the grid, you can also include unbound columns that either display arbitrary data or display data based on an expression. Here's a basic example for creating an unbound column: How to: Add an Unbound Column
In your sample, you could add the unbound column after you set your datasource:
gcProxies.DataSource = dt;
// Create an unbound column.
DevExpress.XtraGrid.Columns.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";
gridView1.CustomUnboundColumnData += gridView1_CustomUnboundColumnData;
Then, here's the event that fills in the unbound columns:
void gridView1_CustomUnboundColumnData(object sender,
DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs e)
{
if (e.Column.FieldName == "Total" && e.IsGetData)
e.Value = 100;
}
These examples should get you started to customizing your exact solution.
Upvotes: 5