我通过C#代码手动填充ASP.NET GridView并打开了分页。当用户从一个页面转到另一个页面时,列宽度相应地调整到屏幕上的内容。我希望无论用户在哪个页面上,GridView列都保持不变(最好将每个列设置为其特定列的最大字符长度)。有人可以建议最好的行动方案来实现这一目标。以下是我填充GridView的方法
private void BindData(string sortExpression, string sortDirection)
{
db_set_connection_string.set_db_connnection_string(configuration_manager.GetConfigValue("DBSTRING.1"));
db_set_connection_string.set_individual_global_variables(configuration_manager.GetConfigValue("DBSTRING.1"));
sSQLResults = db_connector_mysql.execute_query_with_results_returned("SELECT * From city", false);
//Dynamically create columns
foreach (DataColumn column in sSQLResults.Columns)
{
//So we aren't getting duplicates, we need to check to see if it exists first
if (DoesColumnExistInGridView(column.ColumnName) == false)
{
BoundField nameColumn = new BoundField();
nameColumn.DataField = column.ColumnName;
nameColumn.HeaderText = column.ColumnName;
nameColumn.SortExpression = column.ColumnName;
GridView1.Columns.Add(nameColumn);
}
}
this.GridView1.DataSource = sSQLResults;
if (sortExpression != null)
{
sSQLResults.DefaultView.Sort = sortExpression + " " + sortDirection;
}
this.GridView1.DataBind();
}
答案 0 :(得分:0)
有两种方法可以做到这一点。在动态添加列时定义宽度。
//Dynamically create columns
foreach (DataColumn column in sSQLResults.Columns)
{
//So we aren't getting duplicates, we need to check to see if it exists first
if (DoesColumnExistInGridView(column.ColumnName) == false)
{
BoundField nameColumn = new BoundField();
nameColumn.DataField = column.ColumnName;
nameColumn.HeaderText = column.ColumnName;
nameColumn.SortExpression = column.ColumnName;
nameColumn.Width=1;
GridView1.Columns.Add(nameColumn);
}
}
或者您可以在Gird View行数据绑定事件
中执行此操作protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e){
e.Rows.Cell[1].width=1;}