Click here to Skip to main content
15,895,777 members
Articles / Desktop Programming / Windows Forms

Plug-in Framework

Rate me:
Please Sign up or sign in to vote.
4.84/5 (25 votes)
17 May 2018CPOL4 min read 101.3K   1.9K   132  
Basic framework for building desktop plug-in applications
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using MBG.Extensions.Core;

namespace MBG.Extensions.Data
{
    public static class DataExtensions
    {
        public static void AddRange(this DataColumnCollection dataColumns, params string[] columnNames)
        {
            foreach (string columnName in columnNames)
            {
                dataColumns.Add(columnName);
            }
        }
        public static void AddRange(this DataColumnCollection dataColumns, params DataColumn[] columns)
        {
            foreach (DataColumn column in columns)
            {
                dataColumns.Add(column);
            }
        }
        public static void AddRange<T>(this DataColumnCollection dataColumns, IEnumerable<string> enumerable)
        {
            foreach (string item in enumerable)
            {
                dataColumns.Add(item);
            }
        }

        public static bool TryAdd(this DataColumnCollection dataColumns, string columnName)
        {
            if (!dataColumns.Contains(columnName))
            {
                dataColumns.Add(columnName);
                return true;
            }
            return false;
        }

        public static bool SaveToCsv(this DataSet dataSet, string filePath)
        {
            return dataSet.SaveToCsv(filePath, true);
        }
        public static bool SaveToCsv(this DataSet dataSet, string filePath, bool outputColumnNames)
        {
            bool ok = false;
            StringBuilder sb = new StringBuilder(2000);
            if (dataSet.Tables.Count > 0)
            {
                foreach (DataTable table in dataSet.Tables)
                {
                    #region Column Names
                    if (outputColumnNames)
                    {
                        foreach (DataColumn column in table.Columns)
                        {
                            sb.Append(column.ColumnName);
                            sb.Append(',');
                        }
                        sb.Remove(sb.Length - 1, 1);
                        sb.Append(Environment.NewLine);
                    }
                    #endregion

                    #region Rows (Data)
                    foreach (DataRow row in table.Rows)
                    {
                        foreach (DataColumn column in table.Columns)
                        {
                            sb.Append(row[column].ToString());
                            sb.Append(',');
                        }
                        //Remove Last ','
                        sb.Remove(sb.Length - 1, 1);
                        sb.Append(Environment.NewLine);
                    }
                    #endregion
                }

                ok = sb.ToString().ToFile(filePath);
            }

            return ok;
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
Software Developer (Senior) Freelancer
Australia Australia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions