How to Set a gridview column width when binding to a datatable

前端 未结 5 1699
别那么骄傲
别那么骄傲 2021-01-12 00:12

I am binding a table to a gridview in asp.net as such

grdIssues.DataSource = mdtIssues;

grdIssues.DataBind();

The problem is I cannot the

5条回答
  •  温柔的废话
    2021-01-12 00:44

    I like to answer my own question whenever I can so future users searching the thread will find the answer.

    I could not find a way to do what I wanted directly. However I found if I define the columns myself, I could change the properties. In this example, I wanted to center the column data. Something like this.

    BoundField bdfRaisedDate = new BoundField();
    clsUtilities.SetBoundFieldCenter(ref bdfRaisedDate, "RaisedDateShort",    "Opened", "RaisedDate");
    
    grdIssues.Columns.Add(bdfRaisedDate);
    
    grdIssues.DataSource = mdtIssues;
    
    grdIssues.DataBind();
    
    public static void SetBoundFieldCenter(ref BoundField bdfAny, string pDataField, string pHeadingValue, string  pSortExpression)
    {
          bdfAny.DataField = pDataField;
          bdfAny.HeaderText = pHeadingValue;
          bdfAny.SortExpression = pSortExpression;
          bdfAny.HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
          bdfAny.ItemStyle.HorizontalAlign = HorizontalAlign.Center;
    }
    

提交回复
热议问题