C# Winform 自定义 DataGridView 附源码 以下代码可实现这样的效果,不解释了: public partial class PersonalAccountForm : Form { private DataGridView dataGridView = new DataGridView(); private
C#Winform
自定义DataGridView
附源码
以下代码可实现这样的效果,不解释了:
public partial class PersonalAccountForm : Form
{
private DataGridView dataGridView = new DataGridView();
private VScrollBar scrollBar = new VScrollBar();
public PersonalAccountForm()
{
InitializeComponent();
//设置scrollBar属性
scrollBar.Width = 5;
scrollBar.Maximum = dataGridView.RowCount;
scrollBar.SmallChange = 1;
scrollBar.LargeChange = 1;
scrollBar.Dock = DockStyle.Right;//设置scrollBar在父控件的右侧
scrollBar.Hide();
//scrollBar事件
scrollBar.MouseEnter += scrollBar_MouseEnter;
scrollBar.MouseLeave += scrollBar_MouseLeave;
scrollBar.Scroll += scrollBar_Scroll;
//将scrollBar添加到dataGridView中
dataGridView.Controls.Add(scrollBar);
//设置dataGridView属性
dataGridView.Dock = DockStyle.Fill;
dataGridView.ScrollBars = ScrollBars.None;
dataGridView.SelectionMode = DataGridViewSelectionMode.CellSelect;//一次选择一个单元格
dataGridView.MultiSelect = false;
dataGridView.BackgroundColor =SystemColors.Control;
dataGridView.BorderStyle = BorderStyle.None;
dataGridView.GridColor = Color.LightGray;
dataGridView.RowHeadersVisible = false;//这样左侧空白列就没有了
dataGridView.AllowUserToResizeColumns = false;
dataGridView.AllowUserToResizeRows = false;
dataGridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;//这样就禁止拖动标题行
dataGridView.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;//这样标题行内容在垂直水平方向均居中
dataGridView.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;//这样所有单元格内容在垂直水平方向均居中
dataGridView.ColumnHeadersHeight = 30;//设置列标题行高为30
dataGridView.RowTemplate.Height = 23;//设置单元格行高为23
dataGridView.ColumnHeadersDefaultCellStyle.Font = new Font("微软雅黑",9, FontStyle.Bold);
dataGridView.RowsDefaultCellStyle.Font = new Font("微软雅黑",8.2F, FontStyle.Regular);
dataGridView.ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.Single;
dataGridView.AllowUserToAddRows = false;//这样默认就没有额外的空行
//dataGridView事件
dataGridView.KeyUp += dataGridView_KeyUp;
dataGridView.MouseWheel += dataGridView_MouseWheel;
dataGridView.MouseEnter += dataGridView_MouseEnter;
//dataGridView添加两列
dataGridView.Columns.Add("column1", "项");
dataGridView.Columns.Add("column2", "花费数目");
//设置dataGridView各列的属性
dataGridView.Columns[0].Width = panel1.Width / 4;
dataGridView.Columns[1].Width = 3*panel1.Width / 4;
dataGridView.Columns[0].SortMode = DataGridViewColumnSortMode.NotSortable;//这样就禁止第一列排序
//设置第一行的属性
dataGridView.Rows.Add("1");
//将dataGridView添加到panel1中
panel1.Controls.Add(dataGridView);
}
private void scrollBar_Scroll(object sender, ScrollEventArgs e)
{
if (e.NewValue>=rowNo)
{
return;
}
dataGridView.FirstDisplayedScrollingRowIndex = e.NewValue;
}
/// <summary>
/// 鼠标进入dataGridView区域事件
/// </summary>
private void dataGridView_MouseEnter(object sender, EventArgs e)
{
scrollBar.Show();
timer.Interval = 1000;//设置时钟间隔为1s
timer.Start();
timer.Tick += timer_Tick;
}
/// <summary>
/// 鼠标离开scrollBar事件
/// </summary>
private void scrollBar_MouseLeave(object sender, EventArgs e)
{
mouseEnterScrollBar = false;
timer.Start();
}
private bool mouseEnterScrollBar = false;
/// <summary>
/// 鼠标进入scrollBar范围事件
/// </summary>
private void scrollBar_MouseEnter(object sender, EventArgs e)
{
mouseEnterScrollBar = true;
}
/// <summary>
/// 声明一计时工具
/// </summary>
private Timer timer = new Timer();
/// <summary>
/// 鼠标滑轮滚动事件
/// </summary>
private void dataGridView_MouseWheel(object sender, MouseEventArgs e)
{
scrollBar.Show();
timer.Interval = 1000;//设置时钟间隔为1s
timer.Start();
timer.Tick += timer_Tick;
}
//计数,指示当前是计时器开始后的第几秒
int count = 0;
/// <summary>
/// Timer周期事件,每1s发生一次
/// </summary>
private void timer_Tick(object sender, EventArgs e)
{
count++;
if (count>4)
{
timer.Stop();
count = 0;
if (!mouseEnterScrollBar)
{
scrollBar.Hide();
}
}
}
int rowNo = 1;//设置最后一行的编号
int scrollBarMaxinum = 0;//scrollBar的最大值
/// <summary>
/// dataGridView按键事件
/// </summary>
private void dataGridView_KeyUp(object sender, KeyEventArgs e)
{
rowNo++;
if(e.KeyValue==13)
{
dataGridView.Rows.Add(rowNo.ToString());
if (rowNo>15)
{
scrollBarMaxinum++;
scrollBar.Maximum = scrollBarMaxinum;
}
}
}
}