Datagridview refresh problem

feurich

Centurion
Joined
Oct 21, 2003
Messages
170
Location
Holland
Hi There,

I have a datagridview control that I do the following to.
I load a dataset with data from a SQL database then I add an empty datarow as the first row. This empty row i will use for unputting the search text when the datagridview is in runtime. So you can search the database from within the datagridview if the first result isn't what you want.

Now the first row needs to be editable but the rest of the rows need to be read only. This is working but only after I performed a search in the empty row. It looks like when the control is first initialized the SetDialogDimensions sub(I have created which sets the dimensions for the dialog) isn't performed.
I have already tried Datagridview.Update en refresh but this doesn't work.
Is there any other event i can trigger to make the SetDialogDimensions work?


Thanks,

feurich
 
Hi,

This is a piece of the code.


Code:
/// <summary>
        /// Initializes a new instance of the <see cref="DatabaseDialogForm"/> class. 
        /// </summary>
        /// <param name="databaseDialog">The database dialog.</param>
        public DatabaseDialogForm(IDatabaseDialog databaseDialog)
        {
            InitializeComponent();

            this.databaseDialog = databaseDialog;
            DataGridView1.DataSource = this.databaseDialog.DataCollection.Tables[0];
            
            this.SetDialogDimensions();
            
            this.Text = databaseDialog.DialogCaption;
            this.restoreDialogDimensions();
        }

        /// <summary>
        /// Sets the state of the Database Dialog. The first row should be editable and another color
        /// the rest of the rows must be readonly and alternating color.
        /// </summary>
        private void SetDialogDimensions()
        {
            // Place a empty datarow in the DataCollection Tables 
            var dataRow = this.databaseDialog.DataCollection.Tables[0].NewRow();
            this.databaseDialog.DataCollection.Tables[0].Rows.InsertAt(dataRow, 0);
            
            // Make datagridview not sortable
            foreach (DataGridViewColumn dgvColumn in this.DataGridView1.Columns)
            {
                dgvColumn.SortMode = DataGridViewColumnSortMode.NotSortable;
            }

            // Set the first datarow to editable the rest is not!
            DataGridView1.Rows[0].DefaultCellStyle.BackColor = Color.LightYellow;
            DataGridView1.Rows[0].ReadOnly = false;

            // Make the rest of the row readonly. Starting from row 1
            for (var i = 1; i < DataGridView1.Rows.Count; i++)
            {
                DataGridView1.Rows[i].ReadOnly = true;
            }

            DataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.Honeydew;
            DataGridView1.Refresh();
        }
 
Back
Top