DateTimePickerColumn in DataGridView



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace SIMS.MesoftControl
{
    class CalendarColumn : DataGridViewColumn
    {
        public CalendarColumn()
            : base(new CalendarCell())
        {
        }
        public override DataGridViewCell CellTemplate
        {
            get
            {
                return base.CellTemplate;
            }
            set
            {
                if (value != null && !value.GetType().IsAssignableFrom(typeof(CalendarCell)))
                {
                    throw new InvalidCastException("Must be a CalendarCell");
                }
                base.CellTemplate = value;
            }
        }
    }
    public class CalendarCell : DataGridViewTextBoxCell
    {
        public CalendarCell()
            : base()
        {
            this.Style.Format = "d";
        }
        public override void InitializeEditingControl(int rowIndex, object initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
        {
            base.InitializeEditingControl(rowIndex, initialFormattedValue,
            dataGridViewCellStyle);
            CalendarEditingControl ctl =
            DataGridView.EditingControl as CalendarEditingControl;
            ctl.Value = (DateTime)this.Value;
        }
        public override Type EditType
        {
            get
            {
                return typeof(CalendarEditingControl);
            }
        }
        public override Type ValueType
        {
            get
            {
                return typeof(DateTime);
            }
        }
        public override object DefaultNewRowValue
        {
            get
            {
                return DateTime.Now;
            }
        }
    }
    class CalendarEditingControl : DateTimePicker, IDataGridViewEditingControl
    {
        DataGridView dataGridView;
        private bool valueChanged = false;
        int rowIndex;
        public CalendarEditingControl()
        {
            this.Format = DateTimePickerFormat.Short;
        }
        public object EditingControlFormattedValue
        {
            get
            {
                return this.Value.ToShortDateString();
            }
            set
            {
                if (value is String)
                {
                    this.Value = DateTime.Parse((String)value);
                }
            }
        }
        public object GetEditingControlFormattedValue(DataGridViewDataErrorContexts context)
        {
            return EditingControlFormattedValue;
        }
        public void ApplyCellStyleToEditingControl(DataGridViewCellStyle dataGridViewCellStyle)
        {
            this.Font = dataGridViewCellStyle.Font;
            this.CalendarForeColor = dataGridViewCellStyle.ForeColor;
            this.CalendarMonthBackground = dataGridViewCellStyle.BackColor;
        }
        public int EditingControlRowIndex
        {
            get
            {
                return rowIndex;
            }
            set
            {
               rowIndex = value;
            }
        }
        public bool EditingControlWantsInputKey(Keys key, bool dataGridViewWantsInputKey)
        {
            switch (key & Keys.KeyCode)
            {
                case Keys.Left:
                case Keys.Up:
                case Keys.Down:
                case Keys.Right:
                case Keys.Home:
                case Keys.End:
                case Keys.PageDown:
                case Keys.PageUp:
                    return true;
                default:
                    return false;
            }
        }
        public void PrepareEditingControlForEdit(bool selectAll)
        {
        }
        public bool RepositionEditingControlOnValueChange
        {
            get
            {
                return false;
            }
        }
        public DataGridView EditingControlDataGridView
        {
            get
            {
                return dataGridView;
            }
            set
            {
                dataGridView = value;
            }
        }
        public bool EditingControlValueChanged
        {
            get
            {
                return valueChanged;
            }
            set
            {
                valueChanged = value;
            }
        }
        public Cursor EditingPanelCursor
        {
            get
            {
                return base.Cursor;
            }
        }
        protected override void OnValueChanged(EventArgs eventargs)
        {
            valueChanged = true;
            this.EditingControlDataGridView.NotifyCurrentCellDirty(true);
            base.OnValueChanged(eventargs);
        }
    }
}

Group Chat Example



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace MulticastChat
{
    public partial class Form1 : Form
    {
        Socket cs;
        Thread t;
        IPEndPoint gip;
        public Form1()
        {
            InitializeComponent();
            gip = new IPEndPoint(IPAddress.Parse(GroupIptextBox.Text), 7080);
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                panel1.Enabled = false;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        public void Receive()
        {
            try
            {
                EndPoint ep = (EndPoint)gip;
                byte[] data = new byte[2048];
                string Msg;
                int recv;
                while (true)
                {
                    recv = cs.ReceiveFrom(data, ref ep);
                    Msg = Encoding.ASCII.GetString(data, 0, recv);
                    string UserName = "";
                    UserName = Msg.Substring(0, Msg.IndexOf(":"));
                    Msg = Msg.Substring(Msg.IndexOf(":") + 1);
                    ReceverichTextBox.Text = ReceverichTextBox.Text + UserName + " : " + Msg + "\n";
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void Sendbutton_Click(object sender, EventArgs e)
        {
            try
            {
                byte[] msg = Encoding.ASCII.GetBytes(UserNametextBox.Text + " : " + SendtextBox.Text);
                SendtextBox.Clear();
                cs.SendTo(msg, SocketFlags.None, gip);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void Connectbutton_Click(object sender, EventArgs e)
        {
            try
            {
                if (GroupIptextBox.Text != "")
                {
                    if (UserNametextBox.Text != "")
                    {
                        if (Connectbutton.Text == "Connect")
                        {
                            cs = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                            IPEndPoint ipe = new IPEndPoint(IPAddress.Any, 7080);
                            cs.Bind(ipe);
                            cs.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(IPAddress.Parse("224.10.0.1")));
                            t = new Thread(new ThreadStart(Receive));
                            t.IsBackground = true;
                            t.Start();
                            panel1.Enabled = true;
                            Connectbutton.Text = "Disconnect";
                            GroupIptextBox.ReadOnly = true;
                            UserNametextBox.ReadOnly = true;
                        }
                        else if (Connectbutton.Text == "Disconnect")
                        {
                            t.Abort();
                            cs.Close();
                            GroupIptextBox.ReadOnly = false;
                            UserNametextBox.ReadOnly = false;
                            panel1.Enabled = false;
                            Connectbutton.Text = "Connect";
                        }
                    }
                    else
                    {
                        MessageBox.Show("Please Enter Your Name...", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
                else
                {
                    MessageBox.Show("Please Enter Group Ip...", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}

Translation Using Google API



Google.API.Translate.TranslateClient tc;
tc = new Google.API.Translate.TranslateClient("http://www.translate.google.com");
Msg = tc.Translate(Msg, Google.API.Translate.Language.English, Google.API.Translate.Language.Hindi);

Exmple of BindingNavigator & DataGridView




//Drag DataGridView,TextBox & BindingNavigator
//Assign name as per this code
//====================================================================
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
namespace DataGridiVewExample
{
    public partial class Form1 : Form
    {
        OleDbConnection con = new OleDbConnection();
        OleDbCommand com = new OleDbCommand();
        OleDbDataAdapter adp;
        DataTable dt = new DataTable();
        CurrencyManager cm;
        BindingSource bs = new BindingSource();
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'dataGridDataSet.DataTab' table. You can move, or remove it, as needed.
            this.dataTabTableAdapter.Fill(this.dataGridDataSet.DataTab);
            try
            {
                con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=DataGrid.mdb";
                con.Open();
                com.Connection = con;
                com.CommandText = "Select * from DataTab";
                adp = new OleDbDataAdapter(com);
                adp.Fill(dt);
                bs.DataSource = dt;
                dataGridView1.DataSource = bs;
                textBox1.DataBindings.Add("Text", bs, "Name", true);
                cm = (CurrencyManager)this.BindingContext[bs];
                bindingNavigator1.BindingSource = bs;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
        }
        private void bindingNavigatorMoveFirstItem_Click(object sender, EventArgs e)
        {
            cm.Position = 0;
        }
    }
}

Store Photo in Sql Server



byte[] ReadImageToBytes(String sPath)   ///This Metod Conver the Photo IN Binary
{
    byte[] data = null;
    FileInfo finfo = new FileInfo(sPath);
    long numBytes = 0;
    numBytes = finfo.Length;
    FileStream fstream = new FileStream(sPath, FileMode.Open, FileAccess.Read);
    BinaryReader br = new BinaryReader(fstream);
    data = br.ReadBytes((int)numBytes);
    fstream.Close();
    return data;
}
private void SaveImage(string comstr, string ImagePath)
{
    try
    {
        byte[] imageSampleData = null;
        SqlCommand com = new SqlCommand(comstr, con);
        imageSampleData = ReadImageToBytes(ImagePath); //From Here The Method For Conver Image To Byte Can Be Call
        com.Parameters.Add(new SqlParameter("@Image", (object)imageSampleData));//Here Image Can Be Stored Into Database
        com.ExecuteNonQuery();
        com.Parameters.Clear();
    }
    catch (Exception ex)
    {
        throw ex;
    }
}

Create Database Class Library



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.IO;
namespace Mesoft
{
    namespace DatabaseActivities
    {
        public class SqlServerActiviteis
        {
            #region "Variable Declration"
            private SqlConnection con = new SqlConnection();
            private SqlDataReader rdr;
            private string ConnectionString = "";
            #endregion
            #region "Constructores"
            public SqlServerActiviteis(String ConnectionString)
            {
                this.ConnectionString = ConnectionString;
            }
            #endregion
            #region "Methods"
            public Boolean OpenConnection()
            {
                try
                {
                    con.ConnectionString = ConnectionString;
                    con.Open();
                    return true;
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            public Boolean CloseConnection()
            {
                try
                {
                    if (con.State.ToString().Equals("Open"))
                    {
                        con.Close();
                        return true;
                    }
                    else
                    {
                        throw new Exception("Connection is already close.");
                    }
                }
                catch (Exception ex
                {
                    throw ex;
                }
            }
            private SqlCommand CreateCommand(string comstr, string ImagePath)
            {
                try
                {
                    byte[] imageSampleData = null;
                    SqlCommand com = new SqlCommand(comstr, con);
                    imageSampleData = ReadImageToBytes(ImagePath); //From Here The Method For Conver Image To Byte Can Be Call
                    com.Parameters.Add(new SqlParameter("@Image", (object)imageSampleData));//Here Image Can Be Stored Into Database
                    com.ExecuteNonQuery();
                    com.Parameters.Clear();
                    return com;
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            private SqlCommand CreateCommand(string ComStr)
            {
                try
                {
                    SqlCommand com = new SqlCommand();
                    com.Connection = con;
                    com.CommandText = ComStr;
                    com.ExecuteNonQuery();
                    return com;
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            public SqlDataAdapter CollectTable(string Query)
            {
                try
                {
                    SqlDataAdapter adp = new SqlDataAdapter(Query, con);
                    return adp;
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            public DataTable FillData(string Query)
            {
                try
                {
                    DataTable dt = new DataTable();
                    SqlDataAdapter adp = new SqlDataAdapter(Query, con);
                    adp.Fill(dt);
                    return dt;
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            public DataSet FillData(string Query, string TableName)
            {
                try
                {
                    DataSet ds = new DataSet();
                    SqlDataAdapter adp = new SqlDataAdapter(Query, con);
                    adp.Fill(ds, TableName);
                    return ds;
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            public SqlDataReader ReadData(string comstr)
            {
                try
                {
                    SqlCommand com = new SqlCommand(comstr, con);
                    rdr = com.ExecuteReader();
                    return rdr;
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            public object GetScalar(string Query)
            {
                try
                {
                    SqlCommand com = new SqlCommand(Query, con);
                    return com.ExecuteScalar();
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            public Boolean ExecutQuery(string comstr)
            {
                try
                {
                    CreateCommand(comstr);
                    return true;
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            public Boolean ExecutQuery(string comstr, string ImagePath)
            {
                try
                {
                    CreateCommand(comstr, ImagePath);
                    return true;
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            byte[] ReadImageToBytes(String sPath)   ///This Metod Conver the Photo IN Binary
            {
                byte[] data = null;
                FileInfo finfo = new FileInfo(sPath);
                long numBytes = 0;
                numBytes = finfo.Length;
                FileStream fstream = new FileStream(sPath, FileMode.Open, FileAccess.Read);
                BinaryReader br = new BinaryReader(fstream);
                data = br.ReadBytes((int)numBytes);
                fstream.Close();
                return data;
            }
            public Boolean Login(string UserId, string Password, string TableName, string WhereUserIdField, string WherePasswordField)
            {
                try
                {
                    if (UserId != "")
                    {
                        if (Password != "")
                        {

                            object temp = null;
                            temp = GetScalar("Select count(*) from " + TableName + " Where " + WhereUserIdField + "='" + UserId + "' AND "
                                 + WherePasswordField + "='" + Password + "'");
                            if (temp.ToString() == "1")
                            {
                                return true;
                            }
                            else
                            {
                                throw new Exception("No User Avilable.");
                            }
                        }
                        else
                        {
                            throw new Exception("Please Enter Password.");
                        }
                    }
                    else
                    {
                        throw new Exception("Please Enter Userid");
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            #endregion
        }
    }
}