Question

I'm currently working on upgrading a Vb6 app that used Flexgrid to C#, and one of the requirements is to have several dynamically added columns added to the end of the bound data where the user inputs data.

AllowEditing is enabled on the entire grid at present to allow me to test some things out, and i've found that i'm able to manipulate data programmatically or manually on the grid if it the column I am editing is one of the bound columns, however if I try and edit one of the unbound columns, it lets me input the value then it dissapears as soon as I leave the cell.

This is the code I have adding the Dynamic Columns and setting their data:

foreach (var O in Orders)
{
  if (!AddedOrders.Contains(O.L.Order))
  {
    c1FlexGrid1.Cols.Add(2);
    c1FlexGrid1.Cols[c1FlexGrid1.Cols.Count - 2].Caption = "Cus " + (AddedOrders.Count + 1).ToString();
    c1FlexGrid1.Cols[c1FlexGrid1.Cols.Count - 2].Name = "Cus " + (AddedOrders.Count + 1).ToString();
    c1FlexGrid1.Cols[c1FlexGrid1.Cols.Count - 2].DataType = typeof(string);
    c1FlexGrid1.Cols[c1FlexGrid1.Cols.Count - 1].Caption = "Qty " + (AddedOrders.Count + 1).ToString();
    c1FlexGrid1.Cols[c1FlexGrid1.Cols.Count - 1].Name = "Qty " + (AddedOrders.Count + 1).ToString();
    c1FlexGrid1.Cols[c1FlexGrid1.Cols.Count - 1].DataType = typeof(int);

    bool Res = c1FlexGrid1.SetData(1, c1FlexGrid1.Cols.Count - 2, O.C.Company, true);
    c1FlexGrid1.SetData(2, c1FlexGrid1.Cols.Count - 2, O.L.Order, true);
    c1FlexGrid1.SetData(3, c1FlexGrid1.Cols.Count - 2, O.L.Confirmed, true);
    c1FlexGrid1.SetData(4, c1FlexGrid1.Cols.Count - 2, O.L.Variety, true);

    c1FlexGrid1.SetData(1, c1FlexGrid1.Cols.Count - 1 , 0);

    CustCols.Add(c1FlexGrid1.Cols.Count - 2);
    AddedOrders.Add(O.L.Order);
  }
}

The bool I added was to test that the function was returning true, which it is, so as far as the code as concerned it appears as if it successfully set the value.

So in short - How to do I allow editing of unbound columns in a bound Flexgrid?

Was it helpful?

Solution

I eventually solved this, after reading through C1's incredibly unhelpful 'example' on how to use unbound fields, I put together my own code to handle it.

Since I was using lists generated from Linq, I had to first convert them to datatables, aided by 'FastMember' which makes the table generation instant (and pretty painless to code).

Then the hashtable and get/set events handle data being loading into and out of the grid, be careful not to give names to any unbound columns or the grid will fail to render:

Hashtable _hash = new Hashtable();

    private static DataTable CreateTable<T>(List<T> List)
    {
        DataTable OutTable = new DataTable();
        using (var reader = ObjectReader.Create(List))
        {
            OutTable.Load(reader);
        }
        return OutTable;
    }

    private void c1FlexGrid1_GetUnboundValue(object sender, C1.Win.C1FlexGrid.UnboundValueEventArgs e)
    {
        DataRowView drv = (DataRowView)c1FlexGrid1.Rows[e.Row].DataSource;

        e.Value = _hash[e.Row.ToString() + "|" + e.Col.ToString()];
    }

    private void c1FlexGrid1_SetUnboundValue(object sender, C1.Win.C1FlexGrid.UnboundValueEventArgs e)
    {
        DataRowView drv = (DataRowView)c1FlexGrid1.Rows[e.Row].DataSource;
        _hash[e.Row.ToString() + "|" + e.Col.ToString()] = e.Value;
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top