Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

Trust the Universe
Posted

Hi,

 

This is a piece of the 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();
       }

Trust the Universe

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...