We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Add Column dynamically

Hi Team

1)Is possible to add column dynamically when i click in add button in mvvm pattern.

2)Is possible to get all the row values of single column based on the column name provided.

3)Is Possible to binding column with different collection(Ex. column1 is filled NameRegister, column2 to is bind with some telephone collection.

Thanks

Sankar A


3 Replies

AR Akila Rajaram Syncfusion Team May 25, 2015 09:56 AM UTC

Hi Sankar,

Thank you for using Syncfusion Products.

We analyzed your query. We have prepared a sample based on your requirement . Please find the sample from the following location:

Sample : http://www.syncfusion.com/downloads/support/forum/119209/SfDataGrid_Sample1352673260.zip

Please find the below responses as follows :

Query : Is possible to add column dynamically when i click in add button in mvvm pattern.

Yes , it is possible to add column dynamically at runtime using click event of button in command of MVVM pattern.Please refer the following code example as follows :

C#:

private static void OnAddColumn(object sender, ExecutedRoutedEventArgs args)

{

var sfDataGrid = sender as SfDataGrid;

sfDataGrid.Columns.Add(new GridNumericColumn() { MappingName = "GradePointAverage", HeaderText = "GradePointAverage" });

}



Query : Is possible to get all the row values of single column based on the column name provided.

Yes ,it is possible to get all the row values of specified column name in SfDataGrid using GetPropertyAccessProvider() helper method. Please refer the following code example as follows:

C#:

private static void OnGetRowValue(object sender, ExecutedRoutedEventArgs args)

{

var sfDataGrid = sender as SfDataGrid;

var records=sfDataGrid.View.Records;

List<object> columnvalue = new List<object>();

var provider = sfDataGrid.View.GetPropertyAccessProvider();

for (int i = 0; i < sfDataGrid.View.Records.Count; i++)

{

columnvalue.Add(provider.GetValue((records[i] as RecordEntry).Data, "Age"));

}
}


Query : Is Possible to binding column with different collection(Ex. column1 is filled NameRegister, column2 to is bind with some telephone collection.

Yes ,you can bind the column with different collection in SfDataGrid. We have to bind the StudentName and PhoneNumber two different collection in to GridComboBoxColumn as shown in the following code example:

XAML :

<!--StudentName collection with GridComboBoxColumn-->

<syncfusion:GridComboBoxColumn HeaderText="Name"

MappingName="Name"

ItemsSource="{Binding StudentName,Source={StaticResource collection}}" />


<!--PhoneNumber collection with GridComboBoxColumn-->

<syncfusion:GridComboBoxColumn HeaderText="Phone Number"

MappingName="PhoneNumer"

ItemsSource="{Binding PhoneNumber,Source={StaticResource collection}}"/>



Please let us know if this solution helps you.

Regards,
Akila R.


SA Sankar May 26, 2015 05:06 AM UTC

Dear Akila,

Thanks for your responce and effort.
These questions belongs to Dataview inside the list control. not sepertate DataGrid.
 


AR Akila Rajaram Syncfusion Team May 26, 2015 11:49 AM UTC

Hi Sankar ,

Thank you for your update.

We analyzed you requirement, but couldn’t get your requirement clearly. We thought that your requirement is about Details View. We have prepared a sample based on that. Please refer the sample from the following location :


Sample : http://www.syncfusion.com/downloads/support/forum/119209/MasterDetails_SfDataGrid_Sample2048700716.zip

Please find the response as follows :


Query : add column dynamically when i click in add button in mvvm pattern.

You can add the column dynamically in Details View by using Command of MVVM pattern. Please refer the following code example :


C#:

private static void OnAddColumn(object sender, ExecutedRoutedEventArgs args)

{

var sfDataGrid = sender as SfDataGrid;

//columns added dynamically for DetailsViewDataGrid

(sfDataGrid.DetailsViewDefinition[0] as GridViewDefinition).DataGrid.Columns.Add(new GridTextColumn() { MappingName = "ProductName", HeaderText = "Product Name" });

sfDataGrid.GetVisualContainer().InvalidateMeasure();
}


Query : Get all the row values of single column based on the column name provided.

You can get all the row values of specified column name in SfDataGrid using GetPropertyAccessProvider() helper method. Please refer the following code example as follows:


C#:

private static void OnGetRowValue(object sender, ExecutedRoutedEventArgs args)

{

var sfDataGrid = sender as SfDataGrid;

//You can get corresponding DetailsViewDataGrid ,for accessing records count of Details View

var detailsViewDataGrid = sfDataGrid.GetDetailsViewGrid(sfDataGrid.ResolveToRecordIndex(sfDataGrid.SelectionController.CurrentCellManager.CurrentCell.RowIndex), "ProductDetails");

var records = detailsViewDataGrid.View.Records;

List<object> columnvalue = new List<object>();

var provider = detailsViewDataGrid.View.GetPropertyAccessProvider();

for (int i = 0; i < detailsViewDataGrid.View.Records.Count; i++)

{

columnvalue.Add(provider.GetValue((records[i] as RecordEntry).Data, "ProductId"));

}
}


Query : Binding column with different collection(Ex. column1 is filled NameRegister, column2 to is bind with some telephone collection.

You can bind the column with different collection in SfDataGrid. We have to bind the Name and PhoneNumber two different collection in to GridComboBoxColumn as shown in the following code example:


XAML :

<syncfusion:GridComboBoxColumn MappingName="PhoneNumber" ItemsSource="{Binding PhoneNumberCollection,Source={StaticResource collection} }"/>


<syncfusion:GridComboBoxColumn MappingName="ProductNameList" ItemsSource="{Binding NameCollection,Source={StaticResource collection} }"/>


Could you please share more detailed information on this if we have misunderstood your requirement?


Please let us know if this solution helps you .

Regards,
Akila R.

Loader.
Live Chat Icon For mobile
Up arrow icon