<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Stories about everyday life &#187; C#</title>
	<atom:link href="http://www.hariscusto.com/tag/c/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.hariscusto.com</link>
	<description>my playground</description>
	<lastBuildDate>Wed, 14 Dec 2011 23:10:50 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.3</generator>
		<item>
		<title>ExportData Table to Excel using C#</title>
		<link>http://www.hariscusto.com/programming/export-data-table-to-excel-using-c/</link>
		<comments>http://www.hariscusto.com/programming/export-data-table-to-excel-using-c/#comments</comments>
		<pubDate>Mon, 15 Feb 2010 17:08:14 +0000</pubDate>
		<dc:creator>Haris Čusto</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Database]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[datatable]]></category>
		<category><![CDATA[excel]]></category>
		<category><![CDATA[export]]></category>
		<category><![CDATA[xls]]></category>

		<guid isPermaLink="false">http://www.hariscusto.com/?p=83</guid>
		<description><![CDATA[Here is little class that I wrote some time ago that helps you export data from DataTable into Excell xls file. It format fields in xls file based on data type from data table. It&#8217;s not a perfect but it will help you to quickly save data from Data Table to Excel file. Example of [...]]]></description>
			<content:encoded><![CDATA[<p>Here is little class that I wrote some time ago that helps you export data from DataTable into Excell xls file. It format fields in xls file based on data type from data table.</p>
<p>It&#8217;s not a perfect but it will help you to quickly save data from Data Table to Excel file.</p>
<p>Example of usage:</p>
<pre class="brush: csharp; title: ; notranslate">
Common.Data.Export2Excel(dataTableWithData, &quot;exported.xls&quot;);
</pre>
<p><span id="more-83"></span></p>
<pre class="brush: csharp; title: ; notranslate">
using System;
using System.Data;
using System.Data.OleDb;
using System.IO;

namespace Common.Data
{
	public class Export2Excel
	{
		public static bool Export(DataTable tbl, string fileName, bool createTable, bool overwriteFile)
		{
      string tableName = tbl.TableName.Replace(&amp;quot; &amp;quot;, &amp;quot;_&amp;quot;);
			tableName = String.IsNullOrEmpty(tableName) ? &amp;quot;Sheet1&amp;quot; : tableName;
			try
			{
				System.Globalization.CultureInfo _infoEn = System.Globalization.CultureInfo.GetCultureInfo(&amp;quot;en-GB&amp;quot;);
				string sql = &amp;quot;&amp;quot;;
				if (overwriteFile)
					if (File.Exists(fileName))
						File.Delete(fileName);

				using (OleDbConnection con = new OleDbConnection(&amp;quot;Provider=Microsoft.Jet.OLEDB.4.0;Data Source=&amp;quot; + fileName + &amp;quot;;Extended Properties='Excel 8.0;HDR=Yes'&amp;quot;))
				{
					con.Open();
					OleDbCommand cmdInsert;
					if (createTable || !File.Exists(fileName))
					{
						sql = &amp;quot;CREATE TABLE &amp;quot; + tableName + &amp;quot; (&amp;quot;;
						for (int i = 0; i &amp;lt; tbl.Columns.Count; i++)
						{
							sql += tbl.Columns[i].ColumnName;
							if (i + 1 == tbl.Columns.Count) //Here we decide should we close insert command or appebd another create column command
								sql += &amp;quot; &amp;quot; + GetColumnType(tbl.Columns[i]) + &amp;quot;)&amp;quot;; //Close insert
							else
								sql += &amp;quot; &amp;quot; + GetColumnType(tbl.Columns[i]) + &amp;quot;,&amp;quot;; //there is more columns to add
						}
					}
					if (!String.IsNullOrEmpty(sql))
					{
						cmdInsert = new OleDbCommand(sql, con);
						cmdInsert.ExecuteNonQuery();
					}
					foreach (DataRow row in tbl.Rows)
					{
						//Dodati parametre na comandu
						string values = &amp;quot;(&amp;quot;;
						for (int i = 0; i &amp;lt; tbl.Columns.Count; i++)
						{
							if (i + 1 == tbl.Columns.Count)
							{
								if (tbl.Columns[i].DataType == System.Type.GetType(&amp;quot;System.Decimal&amp;quot;) ||
									 tbl.Columns[i].DataType == System.Type.GetType(&amp;quot;System.Int64&amp;quot;) ||
									 tbl.Columns[i].DataType == System.Type.GetType(&amp;quot;System.Double&amp;quot;))
									values += String.IsNullOrEmpty(row[i].ToString()) ? &amp;quot;0)&amp;quot; : Convert.ToDecimal(row[i]).ToString(&amp;quot;#0.00&amp;quot;, _infoEn) + &amp;quot;)&amp;quot;;
								else
									values += &amp;quot;'&amp;quot; + System.Security.SecurityElement.Escape(row[i].ToString()) + &amp;quot;')&amp;quot;;
							}
							else
							{
								if (tbl.Columns[i].DataType == System.Type.GetType(&amp;quot;System.Decimal&amp;quot;) ||
									 tbl.Columns[i].DataType == System.Type.GetType(&amp;quot;System.Int64&amp;quot;) ||
									 tbl.Columns[i].DataType == System.Type.GetType(&amp;quot;System.Double&amp;quot;))
									values += String.IsNullOrEmpty(row[i].ToString()) ? &amp;quot;0,&amp;quot; : Convert.ToDecimal(row[i]).ToString(&amp;quot;#0.00&amp;quot;, _infoEn) + &amp;quot;,&amp;quot;;
								else
									values += &amp;quot;'&amp;quot; + System.Security.SecurityElement.Escape(row[i].ToString()) + &amp;quot;',&amp;quot;;
							}
						}
						string sqlInsert = String.Format(&amp;quot;Insert into [{0}$] VALUES {1}&amp;quot;, tableName, values);
						cmdInsert = new OleDbCommand(sqlInsert, con);

						cmdInsert.ExecuteNonQuery();
					}
				}
			}
			catch (Exception)
			{
				return false;
			}
				return true;
		}

		private static string GetColumnType(DataColumn dataColumn)
		{
			string t;
			if (dataColumn.DataType == System.Type.GetType(&amp;quot;System.Decimal&amp;quot;))
				t = &amp;quot;decimal&amp;quot;;
			else if (dataColumn.DataType == System.Type.GetType(&amp;quot;System.Int64&amp;quot;))
				t = &amp;quot;INT&amp;quot;;
			else if (dataColumn.DataType == System.Type.GetType(&amp;quot;System.Double&amp;quot;))
				t = &amp;quot;double&amp;quot;;
			else
				t = &amp;quot;VARCHAR(255)&amp;quot;;
			return t;
		}
		public static bool Export(DataTable tbl, string fileName)
		{
			return Export(tbl, fileName, true, true);
		}
	}
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.hariscusto.com/programming/export-data-table-to-excel-using-c/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Reset all controls on form (Windows forms &#8211; C#)</title>
		<link>http://www.hariscusto.com/programming/reset-all-controls-on-forms/</link>
		<comments>http://www.hariscusto.com/programming/reset-all-controls-on-forms/#comments</comments>
		<pubDate>Fri, 25 Dec 2009 16:13:54 +0000</pubDate>
		<dc:creator>Haris Čusto</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[UI]]></category>
		<category><![CDATA[WinForms]]></category>
		<category><![CDATA[Windows]]></category>

		<guid isPermaLink="false">http://www.hariscusto.com/?p=16</guid>
		<description><![CDATA[Well i had a situation where i need to reset all controls (mostly Textboxes and Comboboxes) on form to their default (entry ready) state. Well, at first sight I was thinking it&#8217;s not a problem, my .net Win form have property called Controls &#8211; list of all controls on form. But problem was that i [...]]]></description>
			<content:encoded><![CDATA[<p>Well i had a situation where i need to reset all controls (mostly Textboxes and Comboboxes)  on form to their default (entry ready) state.  Well, at first sight I was thinking it&#8217;s not a problem, my .net Win form have property called <strong>Controls</strong> &#8211; list of all controls on form.  But problem was that i had overlapping panels which i used for positioning, and inside of panels was Group box control. So it&#8217;s allready complicated then at first sight.</p>
<p>I have created little Form helper which will help you reset all controls on form to their default state.<br />
<span id="more-16"></span></p>
<pre class="brush: csharp; title: ; notranslate">
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace UI.Forms
{
	public class FormHelper
	{
		public static void ResetFields(Control form)
		{
			foreach (Control ctrl in form.Controls)
			{
				if (ctrl.Controls.Count &amp;gt; 0)
					ResetFields(ctrl);
				Reset(ctrl);
			}
		}
		public static void Reset(Control ctrl)
		{
			if (ctrl is TextBox)
			{
				TextBox tb = (TextBox)ctrl;
				if (tb != null)
				{
					tb.ResetText();
				}
			}
			else if (ctrl is ComboBox)
			{
				ComboBox dd = (ComboBox)ctrl;
				if (dd != null)
				{
					dd.SelectedIndex = 0;
				}
			}
		}
	}
}
</pre>
<p>Sample usage &#8211; calling method from code behind form:<br />
Example:</p>
<pre class="brush: csharp; title: ; notranslate">FormHelper.ResetFields(this);
</pre>
<p>Where <strong>this</strong> is reference to Form on which controls resides</p>
<p>I hope you&#8217;ll find it useful.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.hariscusto.com/programming/reset-all-controls-on-forms/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

