日期:2014-05-18  浏览次数:21612 次

C# DataGridView出现滚动条显示问题
我现在有一些数据,条数不定,我现在要做的就是不管窗体显示多大,根据窗体的大小,判断DataGridView是否出现滚动条,
如果出现滚动条,就把剩下的数据显示在另一个DataGridView中,以此类推 具体实现怎么实现,

------解决方案--------------------
为什么不分页呢?如果你真心的要那样做的话,用JS控制
------解决方案--------------------
这是datagridview替换系统滚动条的代码,看懂这个实现你那个不难
C# code

private void SetupScrollBars()
        {
            // Vertical Scroll Bar Replacement
            Type t = typeof(System.Windows.Forms.DataGridView);
            FieldInfo fi = t.GetField("vertScrollBar", BindingFlags.NonPublic | BindingFlags.Instance);
            if (fi == null) return;
            System.Windows.Forms.ScrollBar sb = fi.GetValue(this) as System.Windows.Forms.ScrollBar;
            if (sb == null) return;
            //sb.Scroll += new ScrollEventHandler(sb_Scroll); return;
            MethodInfo mi = t.GetMethod("DataGridViewVScrolled", BindingFlags.NonPublic | BindingFlags.Instance);
            if (mi == null) return;


            VScrollBarEx newVSb = new VScrollBarEx();
            //newVSb.Minimum = sb.Minimum;
            //newVSb.Maximum = sb.Maximum;
            //newVSb.SmallChange = sb.SmallChange;
            //newVSb.LargeChange = sb.LargeChange;
            //newVSb.Top = sb.Top;
            //newVSb.AccessibleName = sb.AccessibleName;
            //newVSb.Left = sb.Left;
            //newVSb.Visible = sb.Visible;
            newVSb.Scroll += (ScrollEventHandler)ScrollEventHandler.CreateDelegate(typeof(ScrollEventHandler), this, mi);
            fi.SetValue(this, newVSb);
            sb.Dispose();
            this.Controls.Remove(sb);
            this.Controls.Add(newVSb);

            // Horizontal Scroll Bar Replacement
            fi = t.GetField("horizScrollBar", BindingFlags.NonPublic | BindingFlags.Instance);
            if (fi == null) return;
            sb = fi.GetValue(this) as System.Windows.Forms.ScrollBar;
            if (sb == null) return;
            mi = t.GetMethod("DataGridViewHScrolled", BindingFlags.NonPublic | BindingFlags.Instance);
            if (mi == null) return;

            HScrollBarEx newHSb = new HScrollBarEx();
            //newHSb.Minimum = sb.Minimum;
            //newHSb.Maximum = sb.Maximum;
            //newHSb.SmallChange = sb.SmallChange;
            //newHSb.LargeChange = sb.LargeChange;
            //newHSb.Top = sb.Top;
            //newHSb.AccessibleName = sb.AccessibleName;
            //newHSb.Left = sb.Left;
            //newHSb.Visible = sb.Visible;
            //newHSb.RightToLeft = sb.RightToLeft;
            newHSb.Scroll += (ScrollEventHandler)ScrollEventHandler.CreateDelegate(typeof(ScrollEventHandler), this, mi);
            fi.SetValue(this, newHSb);
            sb.Dispose();
            this.Controls.Remove(sb);
            this.Controls.Add(newHSb);
        }

------解决方案--------------------


给你一些应该有帮助的代码。
1 当前展示的第一行 2 是否出现滚动条

var rowBandInfo = typeof(DataGridView).GetField("displayedBandsInfo", BindingFlags.Instance | BindingFlags.NonPublic);
if (rowBandInfo != null)
{
var val = rowBandInfo.GetValue(dataGridView1);
//获取显示的第一行
var firstDisplayedScrollingRow = (int)rowBandInfo.FieldType.GetProperty("FirstDisplayedScrollingRow", BindingFlags.Instance | BindingFlags.Public).GetValue(val, null);
}

if(dataGridView1.DisplayedRowCount(true)<dataGridView1.Rows.Count)//出现滚动条
 { 

 }

------解决方案--------------------
很简单,一旦有滚动条必然那一行的值无法在屏幕可见
C# code
foreach (DataGridViewRow dgrv in dataGridView1.Rows)
{
     if (!dgrv.Displayed)
      {
           string v = dgrv.Cells["ColumnName"].Value.ToString();//获取列值
           //还可以知道索引
      }
      bre