From 5af1daac11ef846eb85a68e083dce5a4e5bdcda4 Mon Sep 17 00:00:00 2001 From: AJ Date: Thu, 7 Oct 2021 09:10:34 -0400 Subject: [PATCH] DataGridView.DrawingRowNumbers --- CutList/CutList.csproj | 1 + CutList/Forms/DataGridViewExtensions.cs | 31 ++++++ CutList/Forms/MainForm.cs | 3 + CutList/Forms/ResultsForm.cs | 128 +++++++++++------------- 4 files changed, 91 insertions(+), 72 deletions(-) create mode 100644 CutList/Forms/DataGridViewExtensions.cs diff --git a/CutList/CutList.csproj b/CutList/CutList.csproj index de13530..10b0f6a 100644 --- a/CutList/CutList.csproj +++ b/CutList/CutList.csproj @@ -86,6 +86,7 @@ Component + Form diff --git a/CutList/Forms/DataGridViewExtensions.cs b/CutList/Forms/DataGridViewExtensions.cs new file mode 100644 index 0000000..75ccdeb --- /dev/null +++ b/CutList/Forms/DataGridViewExtensions.cs @@ -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); + }; + + } + } +} diff --git a/CutList/Forms/MainForm.cs b/CutList/Forms/MainForm.cs index 2e70725..a748745 100644 --- a/CutList/Forms/MainForm.cs +++ b/CutList/Forms/MainForm.cs @@ -26,6 +26,9 @@ namespace CutList.Forms items = new BindingList(); bins = new BindingList(); + dataGridView1.DrawingRowNumbers(); + dataGridView2.DrawingRowNumbers(); + itemBindingSource.DataSource = items; itemBindingSource.ListChanged += ItemBindingSource_ListChanged; diff --git a/CutList/Forms/ResultsForm.cs b/CutList/Forms/ResultsForm.cs index 6d4791c..889f64e 100644 --- a/CutList/Forms/ResultsForm.cs +++ b/CutList/Forms/ResultsForm.cs @@ -13,100 +13,84 @@ using System.Windows.Forms; namespace CutList.Forms { - public partial class ResultsForm : Form - { - public ResultsForm() - { - InitializeComponent(); + public partial class ResultsForm : Form + { + public ResultsForm() + { + InitializeComponent(); + dataGridView1.DrawingRowNumbers(); + } - dataGridView1.RowPostPaint += DataGridView1_RowPostPaint; - } + private void dataGridView1_RowEnter(object sender, DataGridViewCellEventArgs e) + { + var selectedBin = dataGridView1.Rows[e.RowIndex].DataBoundItem as Bin; - private void DataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e) - { - var grid = sender as DataGridView; - var rowIdx = (e.RowIndex + 1).ToString(); + if (selectedBin == null) + return; - var centerFormat = new StringFormat() - { - Alignment = StringAlignment.Far, - LineAlignment = StringAlignment.Center - }; + binLayoutView1.Bin = selectedBin; + binLayoutView1.Invalidate(); - 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); - } + dataGridView2.DataSource = selectedBin.Items; + } - private void dataGridView1_RowEnter(object sender, DataGridViewCellEventArgs e) - { - var selectedBin = dataGridView1.Rows[e.RowIndex].DataBoundItem as Bin; - - if (selectedBin == null) - return; - - binLayoutView1.Bin = selectedBin; - binLayoutView1.Invalidate(); - - dataGridView2.DataSource = selectedBin.Items; - } - - public List Bins - { - get { return dataGridView1.DataSource as List; } - set { dataGridView1.DataSource = value; } - } + public List Bins + { + get { return dataGridView1.DataSource as List; } + set { dataGridView1.DataSource = value; } + } private void saveToolStripMenuItem_Click(object sender, EventArgs e) { - var s = new SaveFileDialog(); + var s = new SaveFileDialog(); - var today = DateTime.Today; - var name = $"{today.Year}-{today.Month.ToString().PadLeft(2, '0')}-{today.Day.ToString().PadLeft(2, '0')} cut list.txt"; + var today = DateTime.Today; + var name = $"{today.Year}-{today.Month.ToString().PadLeft(2, '0')}-{today.Day.ToString().PadLeft(2, '0')} cut list.txt"; - s.FileName = name; - s.Filter = "Text File|*.txt"; - - if (s.ShowDialog() != DialogResult.OK) + s.FileName = name; + s.Filter = "Text File|*.txt"; + + if (s.ShowDialog() != DialogResult.OK) { - return; + return; } - SaveBins(s.FileName); + SaveBins(s.FileName); } - private void SaveBins(string file) - { - var writer = new StreamWriter(file); - writer.AutoFlush = true; + private void SaveBins(string file) + { + var writer = new StreamWriter(file); + writer.AutoFlush = true; - var max = Bins.Max(b => b.Items.Max(i => ArchUnits.FormatFromInches(i.Length).Length)); - var id = 1; + var max = Bins.Max(b => b.Items.Max(i => ArchUnits.FormatFromInches(i.Length).Length)); + var id = 1; - foreach (var bin in Bins) - { - writer.WriteLine(id++.ToString() + ". " + bin.ToString()); + foreach (var bin in Bins) + { + writer.WriteLine(id++.ToString() + ". " + bin.ToString()); - var groups = bin.Items.GroupBy(i => $"{i.Name} {i.Length}"); + var groups = bin.Items.GroupBy(i => $"{i.Name} {i.Length}"); - foreach (var group in groups) - { - var first = group.First(); - var count = group.Count(); - var length = ArchUnits.FormatFromInches(first.Length).ToString().PadLeft(max); - var name = first.Name; + foreach (var group in groups) + { + var first = group.First(); + var count = group.Count(); + var length = ArchUnits.FormatFromInches(first.Length).ToString().PadLeft(max); + var name = first.Name; - var pcsSingularOrPlural = count == 1 ? "pc " : "pcs"; + var pcsSingularOrPlural = count == 1 ? "pc " : "pcs"; - writer.WriteLine($" {count}{pcsSingularOrPlural} @ {length} LG Tag: {name}"); - } + writer.WriteLine($" {count}{pcsSingularOrPlural} @ {length} LG Tag: {name}"); + } - writer.WriteLine("---------------------------------------------------------------------"); - //writer.WriteLine(); - } + writer.WriteLine("---------------------------------------------------------------------"); + //writer.WriteLine(); + } - writer.Close(); + writer.Close(); - Process.Start(file); - } - } + Process.Start(file); + } + } }