codeproject.com 有根据记录内容自动调整列宽的!效果不错public void SizeColumnsToContent (DataGrid dataGrid, int nRowsToScan) //dataGrid 就是要绑定的dataGrid 的Name;nRowsToScan=-1 表示对所有的记录都自动调整列宽 { // 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); iWidth = (int)System.Math.Max(iWidth, iColWidth); } } columnStyle.Width = iWidth + 4; // Add the new column style to the table style. 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(); } }
this.dataGridTextBoxColumnName.Width = 75;
else
this.dataGridTextBoxColumnName.Width = 0;
-------------------------
代码控制:
在datagrid的ItemDataBound事件里写
比如:
private void dgd_temp_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
LinkButton btn_del;
if(e.Item.ItemType == ListItemType.Item||e.Item.ItemType==ListItemType.AlternatingItem)
{
btn_del = (LinkButton)e.Item.FindControl("btn_del");
btn_del.Attributes.Add("onclick","return confirm('你真的要删除吗?');");
}
}-----
e.Item.Cells[i]---访问单元格
-----
DataGrid1.Columns[i].ItemStyle.Width--列宽
this.objTableStylegrdIssuesIssues.GridColumnStyles.AddRange这个方法,去掉里面不需要的列
宽度也在自动生成的代码中可以找到,Width属性,不难发现
或者
ds.Tables["Products"].Columns["QuantityPerUnit"].ColumnMapping = MappingType.Hidden;
DataGrid1.SetDataBinding[ds, "Products"];
http://community.csdn.net/Expert/topic/3896/3896149.xml?temp=.5802423
3.cs中,设置DataGrid控件的莫列的宽度为0,让其不显示出来DataGridTableStyle MyStyle =new DataGridTableStyle(); //定义表样式对象
DataTable tableName = (DataTable)this.dgDoc.DataSource; //获取表对象
MyStyle.MappingName = tableName.TableName; //把表的名字传给样式对象的属性(必要的)
dgDoc.TableStyles.Add(MyStyle); //把样式添加到DataGrid控件中
MyStyle.GridColumnStyles["编号"].Width=0; //设置莫行不显示的列的宽度为0
MyStyle.GridColumnStyles["文档名称"].Width=400;
codeproject.com 有根据记录内容自动调整列宽的!效果不错public void SizeColumnsToContent (DataGrid dataGrid, int nRowsToScan)
//dataGrid 就是要绑定的dataGrid 的Name;nRowsToScan=-1 表示对所有的记录都自动调整列宽
{
// 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);
iWidth = (int)System.Math.Max(iWidth, iColWidth);
}
}
columnStyle.Width = iWidth + 4; // Add the new column style to the table style.
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();
}
}
用不着那么复杂的方法吧Win Form用列集合的属性绑定需要的字段Web 里用属性生成器绑定需要的字段就可以