WPF DataGrid properties

Dec 22, 2009 at 4:06 AM

I am binding WPF DataGrid with a DataTable. When I try to refresh DataGrid by clearing out rows and columns of the DataTable and adding new data to DataTable and giving that as a DataContext for the DataGrid, it clears all the rows but still keeps old column headers, and it doesn't display new data. Any idea on this ?

How can we set style for a particular cell, let's say user has changed a cell's value, how do we make that cell's backcolor to yellow and make the text bold and restrict the user to enter same number of letters it previously has ?

Does DataGrid has a property like DataGrid.FinishEditing() ?

How do we get Index of the Row under the Curser for DataGrid ?

Coordinator
Dec 22, 2009 at 9:20 PM

See my answers inline

I am binding WPF DataGrid with a DataTable. When I try to refresh DataGrid by clearing out rows and columns of the DataTable and adding new data to DataTable and giving that as a DataContext for the DataGrid, it clears all the rows but still keeps old column headers, and it doesn't display new data. Any idea on this ?
[Try setting ItemsSource to null and then set ItemsSource to your new DataTable]

How can we set style for a particular cell, let's say user has changed a cell's value, how do we make that cell's backcolor to yellow and make the text bold and restrict the user to enter same number of letters it previously has ?

[I think you are looking for some kind of validation for the cell being edited. This article explains how to do row level and cell level validations. http://www.codeproject.com/KB/WPF/WPFDataGridExamples.aspx#validation]

Does DataGrid has a property like DataGrid.FinishEditing() ?

[There is an event DataGrid.CellEditEnding]

How do we get Index of the Row under the Curser for DataGrid ?

[DataGrid.CurrentItem is the item being edited. You can get its index by DataGrid.Items.IndexOf(DataGrid.CurrentItem)]

Dec 23, 2009 at 9:47 AM

To refresh the DataGrid, I have cleared all the rows and columns of the DataTable. When I try to add columns again its erroring out by saying the column already exists in the DataTable. My implementation for this case looks like the following

DataGrid.DataContext = null;
DataTable.Rows.Clear();
DataTable.Columns.Clear();
DataColumn dc = new DataColumn("Address");
DataTable.Columns.Add(dc); -- error: column with name Address already exist

Thanks,

Sandeep