2015-06-08 2 views
0

Как установить ширину столбцов в динамическом Gridview? Когда AutoGenerateColumns = "истинный"Как установить ширину столбцов в динамическом Gridview?

+0

Добавить код ..? –

+1

@Ameer: ​​Вы пробовали [Что-то здесь] (http://www.dotnetgallery.com/kb/resource69-Set-Gridview-column-width-dynamically-using-C.aspx) – BNN

+0

Спасибо Nadeem за его работу :) –

ответ

0

Вам необходимо внести изменения в RowDataBound случае GridView, как этот

protected void gvData_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e) 
    { 
    if (e.Row.RowType == DataControlRowType.DataRow) { 
     e.Row.Cells(0).Width = new Unit("200px"); 
     e.Row.Cells(1).Width = new Unit("500px"); 
    } 
    } 

разметке

<asp:GridView id="gvData" runat="server"     
       OnRowDataBound="gvData_RowDataBound"> 
</asp:GridView> 

По умолчанию autogeneratecolumn ложна поэтому нет необходимости указывать autogeneratecolumn = «true»

+0

Выше Оба решения не работают в моем случае :( –

0

Вы можете использовать метод serverside:

private void GV_RowDataBound(object o, GridViewRowEventArgs e) 
{   
    // apply custom formatting to data cells 
    if (e.Row.RowType == DataControlRowType.DataRow) 
    { 
     // set formatting for the category cell 
     TableCell cell = e.Row.Cells[0]; 
     cell.Width = new Unit("120px"); 
     cell.Style["border-right"] = "2px solid #666666"; 

     // set formatting for value cells 
     for(int i=1; i<e.Row.Cells.Count; i++) 
     { 
      cell = e.Row.Cells[i]; 
      // right-align each of the column cells after the first 
      // and set the width 
      cell.HorizontalAlign = HorizontalAlign.Right; 
      cell.Width = new Unit("90px"); 
      // alternate background colors 
      if (i % 2 == 1) 
        cell.BackColor 
         = System.Drawing.ColorTranslator.FromHtml("#EFEFEF"); 
        // check value columns for a high enough value 
        // (value >= 8000) and apply special highlighting 
      }      
     } 

     // apply custom formatting to the header cells 
     if (e.Row.RowType == DataControlRowType.Header) 
     { 
      foreach (TableCell cell in e.Row.Cells) 
      { 
       cell.Style["border-bottom"] = "2px solid #666666"; 
       cell.BackColor=System.Drawing.Color.LightGray; 
      } 
     } 

    } 
} 

Ваш aspx страница

<asp:GridView id="myList" runat="server" 
        AutoGenerateColumns="true" 
        OnRowDataBound="GV_RowDataBound" 
        . . . 
        > 
</asp:GridView> 

Для получения более подробной информации вы можете проверить here

+0

по умолчанию AutoGenerateColumns является «истинным». Так что бесполезно упоминать – Alex

+0

Я согласен, но я думаю, вам нужно также проверить свой ответ, как вы уже упоминали. _По умолчанию autogeneratecolumn ** false **, поэтому нет необходимости указывать autogeneratecolumn = "true" _ :) –

Смежные вопросы