0

I've a problem where i need to get the total number of columns, which defines the total columns. Currently i have a function which dictates the header text on the DataGrid.columns.

public static string ColumnHeader1 = Properties.Settings.Default.ColumnHeader1;

public void dataGridHeaders()
    {
        dataGrid.Columns[0].Header = ColumnHeader1;
        dataGrid.Columns[1].Header = ColumnHeader2;
        dataGrid.Columns[2].Header = ColumnHeader3;
        dataGrid.Columns[3].Header = ColumnHeader4;
        dataGrid.Columns[4].Header = ColumnHeader5;
        dataGrid.Columns[5].Header = ColumnHeader6;
        dataGrid.Columns[6].Header = ColumnHeader7;
        dataGrid.Columns[7].Header = ColumnHeader8;
     }

the string vars being read in from an xml settings file. What i'm looking to do is depending on how many strings in the xml file depicts how many columns there will be at runtime. Or adding an int var say..

public static int totalNumberOfColumns = 8; 

and then iterating through a loop adding the columns.

are any of these ways possible?

4

2 回答 2

1

将 XML 文件中的列标题放入字符串列表

List<string> columnHeaderList = new List<string>();
foreach (string header in XmlColumnList)
    columnHeaderList.Add(header);

XmlColumnList保存值的数组在哪里。现在添加以下方法,该方法将根据需要添加所有可用的列标题。

private void DataGridHeaders(List<string> headerList)
{
    for (int i = 0; i < headerList.Count; i++)
        dataGrid.Columns[i].Header = headerList[i];
}

我希望这有帮助。

于 2012-08-10T10:48:02.033 回答
0
            var xmlSample = @"<DataGridColumnsHeader>
                            <ColumnHeaderText>Name</ColumnHeaderText>
                            <ColumnHeaderText>Country</ColumnHeaderText>
                            <ColumnHeaderText>Phone</ColumnHeaderText>
                          </DataGridColumns>";

        var counter = 0;
        var elementCount = XDocument.Load(new System.IO.StringReader(xmlSample)).XPathSelectElements("//ColumnHeaderText").Count();
        foreach (var element in XDocument.Load(new System.IO.StringReader(xmlSample)).XPathSelectElements("//ColumnHeaderText"))
        {
            dataGrid.Columns[counter].Header = element.Value;
            counter++;
        }

希望这可以帮助!

于 2012-08-10T11:03:32.130 回答