Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding rows to DevExpress GridView

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?

like image 883
TheGateKeeper Avatar asked Jul 02 '26 17:07

TheGateKeeper


2 Answers

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.

like image 53
Brad Rem Avatar answered Jul 04 '26 08:07

Brad Rem


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.

like image 23
kiran Avatar answered Jul 04 '26 08:07

kiran