Resolved DatagriView with different ScrollBar Control

giodepa

Member
Joined
Nov 23, 2021
Messages
17
Programming Experience
1-3
Hi all,
I found from Nuget DarkUI package where there are several controls like DarkScrollBar.
So I decided to use use this control and to bind my DataGridView with this new DarkScrollBar control.

First step.
I added DataGridView1 inside a Panel panel1.

Second step.
I disabled DataGridView ScrollBar control
DataGridView1.ScrollBars=ScrollBars.None;

Now I don't know how to proceed in order to bind DataGridView1 with DarkScrollBar DarkScrollBar1.

How can I do this?
Thank in advanced
gio
 
yes, I read but I was not able to find an example to bind ScrollBar.
I just used
C#:
DatagridView1.Controls.Add(DarkScrollBar1);

without success
 
Is there an example of replacing the scrollbar of another existing control, not necessarily the DataGridView?
 
Doing a quick scan of the source code, it looks like majority of the container type controls were written by scratch by the author to act like the WinForms controls, instead of overriding or adapting the existing controls. Looks like you'll need to also write the DataGridView from scratch, too.

Alternatively, you could just turn off the DataGridView scrollbars, and then keep the DataGridView and your dark scrollbars in sync. Look around on stack overflow on a question that asks about how to create a DataGridView with a wider vertical scrollbar. The answer given there was to just hide the DGV's scroll bar and create a wide scrollbar beside the DGV. There's code there on how they keep the two in sync.
 
Sorry for late Skydiver...
I found and I read page on stack overflow.

This is my code:
dgv:
private void bgwADJ_WorkComplete(object sender, RunWorkerCompletedEventArgs e)
        {

            grid2 = new DataGridView();
            grid2.ScrollBars = ScrollBars.Horizontal;
           
            this.grid2.Scroll += new System.Windows.Forms.ScrollEventHandler(this.grid2_Scroll);
           
            bind2 = new BindingSource();
            this.grid2.RowPrePaint += new System.Windows.Forms.DataGridViewRowPrePaintEventHandler(this.dataGridView1_RowPrePaint);
            this.grid2.RowPostPaint += new System.Windows.Forms.DataGridViewRowPostPaintEventHandler(this.dataGridView1_RowPostPaint);
            bind2.DataSource = Tadj;
            myTabPage2 = new TabPage();
            myTabPage2.BackColor = Color.FromArgb(30, 30, 30);
            myTabPage2.Name = "ADJ";
            myTabPage2.Text = "ADJ";
            grid2.DataSource = bind2;
            grid2.BackgroundColor = Color.FromArgb(30, 30, 30);
           
            darkScrollBar1.Maximum = grid2.RowCount;
            myTabPage2.Controls.Add(grid2);
            grid2.Dock = DockStyle.Fill;
            this.Invoke(new addTabPageDelegate(addTabPage), _t2, myTabPage2);

            panel3.Visible = true;
            btnSTART.Enabled = true;
        }

        private void grid2_Scroll(object sender, ScrollEventArgs e)
        {
            darkScrollBar1.Value = e.NewValue;
        }

        private void darkScrollBar1_Scroll(object sender, ScrollEventArgs e)
        {
            grid2.FirstDisplayedScrollingRowIndex = e.NewValue;
        }

My issues:
C#:
grid2.RowCount
has value 0 even if Tadj is populated. Maybe I have to use Tadj.Rows.Count instead.
The method
C#:
private void grid2_Scroll(object sender, ScrollEventArgs e)
is never called even if I created an EventHandler
There is no eventHandler in darkScrollBar1 to call method
C#:
private void darkScrollBar1_Scroll(object sender, ScrollEventArgs e)
 
Last edited:
RowCount of grid after setting DataSource is not available until DataBindingComplete event. Use source count if available.

In my experience the grids Scroll event is raised always, even when ScrollBars.None.

DarkScrollBar only has ValueChanged event.
 
Hi JohnH
I used following code to set maximum
C#:
darkScrollBar1.Maximum = TadjsReciproche.Rows.Count

Menawhile two methods mentioned above never raised :(
 
little progress...
@ JohnH you are right: grid scroll event is raised. I had to scroll the dgv in order to trigger this event. This is my mistake
Now darkScroolBar and DGV are synchronized but not perfectly..
I had following events:
C#:
private void darkScrollBar1_Click(object sender, EventArgs e)
 {
grid2.FirstDisplayedScrollingRowIndex= darkScrollBar1.Value ;
grid2.PerformLayout();
grid2.Refresh();
}

 private void darkScrollBar1_MouseDown(object sender, MouseEventArgs e)
 {
if(darkScrollBar1.Value==0) grid2.FirstDisplayedScrollingRowIndex = darkScrollBar1.Value;
else grid2.FirstDisplayedScrollingRowIndex = darkScrollBar1.Value - 1;
}

But the ScrollBar end doesn't match with the end of DGV even if I specified it : darkScrollBar1.Maximum = Tadjs.Rows.Count-1;

iimage.jpg


Moreover I added darkScrollBar MouseDown event in order to show immediately DVG rows when I move down scrollBar, but this doesn't work.
Basically only when I release mouse dvg rows are showed at correct position of scrollBar.
 
Why not just use the value changed event?
 
SKydiver you are perfectly right !!!!
with code
C#:
private void darkScrollBar1_ValueChanged(object sender, DarkUI.Controls.ScrollValueEventArgs e)
{
grid2.FirstDisplayedScrollingRowIndex = darkScrollBar1.Value;
}
Now DGV is synchronized when I move scrollBar!!!!
Now I have to find solution for ScrollBar end 😅
 
Thank @JohnH . See bottom of his post #8.
 
I don't know if it is the best solution but finally I found an example and I adapted it to move scrollBar at the end:
C#:
private void grid2_Scroll(object sender, ScrollEventArgs e)
{
grid2.FirstDisplayedScrollingRowIndex = e.NewValue;
darkScrollBar1.Value = e.NewValue;
//NEW PART
int totalHeight = 0;
foreach (DataGridViewRow row in grid2.Rows)
totalHeight += row.Height;
 if (totalHeight - grid2.Height < grid2.VerticalScrollingOffset)
  {
   darkScrollBar1.Value = darkScrollBar1.Maximum;
  }
}
 
But the ScrollBar end doesn't match with the end of DGV even if I specified it : darkScrollBar1.Maximum = Tadjs.Rows.Count-1;
You also need to set ViewSize for it to behave like the regular scrollbar, set it to number of displayed rows in DataGridView. Try something like this:
C#:
darkScrollBar1.ViewSize = dgv.Rows.GetRowCount(DataGridViewElementStates.Displayed) - 1;
 
Back
Top Bottom