日期:2014-05-18 浏览次数:21612 次
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)//出现滚动条
{
}
------解决方案--------------------
很简单,一旦有滚动条必然那一行的值无法在屏幕可见
foreach (DataGridViewRow dgrv in dataGridView1.Rows) { if (!dgrv.Displayed) { string v = dgrv.Cells["ColumnName"].Value.ToString();//获取列值 //还可以知道索引 } bre