1

I am populating an ASP.NET GridView manually via C# code and have paging turned on. When a user goes from one page to another the column widths adjust accordingly to what is on the screen. I would like the GridView columns to remain the same no matter which page the user is on (preferably each column is set to the largest character length for its particular column). Can someone suggest the best course of action to accomplish this. Here is how I am populating the 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();
    }
1
  • Everything I'm seeing online isn't showing a simple solution to this. You may have to switch to a Repeater, which will give you exact control over the look and feel of the data display. Jun 24, 2013 at 19:34

1 Answer 1

0

There are two ways you can do this. Define width at the time of adding columns dynamically.

 //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);
        }
    }            

or you can do that in Gird View row data bind event

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e){
 e.Rows.Cell[1].width=1;}
3
  • Neither of those work. nameColumn.BoundField does not contain a Width property. For the RowDataBound Event, I don't believe this works because I am updating the GridView manually. Jun 24, 2013 at 20:11
  • Still its works with row databound event. Because event if you bind grid manually this event will called. Jun 25, 2013 at 5:43
  • I have written a blog post to set dynamic width of asp.net grid view.dotnetjalps.com/2013/07/…. Hope this will help you Jul 6, 2013 at 11:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.