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!!!

Friday, September 17, 2010

Using binding context with 2 tables in C#

       
....
DataTable dt = new DataTable("One");
DataTable dt1 = new DataTable("Two");
DataSet ds = new DataSet();
private void Form1_Load(object sender, EventArgs e)
{
dt.Columns.Add("No");
dt.Columns.Add("Name");
dt.PrimaryKey = new DataColumn[] {dt.Columns[0] };
dt.Rows.Add("1", "A");
dt.Rows.Add("2", "B");


dt1.Columns.Add("No");
dt1.Columns.Add("Address");

dt1.Rows.Add("1", "Local address");
dt1.Rows.Add("2", "Postal address");
dt1.Rows.Add("2", "Local address");


ds.Tables.Add(dt);
ds.Tables.Add(dt1);
//Relation object
DataRelation rel = new DataRelation("relation1", dt.Columns[0], dt1.Columns[0]);
ds.Relations.Add(rel);

bindingSource1.DataSource = ds;
bindingSource1.DataMember = "One";

bindingSource2.DataSource = bindingSource1;
bindingSource2.DataMember = "relation1";


textBox1.DataBindings.Add("Text", bindingSource1, "No");
textBox2.DataBindings.Add("Text", bindingSource1, "Name");
textBox3.DataBindings.Add("Text", bindingSource2, "No");
textBox4.DataBindings.Add("Text", bindingSource2, "Address");
}

private void button1_Click(object sender, EventArgs e)
{
bindingSource1.Position += 1;
}

private void button2_Click(object sender, EventArgs e)
{
bindingSource1.Position -= 1;
}

private void button3_Click(object sender, EventArgs e)
{
bindingSource2.Position += 1;
}

private void button4_Click(object sender, EventArgs e)
{
bindingSource2.Position -= 1;
}
...


Read more!!!