DataGridView.DrawingRowNumbers

This commit is contained in:
AJ
2021-10-07 09:10:34 -04:00
parent 63fa51c5cd
commit 5af1daac11
4 changed files with 91 additions and 72 deletions

View File

@@ -86,6 +86,7 @@
<Compile Include="Controls\BinLayoutView.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="Forms\DataGridViewExtensions.cs" />
<Compile Include="Forms\MainForm.cs">
<SubType>Form</SubType>
</Compile>

View File

@@ -0,0 +1,31 @@
using System.Drawing;
using System.Windows.Forms;
namespace CutList.Forms
{
public static class DataGridViewExtensions
{
static readonly StringFormat CenterVerticallyFormat = new StringFormat
{
Alignment = StringAlignment.Far,
LineAlignment = StringAlignment.Center
};
public static void DrawingRowNumbers(this DataGridView dataGridView)
{
dataGridView.RowPostPaint += (sender, e) =>
{
var rowNumber = (e.RowIndex + 1).ToString();
var headerBounds = new Rectangle(e.RowBounds.Left, e.RowBounds.Top, dataGridView.RowHeadersWidth - 4, e.RowBounds.Height);
e.Graphics.DrawString(
rowNumber,
dataGridView.Font,
Brushes.Blue,
headerBounds,
CenterVerticallyFormat);
};
}
}
}

View File

@@ -26,6 +26,9 @@ namespace CutList.Forms
items = new BindingList<Item>();
bins = new BindingList<BinInputItem>();
dataGridView1.DrawingRowNumbers();
dataGridView2.DrawingRowNumbers();
itemBindingSource.DataSource = items;
itemBindingSource.ListChanged += ItemBindingSource_ListChanged;

View File

@@ -18,23 +18,7 @@ namespace CutList.Forms
public ResultsForm()
{
InitializeComponent();
dataGridView1.RowPostPaint += DataGridView1_RowPostPaint;
}
private void DataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
{
var grid = sender as DataGridView;
var rowIdx = (e.RowIndex + 1).ToString();
var centerFormat = new StringFormat()
{
Alignment = StringAlignment.Far,
LineAlignment = StringAlignment.Center
};
var headerBounds = new Rectangle(e.RowBounds.Left, e.RowBounds.Top, grid.RowHeadersWidth - 4, e.RowBounds.Height);
e.Graphics.DrawString(rowIdx, this.Font, Brushes.Blue, headerBounds, centerFormat);
dataGridView1.DrawingRowNumbers();
}
private void dataGridView1_RowEnter(object sender, DataGridViewCellEventArgs e)