当前位置: 首页>>代码示例>>C#>>正文


C# ListView.ThrowIfNull方法代码示例

本文整理汇总了C#中System.Windows.Forms.ListView.ThrowIfNull方法的典型用法代码示例。如果您正苦于以下问题:C# ListView.ThrowIfNull方法的具体用法?C# ListView.ThrowIfNull怎么用?C# ListView.ThrowIfNull使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Windows.Forms.ListView的用法示例。


在下文中一共展示了ListView.ThrowIfNull方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: CreateCSV

        /// <summary>
        /// This method takes a list view and returns a multi-line string that represents the listview as a CSV (comma-delimited) file.
        /// </summary>
        /// <param name="listViewToExport">Thr listview to export.</param>
        /// <param name="withUnit">if set to <c>true</c> listView is exported with unit column.</param>
        /// <remarks>
        /// For delimiter we use the semicolon in order to support decimal and thousand seperator in different cultures.
        /// The major difference is that the list view assumes to contain units, so if the values in each column contain two values seperated by a space
        /// and the second value is the same in every column (1 and beyond), the unit is seperated out and placed in column "2". This allows the
        /// values to be imported into the spreadsheet software as a number, instead of a string enabling numerical analysis of the export.
        /// </remarks>
        /// <exception cref="System.ArgumentNullException">listViewToExport</exception>
        public static void CreateCSV(ListView listViewToExport, bool withUnit = false)
        {
            listViewToExport.ThrowIfNull(nameof(listViewToExport));

            using (SaveFileDialog saveFileDialog = new SaveFileDialog())
            {
                saveFileDialog.Filter = @"Comma Delimited Files (Semicolon) (*.csv)|*.csv";
                if (saveFileDialog.ShowDialog() != DialogResult.OK)
                    return;

                StringBuilder sb = new StringBuilder();

                // Export the column headers
                bool ignoreSemicolon = true;
                for (int i = 0; i < listViewToExport.Columns.Count; i++)
                {
                    ColumnHeader myColumn = listViewToExport.Columns[i];
                    sb.Append(MakeCSVString(myColumn.Text, ignoreSemicolon));
                    ignoreSemicolon = false;

                    if (i == 0 && withUnit)
                        sb.Append(MakeCSVString("Unit"));
                }
                sb.AppendLine();

                for (int line = 0; line < listViewToExport.Items.Count; line++)
                {
                    string[] elements = listViewToExport.Items[line].SubItems[1].Text.Split(" ".ToCharArray());
                    string unit = String.Empty;
                    if (withUnit)
                    {
                        // Determine if the items have a unit description
                        string possibleUnit = String.Join(" ", elements.Skip(1));
                        bool hasUnit = elements.Length > 1 &&
                                       StaticProperties.AllProperties.Any(prop => prop.Unit == possibleUnit);
                        if (hasUnit)
                            unit = possibleUnit;
                    }

                    // Export the lines
                    ignoreSemicolon = true;
                    int maxElements = listViewToExport.Columns.Count;

                    if (listViewToExport.Items[line].SubItems.Count < maxElements)
                        maxElements = listViewToExport.Items[line].SubItems.Count;

                    for (int subitem = 0; subitem < maxElements; subitem++)
                    {
                        elements = listViewToExport.Items[line].SubItems[subitem].Text.Split(" ".ToCharArray());

                        // If the value is a number format it as so; as string otherwise
                        double number;
                        sb.Append(Double.TryParse(elements[0], out number)
                            ? MakeCSVNumber(number.ToString(CultureConstants.DefaultCulture), ignoreSemicolon)
                            : MakeCSVString(listViewToExport.Items[line].SubItems[subitem].Text, ignoreSemicolon));

                        ignoreSemicolon = false;

                        if (subitem == 0 && withUnit)
                            sb.Append(MakeCSVString(unit));
                    }
                    sb.AppendLine();
                }

                File.WriteAllText(saveFileDialog.FileName, sb.ToString());
            }
        }
开发者ID:,项目名称:,代码行数:79,代码来源:


注:本文中的System.Windows.Forms.ListView.ThrowIfNull方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。