关于c# winform datagrid的列宽如何设置为自动适应内容宽度? 在网上找到的这段代码 10
publicvoidAutoSizeTable(DataGriddgData){intnumCols=dgData.TableStyles[0].GridColumnSt...
public void AutoSizeTable(DataGrid dgData)
{
int numCols = dgData.TableStyles[0].GridColumnStyles.Count;
for (int i = 0; i < numCols; i++)
{
AutoSizeCol(dgData,i);
}
}
private static void AutoSizeCol(DataGrid dgData, int colIndex)
{
int rowNums = ((DataTable)dgData.DataSource).Rows.Count;
Byte[] myByte = System.Text.Encoding.Default.GetBytes(dgData.TableStyles["ROW"].GridColumnStyles
[colIndex].HeaderText);
int textCount = myByte.Length;
int tempCount = 0;
for (int i = 0; i < rowNums; i++)
{
if (dgData[i, colIndex] != null)
{
myByte = System.Text.Encoding.Default.GetBytes(dgData[i, colIndex].ToString().Trim());
tempCount = myByte.Length;
if (tempCount > textCount)
{
textCount = tempCount;
}
}
}
dgData.TableStyles[0].GridColumnStyles[colIndex].Width = textCount * 7;
}
但放进程序后,dgData的值总是为null.请问如何解决此问题 展开
{
int numCols = dgData.TableStyles[0].GridColumnStyles.Count;
for (int i = 0; i < numCols; i++)
{
AutoSizeCol(dgData,i);
}
}
private static void AutoSizeCol(DataGrid dgData, int colIndex)
{
int rowNums = ((DataTable)dgData.DataSource).Rows.Count;
Byte[] myByte = System.Text.Encoding.Default.GetBytes(dgData.TableStyles["ROW"].GridColumnStyles
[colIndex].HeaderText);
int textCount = myByte.Length;
int tempCount = 0;
for (int i = 0; i < rowNums; i++)
{
if (dgData[i, colIndex] != null)
{
myByte = System.Text.Encoding.Default.GetBytes(dgData[i, colIndex].ToString().Trim());
tempCount = myByte.Length;
if (tempCount > textCount)
{
textCount = tempCount;
}
}
}
dgData.TableStyles[0].GridColumnStyles[colIndex].Width = textCount * 7;
}
但放进程序后,dgData的值总是为null.请问如何解决此问题 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询