This is a migrated thread and some comments may be shown as answers.

According to count of columns, generate columns

1 Answer 53 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Kelum
Top achievements
Rank 2
Kelum asked on 03 May 2017, 08:35 AM
I have a requirement to generate columns once define column count on top of the grid, how can I achieve this, 
attached image file I depict the way I'm expecting this.

1 Answer, 1 is accepted

Sort by
0
Kelum
Top achievements
Rank 2
answered on 04 May 2017, 08:05 AM

for this task I did something like this 

this grid contains some predefined columns, and I'm binding data to that predefined columns initial load of this grid.
in this same grid I have button to add dynamic columns

for the button click I'm trying to add 3 dynamic columns to the existing grid.like following

private void btnAddGridColumn_Click(object sender, RoutedEventArgs e)
{
    int NumberOfColumns= 3;
    GridViewDataColumn column = new GridViewDataColumn();
    List<GridViewDataColumn> columnCollection = new List<GridViewDataColumn>();

    if (cols > 0)
    {
        for (int i = 0; i <= cols; i++)
        {
            column.Header = "ColumnName" + i;
            column.UniqueName = "ColumnName" + i;                   
            columnCollection.Add(column);

            column.DataMemberBinding = new Binding("ColumnName"+i);
            gridName.AutoGenerateColumns = false;
        }
              
        gridName.Columns.AddRange(columnCollection);

    }
}

but this is creating "ColumnName3" only others dont have Header name just existing space for that columns.

Tags
GridView
Asked by
Kelum
Top achievements
Rank 2
Answers by
Kelum
Top achievements
Rank 2
Share this question
or