Прокрутите страницу до последней строки в datagridview
Запуск этого кода приводит к этой ошибке:
Необработанное исключение типа ' System.InvalidOperationException'; произошло в системе.Окна.Forms.dll
Дополнительная информация: нет места для отображения строк.
Все, что я пытаюсь сделать, это убедиться, что последняя строка в сетке отображается!
Если у вас есть какие-либо советы, пожалуйста, дайте мне знать.
using AccountsController.Properties; using SJS; using SJS.NewAccounts; using System; using System.ComponentModel; using System.Drawing; using System.Globalization; using System.Windows.Forms; namespace AccountsController.Controls { [Developer("Alan Hauxwell")] [Copyright("SarahSoft", "2016")] [History("28/11/16 Created")] public class HistoryEditor : EditorBaseControl { #region Constructor public HistoryEditor(IAccount account) : base() { if (account == null) { throw new ArgumentNullException("account"); } Account = account; BindingSource.DataSource = account.Transactions; Text = Resources.History; EditVisible = false; PanelEnabled = true; } #endregion #region Events public override event EventHandler<ListItemEventArgs> ListItemDoubleClick; #endregion #region Members private DataGridView dataView = new DataGridView(); private DataGridViewTextBoxColumn categoryColumn = new DataGridViewTextBoxColumn(); private DataGridViewTextBoxColumn dateColumn = new DataGridViewTextBoxColumn(); private DataGridViewTextBoxColumn referenceColumn = new DataGridViewTextBoxColumn(); private DataGridViewTextBoxColumn detailsColumn = new DataGridViewTextBoxColumn(); private DataGridViewTextBoxColumn folioColumn = new DataGridViewTextBoxColumn(); private DataGridViewTextBoxColumn drColumn = new DataGridViewTextBoxColumn(); private DataGridViewTextBoxColumn crColumn = new DataGridViewTextBoxColumn(); private DataGridViewTextBoxColumn balanceColumn = new DataGridViewTextBoxColumn(); #endregion #region Properties public IAccount Account { get; } /// <summary> /// Get SelectedItem property /// </summary> public ITransaction SelectedItem { get { if (dataView.Rows.Count > 0) { return dataView.SelectedRows[0].DataBoundItem as ITransaction; } return null; } } #endregion #region Raise Events private void DataViewCellDoubleClick(object sender, DataGridViewCellEventArgs e) { if (SelectedItem != null) { EventHandler<ListItemEventArgs> temp = ListItemDoubleClick; if (temp != null) temp(this, new ListItemEventArgs(SelectedItem)); } } #endregion #region Helpers private void DataViewCellFormatting(object sender, DataGridViewCellFormattingEventArgs e) { // Blank zero values in Dr and Cr columns. if (e.ColumnIndex == drColumn.Index || e.ColumnIndex == crColumn.Index) { if (Convert.ToDecimal(e.Value, CultureInfo.InvariantCulture) == 0M) { e.Value = null; } } } protected void SelectLastItem() { if (dataView.Rows.Count > 0 && Created) { dataView[0, dataView.Rows.Count - 1].Selected = true; //BindingSource.MoveLast(); dataView.FirstDisplayedScrollingRowIndex = dataView.SelectedRows[0].Index; } } #endregion #region Component Designer protected override void InitializeComponent() { base.InitializeComponent(); ((ISupportInitialize)(dataView)).BeginInit(); SuspendLayout(); DataGridViewCellStyle headerStyle = new DataGridViewCellStyle(); DataGridViewCellStyle defaultStyle = new DataGridViewCellStyle(); DataGridViewCellStyle moneyStyle = new DataGridViewCellStyle(); headerStyle.Alignment = DataGridViewContentAlignment.MiddleLeft; headerStyle.BackColor = SystemColors.Control; headerStyle.Font = new Font("Consolas", 11F, FontStyle.Regular); headerStyle.ForeColor = SystemColors.WindowText; headerStyle.SelectionBackColor = SystemColors.Highlight; headerStyle.SelectionForeColor = SystemColors.HighlightText; headerStyle.WrapMode = DataGridViewTriState.True; defaultStyle.Alignment = DataGridViewContentAlignment.TopLeft; defaultStyle.BackColor = Color.SlateBlue; defaultStyle.Font = new Font("Consolas", 10F, FontStyle.Regular); defaultStyle.ForeColor = Color.Black; defaultStyle.SelectionBackColor = SystemColors.Highlight; defaultStyle.SelectionForeColor = SystemColors.HighlightText; defaultStyle.WrapMode = DataGridViewTriState.True; moneyStyle.Alignment = DataGridViewContentAlignment.TopRight; moneyStyle.BackColor = Color.SlateBlue; moneyStyle.Font = new Font("Consolas", 10F, FontStyle.Regular); moneyStyle.ForeColor = Color.Black; moneyStyle.Format = "N2"; moneyStyle.SelectionBackColor = SystemColors.Highlight; moneyStyle.SelectionForeColor = SystemColors.HighlightText; moneyStyle.WrapMode = DataGridViewTriState.False; dataView.AllowUserToAddRows = false; dataView.AllowUserToDeleteRows = false; dataView.AllowUserToResizeColumns = false; dataView.AllowUserToResizeRows = false; dataView.AutoGenerateColumns = false; dataView.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCellsExceptHeaders; dataView.BackgroundColor = Color.SlateBlue; dataView.BorderStyle = BorderStyle.None; dataView.CellBorderStyle = DataGridViewCellBorderStyle.SingleHorizontal; dataView.ColumnHeadersDefaultCellStyle = headerStyle; dataView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize; dataView.Columns.AddRange(new DataGridViewColumn[] { categoryColumn, dateColumn, referenceColumn, detailsColumn, folioColumn, drColumn, crColumn, balanceColumn }); dataView.DataSource = BindingSource; dataView.DefaultCellStyle = defaultStyle; dataView.Dock = DockStyle.Fill; dataView.EnableHeadersVisualStyles = false; dataView.Location = new Point(0, 23); dataView.MultiSelect = false; dataView.ReadOnly = true; dataView.RowHeadersVisible = false; dataView.ScrollBars = ScrollBars.Vertical; dataView.SelectionMode = DataGridViewSelectionMode.FullRowSelect; dataView.Size = new Size(650, 358); dataView.TabIndex = 10; dataView.CellDoubleClick += new DataGridViewCellEventHandler(DataViewCellDoubleClick); dataView.CellFormatting += new DataGridViewCellFormattingEventHandler(DataViewCellFormatting); BindingSource.DataSource = typeof(TransactionCollection); categoryColumn.DataPropertyName = "Category"; categoryColumn.HeaderText = Resources.Cat; categoryColumn.ReadOnly = true; categoryColumn.Width = 70; dateColumn.DataPropertyName = "TransactionDate"; dateColumn.HeaderText = Resources.Date; dateColumn.ReadOnly = true; referenceColumn.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells; referenceColumn.DataPropertyName = "Reference"; referenceColumn.HeaderText = Resources.Reference; referenceColumn.ReadOnly = true; referenceColumn.Width = 95; detailsColumn.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; detailsColumn.DataPropertyName = "Details"; detailsColumn.HeaderText = Resources.Details; detailsColumn.ReadOnly = true; folioColumn.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; folioColumn.DataPropertyName = "Folio"; folioColumn.HeaderText = Resources.Folio; folioColumn.ReadOnly = true; drColumn.DataPropertyName = "DR"; drColumn.DefaultCellStyle = moneyStyle; drColumn.HeaderText = Resources.Debit; drColumn.ReadOnly = true; crColumn.DataPropertyName = "CR"; crColumn.DefaultCellStyle = moneyStyle; crColumn.HeaderText = Resources.Credit; crColumn.ReadOnly = true; balanceColumn.DataPropertyName = "Balance"; balanceColumn.DefaultCellStyle = moneyStyle; balanceColumn.HeaderText = Resources.Balance; balanceColumn.ReadOnly = true; PanelControls.Add(dataView); ((ISupportInitialize)(dataView)).EndInit(); ResumeLayout(false); } #endregion } }
Что я уже пробовал:
Это то, что я пробовал:
Running this code: protected void SelectLastItem() { if (dataView.Rows.Count > 0 && Created) { dataView[0, dataView.Rows.Count - 1].Selected = true; //BindingSource.MoveLast(); dataView.FirstDisplayedScrollingRowIndex = dataView.SelectedRows[0].Index; } } The line dataView.FirstDisplayedScrollingRowIndex = dataView.SelectedRows[0].Index; Causes the error.