Wednesday, August 14, 2013

Get file names from FTP site

this example is how to get list of file names on specific ftp site and bind the result to RadListView with two column.
I used RadListView of Telerik. You can use standard  ListView or ASP.

public void ListDirectory()
        {
            var request = createRequest(URL, WebRequestMethods.Ftp.ListDirectory);

            DataTable DT = new DataTable();
            DataColumn col1 = new DataColumn("URL");
            DataColumn col2 = new DataColumn("FILENAME");
            DT.Columns.Add(col1);
            DT.Columns.Add(col2);
            DT.Rows.Clear();

            using (var response = (FtpWebResponse)request.GetResponse())
            {
                using (var stream = response.GetResponseStream())
                {
                    using (var reader = new StreamReader(stream, true))
                    {
                        while (!reader.EndOfStream)
                        {
                            string line = reader.ReadLine();
                            DT.Rows.Add(URL + line, line);
                        }
                    }
                }
            }

            this.listView.DataSource = DT;
        }
private FtpWebRequest createRequest(string uri, string method)
        {
            var r = (FtpWebRequest)WebRequest.Create(uri);

            r.Credentials = new NetworkCredential(USERNAME, PASSWORD);
            r.Method = method;

            return r;
        }


Read more!!!

Monday, December 6, 2010

C# Winform DatagridView Numeric Column Sorting


private void dataGridView1_SortCompare(object sender, DataGridViewSortCompareEventArgs e)
{
if (e.Column.Index == 0)
{
if (double.Parse(e.CellValue1.ToString()) > double.Parse(e.CellValue2.ToString()))
{
e.SortResult = 1;
}
else if (double.Parse(e.CellValue1.ToString()) < double.Parse(e.CellValue2.ToString()))
{
e.SortResult = -1;
}
else
{
e.SortResult = 0;
}
e.Handled = true;
}
}


Read more!!!

Tuesday, November 2, 2010

Import from .csv file to DataGridView


using Microsoft.VisualBasic;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Data.Odbc;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;



// ================================================================================================
//
// This program copies data from the data file C:\TEST.CSV to C:\TEMP.CSV
// This is to allow modification of the title line to allow SQL to search on the line.
// TEMP.CSV is deleted at the end of the program.
//
// ================================================================================================

namespace ReadDataFile
{
public partial class Form1 : Form
{

char[] splitArray1 = { ',', '\n', '\r' };
char[] splitArray2 = { ' ' };

Int64[] SearchList = new long[1];

string CSVDataSource;
string FileName;
string FileSize;
string LineText;
string TempFileName;

string[] DataResult1 = { "", "", "", "", "" };// Use to populate the grid.
string[] Titles = { "Date", "Time", "Thickness", "Track Speed" };

//Create a dataset
DataSet dataset = new DataSet("My Dataset");
//Create a table
DataTable datatable = new DataTable("Temp.CSV");

public Form1()
{
InitializeComponent();
TempFileName = @"C:\Temp.csv";

lblLoading.Visible = false;

CreateTable();
dataset.Tables.Add(datatable);
}

private void btnOpenFile_Click(object sender, EventArgs e)
{
// Clear datagrid contents
dgvData.SelectAll();
dgvData.ClearSelection();
// Set file name
FileName = txtFileName.Text;
CSVDataSource = FileName;
if (File.Exists(TempFileName))
{
File.Delete(TempFileName);
}
StreamReader sr = new StreamReader(FileName);
StreamWriter sw = new StreamWriter(TempFileName);
// Read & dump header
string junk = sr.ReadLine();
// Read file into string
string FileData = sr.ReadToEnd();
FileSize = FileData.Length.ToString("N");
FileSize = FileSize.Substring(0, FileSize.IndexOf("."));
lblLoading.Text = "Loading " + FileSize + " bytes.\nPlease wait a moment or two.";
lblLoading.Visible = true;
lblLoading.Update();
// Change header to meet with ODBC title requirements
sw.WriteLine(" Stream No.,Die No,Date,Time,Thickness,Status,Track");
sw.WriteLine(LineText);
sw.Write(FileData);
sr.Close();
sw.Close();
ReadData();
lblLoading.Visible = false;
dgvData.Update();
dgvData.Columns[4].HeaderText = "Track speed";
if (File.Exists(TempFileName))
{
File.Delete(TempFileName);
}
}

private void CreateTable()
{
for (int i = 0; i < 4; i++)
{
datatable.Columns.Add(Titles[i]);
}
}

///
/// Open TEMP.CSV as ODBC database file
/// Access data using SQL 'Select' command
/// Move data from DATABASE to DataTable and assign to DataGridView object
/// Make each column UNSORTABLE to stop user messing with data!!!
///

private void ReadData()
{
string tempPath = "C:";
string strConn = @"Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=" + tempPath + @"\;Extensions=asc,csv,tab,txt";
OdbcConnection conn = new OdbcConnection(strConn);
OdbcDataAdapter da = new OdbcDataAdapter("Select Date,Time,Thickness,Status,Track from temp.csv", conn);
DataTable dt = new DataTable();
da.Fill(dt);
dgvData.DataSource = dt;
dgvData.Columns[1].DefaultCellStyle.Format = "T";
foreach (DataGridViewColumn col in dgvData.Columns)
{
col.SortMode = DataGridViewColumnSortMode.NotSortable;
}
}


}
}


Read more!!!