Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
815
Hiding a column in a child table from .cs code
posted

Hi,

So I have a dynamical structure that I am working with, that means that the structure of my DataTables is generated by some parameters. That structure is a relational one, I am creating DataRelations and adding them to a dataset. After that I am binding one of the DataSet's tables to the datasource like this: dg.datasource = myDataSet.Tables[myIndex].DefaultView (the table has a column that is bound with another one of a different table via a DataRelation).

The problem is I am trying to hide several columns in my child table. I tried going through FieldLayout.Fields[myIndexName] (where myIndexName is the name of the column that I am trying to hide), but it doesn't work because it is not included in the grid's FieldLayout, which contains col1, col2, ..., coln, Relation1

How can I acces the fields that are behind Relation1?

I tried it also another way, setting AutoGenerateFields to false and then dynamically creating a custom fieldLayout and adding it to the grid's FieldLayouts. But it doesn't work, after binding the grid displays nothing. Here's the code, maybe I am doing something wrong:

 FieldLayout fl = new FieldLayout();

            for (int i = 0; i < ds.Tables[0].Columns.Count; i++)

            {

                Field f = new Field();

                if (ds.Tables[0].Columns[i].ColumnName == "cod" || ds.Tables[0].Columns[i].ColumnName == "tip" )

                    f.Visibility = Visibility.Collapsed;

                f.Name = ds.Tables[0].Columns[i].ColumnName;

                while(fl.Fields.Contains(f))

                {

                    f.Name = f.Name + i;

                    ds.Tables[0].Columns[i].ColumnName = f.Name + i;

                }

                fl.Fields.Add(f);

            }

            for (int i = 0; i < ds.Tables[1].Columns.Count; i++)

            {

                Field f = new Field();

                if (ds.Tables[1].Columns[i].ColumnName == "codrez")

                    f.Visibility = Visibility.Collapsed;

                f.Name = ds.Tables[1].Columns[i].ColumnName;

                while (fl.Fields.Contains(f))

                {

                    f.Name = f.Name + i;

                    ds.Tables[1].Columns[i].ColumnName = f.Name + i;

                }

                fl.Fields.Add(f);

            }

            dg.FieldLayouts.Add(fl); 

           dg.DataSource = ds.Tables[0].DefaultView;

tables 0 and 1 are linked via a DataRelation, something like 

ds.Relations.Add(dsDate.Tables[].Columns[0], dsDate.Tables[].Columns[1])

 

Can someone please tell me how to access the fields in my child table so that I can hide them? Or at least is there a way around this?

  • 69686
    Verified Answer
    posted

    Hello,

    I believe you can use the FieldLayoutInitialized event. This event will be fired when the fieldlayout is initialized. As you want to hide some fields in the child layout, or any other if you have more nested, you can use the following example:

    void xamDataGrid1_FieldLayoutInitialized(object sender, Infragistics.Windows.DataPresenter.Events.FieldLayoutInitializedEventArgs e)

            {

                if (!e.FieldLayout.IsDefault)  //any other that the first/parent layout

                {

                    e.FieldLayout.Fields[0].Visibility = Visibility.Collapsed;

                }

            }

    This will hide the first field.

    Please try this and let me know if that works in your scenario.