如何在asp.net中为gridview动态添加列宽
朋友们,我对gridview有一个要求,我有一个数据表,我手动分配列名,并将数据添加到datatable。之后我垂直设置数据表并绑定到gridview。
Hi friends , I have a requirement on gridview, i have a datatable for that i was assigning the column names manually and also adding the data to datatable. After that i am set the datatable vertically and bind to the gridview.
var tbl = dt;
var swappedTable = new DataTable();
if (tbl.Rows.Count > 0)
{
swappedTable.Columns.Add("Name");
for (int i = 0; i <= tbl.Rows.Count-1; i++)
{
DataRow dx = tbl.Rows[i];
var x = dx.ItemArray;
// swappedTable.Columns.Add("Value"+i.ToString());
swappedTable.Columns.Add(x[3].ToString());
}
for (int col = 0; col < tbl.Columns.Count; col++)
{
var r = swappedTable.NewRow();
r[0] = tbl.Columns[col].ToString();
for (int j = 1; j <= tbl.Rows.Count; j++)
r[j] = tbl.Rows[j - 1][col];
swappedTable.Rows.Add(r);
}
//dataGridView1.DataSource = swappedTable;
gvwHDMonitor.DataSource = swappedTable;
gvwHDMonitor.DataBind();
示例:
姓名 ...................... IICSS21
操作系统............. Microsoft Windows 7专业版
版本.. ............ 6.1.7601
制造商.............微软公司
计算机名称。 ............ IICSS21
Windows目录............. C:\ Windows
Serial编号............. 00371-OEM-9203635-37779
ComputerManufacturer名称.............戴尔公司>
计算机型号............. OptiPlex 990
系统类型............... x64-based $
这里:Operationg系统,这些版本是列
Microsoft Windows 7 Professional,6.1.760l这些是数据表中的第一行,同时其他记录也动态添加到数据表中作为列。
这里动态地将列添加到datatable。
问题是我想增加动态添加列的列宽。
如何解决这个问题。
请任何人帮助我。
Example :
Name ...................... IICSS21
Operating System ............. Microsoft Windows 7 Professional
Version .............. 6.1.7601
Manufacturer ............. Microsoft Corporation
Computer Name ............. IICSS21
Windows Directory ............. C:\Windows
Serial Number ............. 00371-OEM-9203635-37779
ComputerManufacturer Name ............. Dell Inc.
Computer Model ............. OptiPlex 990
System Type ............. x64-based PC
here : Operationg system, Version like these are the columns
Microsoft Windows 7 Professional,6.1.760l these are the first row in datatable and same time other records also added dynamically to datatable as columns.
Here dynamically added the columns to datatable.
Problem is i want to increase the column width for dynamically added columns.
How can do this problem.
Please any one help me.
您好,
从以下代码中获取知识:
Hi,
Take knowledge from this below code :
protected void GridView1_RowDataBound(object sender,
GridViewRowEventArgs e)
{
System.Data.DataRowView drv;
drv = (System.Data.DataRowView)e.Row.DataItem;
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (drv != null)
{
String catName = drv[1].ToString();
Response.Write(catName + "/");
int catNameLen = catName.Length;
if (catNameLen > widestData)
{
widestData = catNameLen;
GridView1.Columns[2].ItemStyle.Width =
widestData * 30;
GridView1.Columns[2].ItemStyle.Wrap = false;
}
}
}
}
如果没有解决,请点击此处: http://msdn.microsoft.com/en-us /library/ms178296(v=vs.100).aspx [ ^ ]
如果要包装单元格内容然后点击此处: http://myaspsnippets.blogspot.in/ 2011/01 /那是偶然的,当一个人可能工作的时候...... [ ^ ]