http://community.csdn.net/Expert/topic/2922/2922103.xml?temp=.8740808
解决方案 »
- 求socket 传输文件 源码,参考学习用
- C#中要删除treeview中的结点,同时把对应的listview的内容删掉时,请问我的代码哪里错了
- TextBox 显示上标(#),或TextBox莫认上标(#)
- 有关dockpanel和treeview一起使用的问题!!急急急!!!分不够可以再加!
- DataList存储过程分页代码 最要带注释的 我是新手 谢谢
- 关于Web Site的Publish的问题
- 同步会启动新的线程吗
- 如何才能单击一次回车键,激发Tab键5次。我的代码如下,该怎么修改,谢谢
- 数据库权限连接查询
- application发布后,不能运行的问题
- 求教:如何从*.jpg文件中取字,并复制?
- 斑竹,达人来帮帮忙啊!xml文件输入到dataset中,编辑,然后SAVE回去为什么就有东西不见了!
{
// Create graphics object for measuring widths.
Graphics Graphics = dataGrid.CreateGraphics();
// Define new table style.
DataGridTableStyle tableStyle = new DataGridTableStyle();
try
{
DataTable dataTable = (DataTable)dataGrid.DataSource;
if (-1 == nRowsToScan)
{
nRowsToScan = dataTable.Rows.Count;
}
else
{
// Can only scan rows if they exist.
nRowsToScan = System.Math.Min(nRowsToScan, dataTable.Rows.Count);
}
// Clear any existing table styles.
dataGrid.TableStyles.Clear();
// Use mapping name that is defined in the data source.
tableStyle.MappingName = dataTable.TableName;
// Now create the column styles within the table style.
DataGridTextBoxColumn columnStyle;
int iWidth;
for (int iCurrCol = 0; iCurrCol < dataTable.Columns.Count; iCurrCol++)
{
DataColumn dataColumn = dataTable.Columns[iCurrCol];
columnStyle = new DataGridTextBoxColumn(); columnStyle.TextBox.Enabled = true;
columnStyle.HeaderText = dataColumn.ColumnName;
columnStyle.MappingName = dataColumn.ColumnName;
// Set width to header text width.
iWidth = (int)(Graphics.MeasureString(columnStyle.HeaderText, dataGrid.Font).Width);
// Change width, if data width is wider than header text width.
// Check the width of the data in the first X rows.
DataRow dataRow;
for (int iRow = 0; iRow < nRowsToScan; iRow++)
{
dataRow = dataTable.Rows[iRow];
if (null != dataRow[dataColumn.ColumnName])
{
int iColWidth = (int)(Graphics.MeasureString(dataRow.ItemArray[iCurrCol].ToString(), dataGrid.Font).Width);
int iColHight = (int)(Graphics.MeasureString(dataRow.ItemArray[iCurrCol].ToString(), dataGrid.Font).Height );
iWidth = (int)System.Math.Max(iWidth, iColWidth);
}
}
columnStyle.Width = iWidth + 4;
tableStyle.GridColumnStyles.Add(columnStyle);
}
// Add the new table style to the data grid.
dataGrid.TableStyles.Add(tableStyle);
}
catch(Exception e)
{
MessageBox.Show(e.Message);
}
finally
{
Graphics.Dispose();
}
}