SourceGrid 开源控件,引用SourceGrid.dll 拖动控件即可。
SourceGrid 公共类库
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
Class SourceGridBase
public static void BuildStandardSourceGrid(SourceGrid.Grid grid, string title, string[] headerCaption, int[] headerLengh)
{
DevAge.Drawing.BorderLine border = new DevAge.Drawing.BorderLine(Color.DarkKhaki, 1);
DevAge.Drawing.RectangleBorder cellBorder = new DevAge.Drawing.RectangleBorder(border, border);
grid.BorderStyle = BorderStyle.FixedSingle;
grid.BorderStyle = BorderStyle.FixedSingle;
grid.SelectionMode = SourceGrid.GridSelectionMode.Row;
if (string.IsNullOrWhiteSpace(title))
{
grid.Redim(1, 15);
grid.FixedRows = 1;
}
else
{
grid.Redim(2, 15);
grid.FixedRows = 2;
BuildHeaderColumn(grid, title, 0, 0, 1, headerCaption.Length, false, Color.WhiteSmoke, Color.Black, new Font("Verdana", 10));
grid.Rows[0].Height = 25;
}
if (headerCaption != null && headerCaption.Length > 0)
BuildGridHeader(grid, headerCaption, grid.FixedRows - 1, true, Color.LemonChiffon);
if(headerLengh != null && headerLengh.Length > 0 )
BuildGridColumnWidth(grid, headerLengh);
grid.AutoStretchColumnsToFitWidth = true;
grid.Columns.StretchToFit();
grid.ClipboardMode = SourceGrid.ClipboardMode.Copy;
}
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public static void BuildGridHeader(SourceGrid.Grid Grid, string[] HeaderCaption,
int RowIndex, Boolean DefaultSorter, Color BackColor)
{
for (int i = 0; i < HeaderCaption.Length; i++)
{
BuildHeaderColumn(Grid, HeaderCaption[i].ToString(), RowIndex, i, DefaultSorter, BackColor);
}
}
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public static void BuildGridColumnWidth(SourceGrid.Grid Grid, int[] ColumnWidth)
{
for (int i = 0; i < ColumnWidth.Length; i++)
{
Grid.Columns[i].Width = ColumnWidth[i];
}
}
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public static void BindSourceGrid(SourceGrid.Grid grid, DataTable data)
{
//Redim grid
grid.Redim(data.Rows.Count + grid.FixedRows, data.Columns.Count);
for (int i = 0; i < data.Rows.Count; i++)
{
for (int j = 0; j < data.Columns.Count; j++)
{
grid[i + grid.FixedRows, j] = new SourceGrid.Cells.Cell(data.Rows[i][j]);
grid[i + grid.FixedRows, j].View = SourceGridView.NormalGridView;
}
}
grid.Refresh();
}
SourceGrid 调用方法
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
string[] ColumnNames = new string[] { "Fab Code", "Product", "Related Product", "Last Update User","Last Update Time" };
int[] ColumnWidth = new int[] { 30, 60, 100, 40,150 };
SourceGridBase.BuildStandardSourceGrid(dgrdProCfg, "Product Setting", ColumnNames, ColumnWidth);
SourceGridBase.BindSourceGrid(grid1, dataTable);