Adding datarows in unbound datagridview at a fixed row index

Rajkumar Rao 100 Reputation points
2023-10-27T11:26:09.9833333+00:00

I have a unbound datagridview with no rows initially ,only columns named added via designer (total 5 columns) . Now when it gets focus (via onenter event) , a row gets added . There i have been trying to add three datarows (in column1 and column 5 only) but the much bigger problem is I want that that these three datarows but always be the last rows of datagridview. regardless of the issue if some rows are added in the datagridview if count from the top row of the datagridview . Another problem is they are always adding twice . means if i adding 2 rows , there will be always 4 rows .
I have introduced a variable to stop occuring twice , but no luck .

 public bool times = true;
 protected override void OnEnter(EventArgs e)
        {
            
            if (RowCount == 0)
               
            {
                rows.Add();
                this.CurrentCell = this.Rows[0].Cells[0];
               
            }

            if (times = true)
            {
                DataGridViewRow row1 = new DataGridViewRow();
                row1.Cells.Add(new DataGridViewTextBoxCell { Value = "Data 1" });
                row1.Cells.Add(new DataGridViewTextBoxCell());
                row1.Cells.Add(new DataGridViewTextBoxCell());
                row1.Cells.Add(new DataGridViewTextBoxCell());
                row1.Cells.Add(new DataGridViewTextBoxCell { Value = "Data 2" });

                DataGridViewRow row2 = new DataGridViewRow();
                row2.Cells.Add(new DataGridViewTextBoxCell { Value = "Data 3" });
                row2.Cells.Add(new DataGridViewTextBoxCell());
                row2.Cells.Add(new DataGridViewTextBoxCell());
                row2.Cells.Add(new DataGridViewTextBoxCell());
                row2.Cells.Add(new DataGridViewTextBoxCell { Value = "Data 4" });

                DataGridViewRow row3 = new DataGridViewRow();
                row3.Cells.Add(new DataGridViewTextBoxCell { Value = "Data 5" });
                row3.Cells.Add(new DataGridViewTextBoxCell());
                row3.Cells.Add(new DataGridViewTextBoxCell());
                row3.Cells.Add(new DataGridViewTextBoxCell());
                row3.Cells.Add(new DataGridViewTextBoxCell { Value = "Data 6" });

                // Add the data rows to the DataGridView
                this.Rows.Add(row1);
                this.Rows.Add(row2);
                this.Rows.Add(row3);
                times = false;
            }

            times = false;
           

            base.OnEnter(e);
        }
Developer technologies | Windows Forms
Developer technologies | C#
Developer technologies | C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
{count} votes

2 answers

Sort by: Most helpful
  1. KOZ6.0 6,735 Reputation points
    2023-10-27T13:02:45.68+00:00

    × if (times = true)

    〇 if (times == true)

    There should be a warning


  2. KOZ6.0 6,735 Reputation points
    2023-10-28T08:02:03.4433333+00:00

    I realized that I could customize the sorting.

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Windows.Forms;
    
    internal class DataGridViewEx : DataGridView
    {
        private DataGridViewTextBoxColumn fixedSortColumn;
    
        public DataGridViewEx() { }
    
        protected override void OnCreateControl() {
            base.OnCreateControl();
            if (!DesignMode) {
                Rows.Clear();
                Columns.Clear();
                for (var i = 0; i < 5; i++) {
                    var column = new DataGridViewTextBoxColumn();
                    column.HeaderText = $"column{i + 1}";
                    Columns.Add(column);
                }
                fixedSortColumn = new DataGridViewTextBoxColumn();
                fixedSortColumn.SortMode = DataGridViewColumnSortMode.Programmatic;
                fixedSortColumn.ValueType = typeof(int);
                fixedSortColumn.Visible = false;
                Columns.Add(fixedSortColumn);
            }
        }
    
        private bool times = true;
    
        protected override void OnEnter(EventArgs e) {
            if (RowCount == 0) {
                Rows.Add("", "", "", "", "", 0);
                CurrentCell = Rows[0].Cells[0];
            }
            if (times == true) {
                times = false;
                Rows.Add("Data 1", "", "", "", "Data 2", long.MaxValue - 2);
                Rows.Add("Data 3", "", "", "", "Data 4", long.MaxValue - 1);
                Rows.Add("Data 5", "", "", "", "Data 6", long.MaxValue - 0);
            }
            base.OnEnter(e);
        }
    
        protected override void OnRowsAdded(DataGridViewRowsAddedEventArgs e) {
            base.OnRowsAdded(e);
            if (fixedSortColumn != null) {
                Sort(fixedSortColumn, ListSortDirection.Ascending);
            }
        }
    
        protected override void OnSortCompare(DataGridViewSortCompareEventArgs e) {
            if (e.Column.Index != fixedSortColumn.Index) {
                var fixedX = GetFixedSortValue(e.RowIndex1);
                var fixedY = GetFixedSortValue(e.RowIndex2);
                var fixedComparer = Comparer<long>.Default;
                if (fixedX != 0 || fixedY != 0) {
                    e.Handled = true;
                    switch (SortOrder) {
                        case SortOrder.Ascending:
                            e.SortResult = fixedComparer.Compare(fixedX, fixedY);
                            return;
                        case SortOrder.Descending:
                            e.SortResult = fixedComparer.Compare(fixedY, fixedX);
                            return;
                        default:
                            e.SortResult = fixedComparer.Compare(fixedX, fixedY);
                            return;
                    }
                }
            }
            base.OnSortCompare(e);
        }
    
        private long GetFixedSortValue(int rowIndex) {
            var value = Rows[rowIndex].Cells[fixedSortColumn.Index].Value ?? 0;
            switch (value) {
                case long.MaxValue - 2:
                case long.MaxValue - 1:
                case long.MaxValue - 0:
                    return (long)value;
            }
            if (SortedColumn.Equals(fixedSortColumn)) {
                return rowIndex;
            }
            return 0;
        }
    }
    

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.