Skip to content

Commit

Permalink
Code Cleanup - Pt2
Browse files Browse the repository at this point in the history
- More databining (picturebox, progress label)
- Datagridview allows column sorting, saves sizes and positions of
columns
- Datagridview now with progressbar
- code cleanup, added more comments.
  • Loading branch information
johanneszab committed Feb 23, 2016
1 parent f167a90 commit b8d3eaf
Show file tree
Hide file tree
Showing 7 changed files with 337 additions and 179 deletions.
110 changes: 110 additions & 0 deletions TumblOne/DataGridViewExtended.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;

namespace TumblOne
{
[Description("DataGridView that Saves Column Order, Width and Visibility to user.config")]
[ToolboxBitmap(typeof(System.Windows.Forms.DataGridView))]
public class DataGridViewExtended : DataGridView
{
private void SetColumnOrder()
{
if (!DataGridViewExtendedSetting.Default.ColumnOrder.ContainsKey(this.Name))
return;

List<ColumnOrderItem> columnOrder =
DataGridViewExtendedSetting.Default.ColumnOrder[this.Name];

if (columnOrder != null)
{
var sorted = columnOrder.OrderBy(i => i.DisplayIndex);
foreach (var item in sorted)
{
this.Columns[item.ColumnIndex].DisplayIndex = item.DisplayIndex;
this.Columns[item.ColumnIndex].Visible = item.Visible;
this.Columns[item.ColumnIndex].Width = item.Width;
}
}
}

private void SaveColumnOrder()
{
if (this.AllowUserToOrderColumns)
{
List<ColumnOrderItem> columnOrder = new List<ColumnOrderItem>();
DataGridViewColumnCollection columns = this.Columns;
for (int i = 0; i < columns.Count; i++)
{
columnOrder.Add(new ColumnOrderItem
{
ColumnIndex = i,
DisplayIndex = columns[i].DisplayIndex,
Visible = columns[i].Visible,
Width = columns[i].Width
});
}

DataGridViewExtendedSetting.Default.ColumnOrder[this.Name] = columnOrder;
DataGridViewExtendedSetting.Default.Save();
}
}

protected override void OnCreateControl()
{
//base.OnCreateControl();
//SetColumnOrder();
}

public void SetOrder()
{
base.OnCreateControl();
SetColumnOrder();
}

protected override void Dispose(bool disposing)
{
SaveColumnOrder();
base.Dispose(disposing);
}
}

internal sealed class DataGridViewExtendedSetting : ApplicationSettingsBase
{
private static DataGridViewExtendedSetting _defaultInstace =
(DataGridViewExtendedSetting)ApplicationSettingsBase
.Synchronized(new DataGridViewExtendedSetting());

public static DataGridViewExtendedSetting Default
{
get { return _defaultInstace; }
}


// Because there can be more than one DGV in the user-application
// a dictionary is used to save the settings for this DGV.
// As key the name of the control is used.
[UserScopedSetting]
[SettingsSerializeAs(SettingsSerializeAs.Binary)]
[DefaultSettingValue("")]
public Dictionary<string, List<ColumnOrderItem>> ColumnOrder
{
get { return this["ColumnOrder"] as Dictionary<string, List<ColumnOrderItem>>; }
set { this["ColumnOrder"] = value; }
}
}


[Serializable]
public sealed class ColumnOrderItem
{
public int DisplayIndex { get; set; }
public int Width { get; set; }
public bool Visible { get; set; }
public int ColumnIndex { get; set; }
}
}
73 changes: 73 additions & 0 deletions TumblOne/DataGridViewProgressColumn.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel;
namespace TumblOne
{
public class DataGridViewProgressColumn : DataGridViewImageColumn
{
public DataGridViewProgressColumn()
{
CellTemplate = new DataGridViewProgressCell();
}
}
}
namespace TumblOne
{
class DataGridViewProgressCell : DataGridViewImageCell
{
// Used to make custom cell consistent with a DataGridViewImageCell
static Image emptyImage;
static DataGridViewProgressCell()
{
emptyImage = new Bitmap(1, 1, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
}
public DataGridViewProgressCell()
{
this.ValueType = typeof(int);
}
// Method required to make the Progress Cell consistent with the default Image Cell.
// The default Image Cell assumes an Image as a value, although the value of the Progress Cell is an int.
protected override object GetFormattedValue(object value,
int rowIndex, ref DataGridViewCellStyle cellStyle,
TypeConverter valueTypeConverter,
TypeConverter formattedValueTypeConverter,
DataGridViewDataErrorContexts context)
{
return emptyImage;
}
protected override void Paint(System.Drawing.Graphics g, System.Drawing.Rectangle clipBounds, System.Drawing.Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
{
try
{
int progressVal = (int)value;
float percentage = ((float)progressVal / 100.0f); // Need to convert to float before division; otherwise C# returns int which is 0 for anything but 100%.
Brush backColorBrush = new SolidBrush(cellStyle.BackColor);
Brush foreColorBrush = new SolidBrush(cellStyle.ForeColor);
// Draws the cell grid
base.Paint(g, clipBounds, cellBounds,
rowIndex, cellState, value, formattedValue, errorText,
cellStyle, advancedBorderStyle, (paintParts & ~DataGridViewPaintParts.ContentForeground));
if (percentage > 0.0)
{
// Draw the progress bar and the text
g.FillRectangle(new SolidBrush(Color.FromArgb(203, 235, 108)), cellBounds.X + 2, cellBounds.Y + 2, Convert.ToInt32((percentage * cellBounds.Width - 4)), cellBounds.Height - 4);
g.DrawString(progressVal.ToString() + "%", cellStyle.Font, foreColorBrush, cellBounds.X + (cellBounds.Width / 2) - 5, cellBounds.Y + 2);

}
else
{
// draw the text
if (this.DataGridView.CurrentRow.Index == rowIndex)
g.DrawString(progressVal.ToString() + "%", cellStyle.Font, new SolidBrush(cellStyle.SelectionForeColor), cellBounds.X + 6, cellBounds.Y + 2);
else
g.DrawString(progressVal.ToString() + "%", cellStyle.Font, foreColorBrush, cellBounds.X + 6, cellBounds.Y + 2);
}
}
catch (Exception e) { }

}
}
}
7 changes: 4 additions & 3 deletions TumblOne/Form1.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit b8d3eaf

Please sign in to comment.