DataGrid Control SelectedIndex Highlight Issue

Nov 21, 2011 at 2:52 AM

Hi,All.

    I've got a issue when i use the datagrid control in WPFToolkit.Follow is my step:

    1.Create Microsoft.Windows.Control.DataGrid object in csharp code and set the object's ItemsSource with DataTable.DefaultView.

    2.Add object to TabItem Control belong TabControl.

    3.Set the control's SelectedIndex with code when a DataGrid Control's SelectionChanged event action.

    Then I saw the selected row in datagrid not highlight correct.Follow is my code of create datagrid and binding data:

	/// <summary>
        /// Load table data list
        /// </summary>
        private void LoadTableDataList(string _tableName)
        {
            try
            {
                this.dgTableDataList.ItemsSource = DatabaseAccess.MySqlAccess.GetTableData(this.DatabaseConnection, _tableName).DefaultView;
                LoadReferenceTableData(_tableName);
            }
            catch
            {
                this.dgTableDataList.ItemsSource = null;
            }
        }

        /// <summary>
        /// Load table's reference table data list
        /// </summary>
        private void LoadReferenceTableData(string _tableName)
        {
            List<DataEntity.BasicInfo.TableReferenceColumns> _tableReferenceList = DatabaseAccess.SqliteAccess.GetReferenceList(this.ConfigConnection, _tableName, this.DBConnectionId);
            this.tbAssociatedTableData.Tag = _tableReferenceList;

            tbAssociatedTableData.Items.Clear();

            foreach (DataEntity.BasicInfo.TableReferenceColumns _referenceColumns in _tableReferenceList)
            {
                TabItem _tabItem = new TabItem();
                _tabItem.Header = _referenceColumns.ReferenceTableName;
                _tabItem.ToolTip = "Reference to Column:" + _referenceColumns.ColumnName;

                Microsoft.Windows.Controls.DataGrid _dataGrid = new Microsoft.Windows.Controls.DataGrid();
                //DataGrid _dataGrid = new DataGrid();
                _dataGrid.Name = _referenceColumns.ReferenceTableName;
                _dataGrid.ItemsSource = DatabaseAccess.MySqlAccess.GetTableData(this.DatabaseConnection, _referenceColumns.ReferenceTableName).DefaultView;
                _tabItem.Content = _dataGrid;

                this.tbAssociatedTableData.Items.Add(_tabItem);
            }
        }

    Follow is the code of change the selected index of datagrid control created by code:
	private void dgTableDataList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            DataRowView _dataRowView = this.dgTableDataList.SelectedItem as DataRowView;

            if (_dataRowView != null)
            {
                DataRow _dataRow = _dataRowView.Row;

                List<DataEntity.BasicInfo.TableReferenceColumns> _tableReferenceList = this.tbAssociatedTableData.Tag as List<DataEntity.BasicInfo.TableReferenceColumns>;

                if (_tableReferenceList != null)
                {
                    if (_tableReferenceList.Count > 0)
                    {
                        foreach (DataEntity.BasicInfo.TableReferenceColumns _reference in _tableReferenceList)
                        {
                            SelectReferenceData(_reference, _dataRow[_reference.ColumnName]);
                        }
                    }
                }

                this.btnTableDataEdit.IsEnabled = this.btnTableDataDelete.IsEnabled = true;
            }
            else
            {
                this.btnTableDataEdit.IsEnabled = this.btnTableDataDelete.IsEnabled = false;
            }
        }

	private void SelectReferenceData(DataEntity.BasicInfo.TableReferenceColumns _reference, object _selectVaule)
        {
            foreach (TabItem _tabItem in this.tbAssociatedTableData.Items)
            {
                if (_tabItem.Header.ToString() == _reference.ReferenceTableName)
                {
                    Microsoft.Windows.Controls.DataGrid _dataGrid = _tabItem.Content as Microsoft.Windows.Controls.DataGrid;

                    if (_dataGrid != null)
                    {
                        int _selectIndex = 0;
                        DataView _dataView = _dataGrid.ItemsSource as DataView;

                        foreach (DataRow _dataRow in _dataView.Table.Rows)
                        {
                            object _val = _dataRow[_reference.ReferenceColumnName];
                            if (_dataRow[_reference.ReferenceColumnName].ToString() != _selectVaule.ToString())
                            {
                                _selectIndex++;
                            }
                            else
                            {
                                _dataGrid.SelectedIndex = _selectIndex;

                                _dataGrid.ScrollIntoView(_dataView[_selectIndex]);

                                break;
                            }
                        }
                    }
                }
            }
        }