Click here to Skip to main content
15,881,281 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
Hello everybody,
I have a DataGrid that is being populated from a DataTable by a SQL database. My SQL creates a list of objects and I create the datatable and columns dynamically. I am trying to set the column width of each column so I can specify them to whatever width I want. My grid view is loaded by itemsource=datatable. I tried many different ways to try to call my column widths in my code but so far I have not had any luck. Here is what I have.

C#
public ProductsList()
        {
            InitializeComponent();
            CreateProductList();
            dt = DataTableProductList(productList);
            DataGridProducts.ItemsSource = dt.AsDataView();
            //this.DataGridProducts.Columns[0].Width = 50;
            //this.DataGridProducts.Columns[1].Width = 150;
            //this.DataGridProducts.Columns[2].Width = 100;
            //this.DataGridProducts.Columns[3].Width = 50;
            //this.DataGridProducts.Columns[4].Width = 200;
     //this.DataGridProducts.Columns["ID"].Width = 50;
        }	
												private void CreateProductList()
        {            
            try  { productList = ProductDB.GetProductList();  }
            catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); }
            //DataGridProducts.Columns[0].Width = 50;
        } 
														public static DataTable DataTableProductList(List<Product> prodList)
        {
            DataTable dataTable = new DataTable();

            dataTable.Columns.Add("ID");
            dataTable.Columns.Add("Product Name");
            dataTable.Columns.Add("Product Price");
            dataTable.Columns.Add("Quantity");
            dataTable.Columns.Add("Product Listed Date");

            foreach(var p in prodList)
            {
                var row = dataTable.NewRow();
                row["ID"] = p.ProductId;
                row["Product Name"] = p.ProductName;
                row["Product Price"] = p.ProductPrice;
                row["Quantity"] = p.ProductQuantity;
                row["Product Listed Date"] = p.ProductListedDate;
                dataTable.Rows.Add(row);
            }
            return dataTable;
        }


What I have tried:

turning off - AutoGenerateColumns="False"

setting [0] - this.DataGridProducts.Columns[4].Width = 200;

setting ["ID"] - this.DataGridProducts.Columns[“ID”].Width = 50;

and many more things I have tried....
Posted
Updated 15-Mar-21 14:37pm
Comments
[no name] 4-Dec-18 10:07am    
did you tried settings these properties in data bound event?
srilekhamenon 29-Dec-18 11:41am    
set your width at grid.Loaded event and it will work :)

1 solution

public ProductsList()
{
    InitializeComponent();
    CreateProductList();
    DataTable dt = DataTableProductList(productList); // it is assumed that productList is a public variable of type List<>
    BindingSource bs = new BindingSource(); // introduce a BindingSource
    bs.DataSource = dt;
    //DataGridProducts.ItemsSource = dt.AsDataView();
    DataGridProducts.DataSource = bs; //use datasource instead of itemsource
    this.DataGridProducts.Columns[0].Width = 50;
    this.DataGridProducts.Columns[1].Width = 150;
    this.DataGridProducts.Columns[2].Width = 100;
    this.DataGridProducts.Columns[3].Width = 50;
    this.DataGridProducts.Columns[4].Width = 200;
    this.DataGridProducts.Columns["ID"].Width = 50;
}

private void CreateProductList()
{
    try  { productList = ProductDB.GetProductList();  }
    catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); }
    //DataGridProducts.Columns[0].Width = 50;
}
                                                public static DataTable DataTableProductList(List<Product> prodList)
{
    DataTable dataTable = new DataTable();

    dataTable.Columns.Add("ID");
    dataTable.Columns.Add("Product Name");
    dataTable.Columns.Add("Product Price");
    dataTable.Columns.Add("Quantity");
    dataTable.Columns.Add("Product Listed Date");

    foreach(var p in prodList)
    {
        var row = dataTable.NewRow();
        row["ID"] = p.ProductId;
        row["Product Name"] = p.ProductName;
        row["Product Price"] = p.ProductPrice;
        row["Quantity"] = p.ProductQuantity;
        row["Product Listed Date"] = p.ProductListedDate;
        dataTable.Rows.Add(row);
    }
    return dataTable;
}

The truth is I was looking for this solution and your question pointed me the right direction to a solution that worked for me so I decided to share.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900