using System;
using System.Collections;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace AutoColumnSize {
/// <summary>
/// Windows Forms derived DataGrid that will keep the grid height
/// to row-height increments so resizing vertically will snap between
/// row-heights & never display a partial row. Rows will either be
/// fully displayed or not shown. This grid will also resize column
/// widths so that the grid will not display a horizontal scrollbar.
/// This grid will take into account whether or not the vertical
/// scrollbar is visible when it makes the column widths change.
/// </summary>
public class Grid : DataGrid {
private const int MINIMUM_COLUMN_WIDTH_DEFAULT = 10;
private static readonly object EventMinimumColumnWidthChanged = new object();
private int minimumColumnWidth = MINIMUM_COLUMN_WIDTH_DEFAULT;
private bool inEnsureTableStyleExists;
/// <summary>
/// Initializes a new instance of the AutoGrid.
/// </summary>
public Grid() {
// we have to add an event handler for the OnNavigate event
// because the DataGrid didn't make its own OnNavigate routine
// that raises the event virtual like most property event
// raising routines...
//
this.Navigate += new NavigateEventHandler(OnNavigate);
}
/// <summary>
/// tells how many pixels the border of this grid will be
/// </summary>
private int BorderWidth {
get {
if (BorderStyle == BorderStyle.Fixed3D) {
return SystemInformation.Border3DSize.Width;
}
else if (BorderStyle == BorderStyle.FixedSingle) {
return 2;
}
else {
return 0;
}
}
}
/// <summary>
/// gets or sets the data member that the grid is displaying data for.
/// </summary>
public new string DataMember {
get {
return base.DataMember;
}
set {
EnsureTableStyleExists(DataSource, value);
base.DataMember = value;
}
}
/// <summary>
/// gets or sets the data source that the grid is displaying data for.
/// </summary>
public new object DataSource {
get {
return base.DataSource;
}
set {
EnsureTableStyleExists(value, DataMember);
base.DataSource = value;
}
}
/// <summary>
/// width at which columns stop shrinking as the grid is resized. if
/// the grid's width is not sufficient enough to display all columns
/// with a width of MinimumColumnWidth, the grid will introduce a
/// horizontal scrollbar.
/// </summary>
[DefaultValue(MINIMUM_COLUMN_WIDTH_DEFAULT)]
public int MinimumColumnWidth {
get {
return minimumColumnWidth;
}
set {
if (minimumColumnWidth != value) {
minimumColumnWidth = value;
PerformLayout(this, "MinimumColumnWidth");
OnMinimumColumnWidthChanged(EventArgs.Empty);
}
}
}
/// <summary>
/// event handler for when the MinimumColumnWidth changes
/// </summary>
public event EventHandler MinimumColumnWidthChanged {
add {
Events.AddHandler(EventMinimumColumnWidthChanged, value);
}
remove {
Events.RemoveHandler(EventMinimumColumnWidthChanged, value);
}
}
/// <summary>
/// overridden to give this grid a chance to remove its event handler
/// from its own OnNavigate event.
/// </summary>
protected override void Dispose(bool disposing) {
if (disposing) {
this.Navigate -= new NavigateEventHandler(OnNavigate);
}
base.Dispose(disposing);
}
/// <summary>
/// checks to see if there is an existing tableStyle for the
/// given CurrencyManager, creating one if it does not exist yet.
/// </summary>
private void EnsureTableStyleExists(object dataSource, string dataMember) {
// requesting the BindingContext below will cause us to reenter
// this function, and we only want to do this once.
//
if (inEnsureTableStyleExists) {
return;
}
inEnsureTableStyleExists = true;
try {
if ((dataSource != null) && (BindingContext != null) && !(dataSource == Convert.DBNull)) {
CurrencyManager listManager = (CurrencyManager)BindingContext[dataSource, dataMember];
Debug.Assert(listManager != null, "BindingContext didn't give us a CurrencyManager?");
if (listManager != null) {
string listName = GetListName(dataSource, listManager);
// if we don't have a tableStyle for this list, then create one
// and add it to our collection.
//
if (!TableStyles.Contains(listName)) {
DataGridTableStyle tableStyle = new DataGridTableStyle(listManager);
TableStyles.Add(tableStyle);
}
}
}
}
finally {
inEnsureTableStyleExists = false;
}
}
/// <summary>
/// method that will use the current DataSource and DataMember to determine
/// what tableStyle the grid is currently displaying.
/// </summary>
private DataGridTableStyle GetCurrentTableStyle() {
DataGridTableStyle currentTableStyle = null;
if (DataSource != null && BindingContext != null) {
string listName = GetListName(DataSource, (CurrencyManager)BindingContext[DataSource, DataMember]);
if (TableStyles.Contains(listName)) {
currentTableStyle = TableStyles[listName];
}
}
return currentTableStyle;
}
/// <summary>
/// returns the width that columns can take up on this
/// grid with the given tableStyle
/// </summary>
private int GetFullWidth(DataGridTableStyle tableStyle) {
return Width
- 2*BorderWidth
- (VertScrollBar.Visible ? VertScrollBar.Width : 0)
- (tableStyle.RowHeadersVisible ? tableStyle.RowHeaderWidth: 0);
}
/// <summary>
/// method that duplicates internal code in WinForms.CurrencyManager because
/// almost nothing that DataBinding does is publicly exposed...
/// </summary>
private string GetListName(object dataSource, CurrencyManager listManager) {
string name = null;
IList list = listManager.List;
// if it's a typed-list, then just return the typed-list's name
//
if (list != null && list is ITypedList) {
name = ((ITypedList)listManager.List).GetListName(null);
}
else {
Type finalType = null;
object tempList = dataSource;
// otherwise we have to track down what the CurrencyManager
// stores as its "finalType" -- another internal to WinForms
// concept that we can't access directly.
//
if (tempList is Array) {
finalType = dataSource.GetType();
tempList = (Array)tempList;
}
if (tempList is IListSource) {
tempList = ((IListSource)tempList).GetList();
}
if (tempList is IList) {
if (finalType == null) {
finalType = tempList.GetType();
}
}
if (finalType != null) {
name = finalType.Name;
}
}
if (name == null) {
name = String.Empty;
}
return name;
}
/// <summary>
/// </summary>
protected override void OnBindingContextChanged(EventArgs e) {
// if our binding-context changed, then we need to ensure
// that we have a TableStyle for the new context
//
EnsureTableStyleExists(DataSource, DataMember);
base.OnBindingContextChanged(e);
}
/// <summary>
/// overridden to
/// </summary>
protected override void OnLayout(LayoutEventArgs levent) {
// first, give the DataGrid a chance to lay things out
// so we can tell if a vertical scrollbar is present
//
base.OnLayout(levent);
// now that the DataGrid is done, we need to figure out if
// there is any column resize work to be done
//
DataGridTableStyle tableStyle = GetCurrentTableStyle();
if (tableStyle != null) {
int fullWidth = GetFullWidth(tableStyle);
if (fullWidth > 0) {
bool vertScrollVisible;
int sizableColumnCount;
// it's possible that we will begin with both a vertical & horizontal scrollbar
// after the initial base.OnLayout call and that resizing the columns to make
// the horizontal scrollbar disappear will also force the vertical scrollbar
// to disappear too, and we wouldn't want to leave the area where the
// vertical scrollbar was as blank client area, so we set this up as a
// loop in case we need to lay columns out twice.
//
do {
vertScrollVisible = VertScrollBar.Visible;
sizableColumnCount = 0;
foreach (DataGridColumnStyle column in tableStyle.GridColumnStyles) {
// checkbox columns should not change size so we simply
// remove the size of that column from the overall width
// we're splitting the columns up across
//
if (column is DataGridBoolColumn) {
fullWidth -= column.Width;
}
else {
sizableColumnCount++;
}
}
if (sizableColumnCount > 0) {
int columnWidth = fullWidth / sizableColumnCount;
int remainder = fullWidth % sizableColumnCount;
int index = 0;
bool resized = false;
foreach (DataGridColumnStyle column in tableStyle.GridColumnStyles) {
if (!(column is DataGridBoolColumn)) {
int width = (index < remainder
? Math.Max(columnWidth + 1, MinimumColumnWidth)
: Math.Max(columnWidth, MinimumColumnWidth));
if (column.Width != width) {
column.Width = width;
resized = true;
}
index++;
}
}
// if we changed any column widths, we need to give the DataGrid
// a chance to lay out again so we can see if scrollbar visiblity
// changed before returning
//
if (resized) {
base.OnLayout(levent);
}
}
fullWidth = GetFullWidth(tableStyle);
} while (vertScrollVisible != VertScrollBar.Visible);
}
}
}
/// <summary>
/// called when MinimumColumnWidth changes for this grid.
/// </summary>
protected virtual void OnMinimumColumnWidthChanged(EventArgs e) {
EventHandler eh = Events[EventMinimumColumnWidthChanged] as EventHandler;
if (eh != null) {
eh(this, e);
}
}
/// <summary>
/// called when user navigates within the grid to a sub-table
/// </summary>
private void OnNavigate(object sender, NavigateEventArgs ne) {
// first, make sure a tableStyle exists for this new
// DataSource/DataMember
//
EnsureTableStyleExists(DataSource, DataMember);
// then simply force a layout so we can rearrange column widths
//
OnLayout(new LayoutEventArgs(null, null));
}
/// <summary>
/// obscures WinForms DataGrid method so we can ensure that
/// a TableStyle exists before calling the base class version.
/// </summary>
public new void SetDataBinding(object dataSource, string dataMember) {
EnsureTableStyleExists(dataSource, dataMember);
base.SetDataBinding(dataSource, dataMember);
}
}
}
USAGE:
private AutoColumnSize.Grid dataGrid1;
SqlConnection nwindConn = new SqlConnection("Data Source=localhost;Integrated Security=SSPI;Initial Catalog=northwind");
SqlCommand selectCMD = new SqlCommand("SELECT CustomerID, CompanyName FROM Customers", nwindConn);
selectCMD.CommandTimeout = 30;
SqlDataAdapter custDA = new SqlDataAdapter();
custDA.SelectCommand = selectCMD;
nwindConn.Open();
DataSet custDS = new DataSet();
custDA.Fill(custDS, "Customers");
nwindConn.Close();
if (custDS.Tables.Count > 0)
{
dataGrid1.DataSource = custDS.Tables[0];
}
3万+

被折叠的 条评论
为什么被折叠?



