本文整理汇总了C#中System.Windows.Forms.DataGrid.CreateGraphics方法的典型用法代码示例。如果您正苦于以下问题:C# DataGrid.CreateGraphics方法的具体用法?C# DataGrid.CreateGraphics怎么用?C# DataGrid.CreateGraphics使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Windows.Forms.DataGrid
的用法示例。
在下文中一共展示了DataGrid.CreateGraphics方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: DataGridComboBoxColumn
/// <summary>
/// The only creator ;)
/// </summary>
/// <param name="colName">The name of the column</param>
/// <param name="dataSource">The datasource that contains the lookup table</param>
/// <param name="displayMember">The member of the lookuptable to display</param>
/// <param name="valueMember">The member of the lookuptable with the value</param>
/// <param name="dataGrid">The datagrid parent of this column</param>
public DataGridComboBoxColumn(string colName, DataTable dataSource, string displayMember, string valueMember, DataGrid dataGrid)
{
_comboBox = new ComboBox();
_comboBox.Visible = false;
_comboBox.DataSource = dataSource;
_dataTable = dataSource;
_comboBox.DisplayMember = displayMember;
_displayMember = displayMember;
_valueMember = valueMember;
_comboBox.ValueMember = valueMember;
_comboBox.DropDownStyle = ComboBoxStyle.DropDownList;
Graphics _graphicsContext = dataGrid.CreateGraphics();
float _widest = 0;
SizeF _stringSize = new SizeF(0, 0);
foreach (DataRow dr in dataSource.Rows)
{
_stringSize = _graphicsContext.MeasureString(dr[displayMember].ToString(), dataGrid.Font);
if (_stringSize.Width > _widest)
{
_widest = _stringSize.Width;
}
}
_comboBox.DropDownWidth = (int)Math.Ceiling(_widest);
this.Width = _comboBox.DropDownWidth + 25; // Add the space for the dropdown arrow
this.MappingName = colName;
this.HeaderText = colName;
dataGrid.Controls.Add(_comboBox);
}
示例2: SizeColumnsToContent
public void SizeColumnsToContent(DataGrid dataGrid)
{
Graphics Graphics = dataGrid.CreateGraphics();
DataGridTableStyle tableStyle = new DataGridTableStyle();
DataTable dataTable = (DataTable)dataGrid.DataSource;
dataGrid.TableStyles.Clear();
tableStyle.MappingName = dataTable.TableName;
DataGridTextBoxColumn columnStyle;
DataColumn dataColumn = dataTable.Columns[0];
columnStyle = new DataGridTextBoxColumn();
columnStyle.HeaderText = dataColumn.ColumnName;
columnStyle.MappingName = dataColumn.ColumnName;
columnStyle.Width = 30;
tableStyle.GridColumnStyles.Add(columnStyle);
dataColumn = dataTable.Columns[1];
columnStyle = new DataGridTextBoxColumn();
columnStyle.HeaderText = dataColumn.ColumnName;
columnStyle.MappingName = dataColumn.ColumnName;
columnStyle.Width = 60;
tableStyle.GridColumnStyles.Add(columnStyle);
dataColumn = dataTable.Columns[2];
columnStyle = new DataGridTextBoxColumn();
columnStyle.HeaderText = dataColumn.ColumnName;
columnStyle.MappingName = dataColumn.ColumnName;
columnStyle.Width = 60;
tableStyle.GridColumnStyles.Add(columnStyle);
dataColumn = dataTable.Columns[3];
columnStyle = new DataGridTextBoxColumn();
columnStyle.HeaderText = dataColumn.ColumnName;
columnStyle.MappingName = dataColumn.ColumnName;
columnStyle.Width = 60;
tableStyle.GridColumnStyles.Add(columnStyle);
dataColumn = dataTable.Columns[4];
columnStyle = new DataGridTextBoxColumn();
columnStyle.HeaderText = dataColumn.ColumnName;
columnStyle.MappingName = dataColumn.ColumnName;
columnStyle.Width = 60;
tableStyle.GridColumnStyles.Add(columnStyle);
dataColumn = dataTable.Columns[5];
columnStyle = new DataGridTextBoxColumn();
columnStyle.HeaderText = dataColumn.ColumnName;
columnStyle.MappingName = dataColumn.ColumnName;
columnStyle.Width = 40;
tableStyle.GridColumnStyles.Add(columnStyle);
dataGrid.TableStyles.Add(tableStyle);
Graphics.Dispose();
}
示例3: GetColumnWidth
public static int GetColumnWidth(DataGrid grid, PropertyInfo proInfo, IList listSource, string headerText)
{
int maxWidth = 0;
int headWidth = (int)grid.CreateGraphics().MeasureString(headerText, grid.HeaderFont).Width;
if (headWidth > maxWidth)
maxWidth = headWidth;
foreach (object obj in listSource)
{
int width = 40;
object v = proInfo.GetValue(obj, null);
if (v != null)
{
SizeF size = grid.CreateGraphics().MeasureString(v.ToString(), grid.Font);
width = (int)size.Width;
}
if (width > maxWidth)
maxWidth = width;
}
return maxWidth + 40;
}
示例4: getLongestField
private static int getLongestField(DataGrid grid, int iCol)
{
int num2 = 60;
int count = ((ArrayList)grid.DataSource).Count;
Graphics graphics = grid.CreateGraphics();
int num5 = Convert.ToInt32(Math.Ceiling((double)graphics.MeasureString(" ", grid.Font).Width));
int num = 0;
if (0 < count)
{
do
{
string text = grid[num, iCol].ToString();
int num3 = Convert.ToInt32(Math.Ceiling((double)graphics.MeasureString(text, grid.Font).Width));
if (num3 > num2)
{
num2 = num3;
}
num++;
}
while (num < count);
}
return (num5 + num2);
}
示例5: CreateColumnStyles
private void CreateColumnStyles(DataGrid TheGrid,DataTable TableGrid)
{
// Define new table style.
DataGridTableStyle tableStyle = new DataGridTableStyle();
Graphics g = TheGrid.CreateGraphics();
try
{
// Clear any existing table styles.
TheGrid.TableStyles.Clear();
// Use mapping name that is defined in the data source.
tableStyle.MappingName = TableGrid.TableName;
// Now create the column styles within the table style.
DataGridTextBoxColumn columnStyle;
for (int iCurrCol = 0; iCurrCol < TableGrid.Columns.Count;
iCurrCol++)
{
DataColumn dataColumn = TableGrid.Columns[iCurrCol];
columnStyle = new DataGridTextBoxColumn();
columnStyle.HeaderText = dataColumn.ColumnName;
columnStyle.MappingName = dataColumn.ColumnName;
columnStyle.TextBox.Width = TheGrid.GetCellBounds(0,iCurrCol).Width;
columnStyle.TextBox.Height = (int)g.MeasureString(columnStyle.HeaderText,TheGrid.HeaderFont).Height + 10;
// Add the new column style to the table style.
tableStyle.GridColumnStyles.Add(columnStyle);
}
// Add the new table style to the data grid.
TheGrid.TableStyles.Add(tableStyle);
}
catch(Exception e)
{
Console.WriteLine(e.Message);
}
finally
{
g.Dispose();
}
}