PDA

View Full Version : در زمان اضافه شدن scroll bar به یک دیتاگریدویو چه رویدادی(event) اتفاق میفتد؟



amir11205
چهارشنبه 18 خرداد 1390, 09:57 صبح
سلام خدمت همه عزیزان
شاید سوالم ابتدایی باشه اما برام خیلی مهمه
در زمان اضافه شدن scroll bar به یک دیتاگریدویو چه رویدادی(event) اتفاق میفته؟
من رویداد scroll رو استفاده کردم اما این رویداد فقط زمانی اتفاق میفته که کاربر بخواد دیتاگریدو پیمایش کنه
اما من رویدادی میخوام که وقتی scroll bar به دیتاگرید بطور خودکار اضافه شد از رویداد اضافه شدن اون استفاده کنم.
باتشکر

amir11205
چهارشنبه 18 خرداد 1390, 12:13 عصر
دوستان منتظر جوابم

Reza_Yarahmadi
چهارشنبه 18 خرداد 1390, 18:48 عصر
همچین رویدادی بصورت پیشفرض وجود نداره ولی میتونید اونو به راحتی ایجاد کنید.
یک یوزر کنترل جدید درست کنید و کد زیر رو برای اون بنویسید
public partial class MyDataGridView : DataGridView
{
public MyDataGridView()
{
InitializeComponent();
this.RowsAdded += new DataGridViewRowsAddedEventHandler(MyDataGridView_R owsAdded);
this.SizeChanged += new EventHandler(MyDataGridView_SizeChanged);
}


public delegate void _ScrollAdded(object sender, EventArgs e);
public event _ScrollAdded ScrollAdded;

bool eventCalled = false;
void MyDataGridView_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
{
int height = 0;
for (int i = 0; i < this.Rows.Count; i++)
height += this.Rows[i].Height;
if (height >= this.Height - 22 && !eventCalled && this.ScrollAdded != null)
{
this.ScrollAdded(this, e);
eventCalled = true;
}
else if (height < this.Height - 22)
eventCalled = false;
}

void MyDataGridView_SizeChanged(object sender, EventArgs e)
{
int height = 0;
for (int i = 0; i < this.Rows.Count; i++)
height += this.Rows[i].Height;
if (height >= this.Height - 22 && !eventCalled && this.ScrollAdded != null)
{
this.ScrollAdded(this, e);
eventCalled = true;
}
else if (height < this.Height - 22)
eventCalled = false;
}
}حالا به جای دیتاگرید VS از یوزر کنترل خودتون استفاده کنید.