tags:

views:

207

answers:

1

Hi folks,

I am struggling with the WinForms DataGridView. I have a class, that I use as element to be displayed:

public class BorderFlowHistoryElement
{
    public string nodeTitles { get; set; }
    public double borderFlowRatio { get; set; }
    ...
}

I created a list of these elements:

List<BorderFlowHistoryElement> clusterHistory

which contains a list of thise elements, that should be displayed in my DataGridView. I bound the list at the DataSource of the Grid:

dataGridViewCluster.DataSource = clusterHistory;

Now the DataGridView displays the list. Now I want to format the columns which display the double values to display 5 digits. I tried it with:

dataGridViewCluster.Columns[1].DefaultCellStyle.Format = "n5";

but this has no effect on the column. Anyone knows, how I can do it right? Additionally, I want to size the columnwidth to optimal fit for the largest entry.

Thanks in advance, Frank

+1  A: 

I have replicated what you have done and I had no issue whatsoever. Have you validated your data to ensure that you can actually get the results that you want?

Here is what I did just for your reference:

private void button1_Click(object sender, EventArgs e)
    {
        IList<BorderFlowHistoryElement> clusterHistory = FillClusterHistory();

        dataGridView1.DataSource = clusterHistory;

        dataGridView1.Columns[1].DefaultCellStyle.Format = "n5";

        dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);

    }

    private static IList<BorderFlowHistoryElement> FillClusterHistory()
    {
        IList<BorderFlowHistoryElement> clusterHistory = new   List<BorderFlowHistoryElement>();


        for(int i = 5000; i < 5020; i++)
        {
            BorderFlowHistoryElement element = new BorderFlowHistoryElement();

            element.nodeTitles = Guid.NewGuid().ToString();

            element.borderFlowRatio = i * 3.3.1415672467234823499821D;

            clusterHistory.Add(element);
        }

        return clusterHistory;
    }
}

public class BorderFlowHistoryElement
{
    private string _NodeTitles;
    private double _BorderFlowRatio;

    public string nodeTitles
    {
        get { return _NodeTitles; }
        set { _NodeTitles = value;}
    }

    public double borderFlowRatio
    {
        get { return _BorderFlowRatio; }
        set { _BorderFlowRatio = value;}
    }
}

I hope that helps in some fashion. As you can see you can do the auto sizing as well.

joshlrogers
It had helped me very much: It showed that my formating code was correct, and the mistake would need to be elsewhere. And i found the bugger: I experimented first with creating ColumnTemplates in the VS-Designer. It didn't work, but I missed to delete them all (I thought this would not be a problem, cause they were overwritten, when bounding my List to the DataSource). But it seems that their pure existance prevent me from formating the Grid programatically.Additionally, the auto-size-thing was exactly what I was looking for. Many thanks for your efforts!
Aaginor