Thursday, May 5, 2016

How to add Edit and Delete Button Links in DataGridView.


In This Article I will Explain How to add Edit and Delete Button Links in DataGridView.


In My previous post ,DataGridview will looks like this.



Now  i want to add link buttons to datagridview by using  C#                                                                       

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.SqlClient;

namespace DatagridviewExample
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            displayDataGridView();

            DataGridViewLinkColumn Editlink = new DataGridViewLinkColumn();
            Editlink.UseColumnTextForLinkValue = true;
            Editlink.HeaderText = "Edit";
            Editlink.DataPropertyName = "lnkColumn";
            Editlink.LinkBehavior = LinkBehavior.SystemDefault;
            Editlink.Text = "Edit";
            dataGridView1.Columns.Add(Editlink);

            DataGridViewLinkColumn Deletelink = new DataGridViewLinkColumn();
            Deletelink.UseColumnTextForLinkValue = true;
            Deletelink.HeaderText = "delete";
            Deletelink.DataPropertyName = "lnkColumn";
            Deletelink.LinkBehavior = LinkBehavior.SystemDefault;
            Deletelink.Text = "Delete";
            dataGridView1.Columns.Add(Deletelink);

        }


        public void displayDataGridView()
        {


            SqlConnection con = new SqlConnection("Data Source = CHINNU;Initial Catalog = dotnetdb;Uid = sa;Password = password123;");
            {
                SqlCommand cmd;
                cmd = new SqlCommand("select * from Employee", con);
                cmd.CommandType = CommandType.Text;
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                da.Fill(ds);
                dataGridView1.DataSource = ds.Tables[0];

                dataGridView1.AutoGenerateColumns = false;
                dataGridView1.AllowUserToAddRows = false;
                int i = 1;
                foreach (DataGridViewRow row in dataGridView1.Rows)
                {
                    row.Cells["SNO"].Value = i;
                    i++;
                }


            }

        }
    }
}

OutPut:
 - See more at: http://www.dotnetsharepoint.com/2013/07/how-to-add-edit-and-delete-buttons-in.html#sthash.gR8HmUik.dpuf

Fill combobox items from Database to Windows Forms Application in C#

How to fill Combobox Items from Database in Windows Forms Application in C#.
Database

For filling the ComboBox, First have to create a table in Microsoft SQL Server Database as shown below.
Creating a table
When creating a table make Identity Specification as true for column 'ID'.

Set the table name as shown below.
Adding a ComboBox Control to the Windows Form
Firstly you need to add a ComboBox control to the Windows Form from the Visual Studio ToolBox as shown below.

Form Design
Namespaces
You will need to import the following namespace.
using System.Data.SqlClient;
Adding Items to ComboBox from Database in C#
Below is the sample code for Adding Items to ComboBox from Database.
C#
public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            FillCombobox();
        }

        protected void FillCombobox()
        {
            string conString = @"server=YOUR SERVERNAME; database=YOUR DATABASE NAME; uid=***; password=*****;";
            SqlConnection conn = new SqlConnection(conString);
            DataSet ds = new DataSet();
            try
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand("select ID,EmployeeName from Employee group by ID, EmployeeName", conn);
                SqlDataAdapter da = new SqlDataAdapter();
                da.SelectCommand = cmd;
                da.Fill(ds);              
                comboBox1.DisplayMember = "EmployeeName";
                comboBox1.ValueMember = "ID";
                comboBox1.DataSource = ds.Tables[0];
            }
            catch (Exception ex)
            {
                //Exception Message
            }
            finally
            {
                conn.Close();
                conn.Dispose();
            }
        }
    }
Result
I hope this page will helps to fill ComboBox Items in Windows Forms Application in C#. Thanks.

Adding a Control to a form Programmatically

Introduction:
In this article I explain how to Add a control to a form programmatically. This is useful for developer who is not using VS.NET IDE. The System.Windows.Forms namespace contains a number of types that represent common GUI widgets. Let us see those things in detail.
Creating a Form:
Now first let us see a construction of simple form.
The example: Simple.cs
using System;using System.WinForms;public class MyForm : Form
{
public MyForm()
{
this.Text = "Welcome India!!!";
}
public static void Main(string[] args)
{
MyForm f = 
new MyForm();
Application.Run(f);
}
}
The Output:


Adding Controls to Forms:
The Windows Forms framework summarize native Win32 APIs and exposes secure, managed classes for creating Win32 client-side applications. The Windows Forms class library offers a lot of controls such as buttons, check boxes, drop-down lists, combo boxes, data grid, and other client-side functionality.
To add controls in the form the First step is declare the required private member variables. After that configure the look of each control in the Form's constructor or in the IntializeComponent () method using properties, methods and events. Finally add the controls to the Form's controls collection using control property.

Now let us see an example program Formexample:

In this example as I said previously, first I declare the private Button btncont,& TextBox TBox member variable. Then I constitute the Properties of the Button control,TextBox control in the InitializeComponent(). Then I build a delegate for the click event of a button. The argument to the constructor contains a reference to the method that performs the event handling logic.The btncont.Click + = handler; registers the delegate with the Click event of the button.The Form class has a Controls property that is a collection of child controls. The Add method adds your control to that collection.
The example: Formexample.cs
using System;using System.ComponentModel;using System.WinForms;using System.Drawing;public class Formexample :Form
{
private Button btncont = new Button();private TextBox TBox = new TextBox();
Formexample ()
{
InitializeComponent();
}
private void InitializeComponent()
{
EventHandler handler = 
new EventHandler(btncontClick);
btncont.Text = "Click Me!!";
btncont.Anchor = AnchorStyles.TopLeft;
btncont.Click += handler;
TBox.Location = 
new Point(85, 0);
TBox.Size = 
new Size(150, 50);
TBox.TabIndex = 1;
ClientSize = 
new Size(250, 250);this.Controls.Add(btncont);this.Controls.Add(TBox);
}
private void btncontClick (object sender, EventArgs e)
{
this.BackColor = Color.IndianRed;
TBox.Text = "You Click the Button[Arungg]";
TBox.BackColor=Color.Cyan;
}
public static void Main(string[] args)
{
Application.Run(
new Formexample());
}
}
csc /r:System.WinFOrms.dll /r:System.dll /r:Microsoft.Win32.Interop.dll /r:System.Drawing.dll Formexample.cs
The Output:


In another way by using a rapid application development (RAD) environment such as Visual Studio.NET the application development is very much simplified. In the Visual Studio Windows Forms designer, the controls appear in a toolbox and are dropped on a design surface. This is accompanied by automatic code generation.
The forms designer has a property window that displays properties and events. Every Windows Forms application is a class that derives from System.WinForms.Form.invokes the Run method of the System.WinForms.Application class in its Main method,with an instance of the form as the argument. The Application.Run method processes messages from the operating system to the application.

Upload/Display Image in Picture Box Using C#

Introduction: 

Browsing and displaying an image in picture box tool using  Windows Forms application is a very simple task. In this application, we use the following classes/controls to do the b.

- OpenFileDialog 
- PictureBox
- TextBox
- Button 


Main Functions: 

This functions simply perform the following steps: 
1. Open a file dialog box so that a user can select an image from his/her machine
2. Browse the image 
3. Display selected image in a picture box on a Form 
4. Display image file path in text box 

Here is the code: 
// open file dialog 
 OpenFileDialog open = new OpenFileDialog(); 
 // image filters
 open.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp)|*.jpg; *.jpeg; *.gif; *.bmp"; 
 if (open.ShowDialog() == DialogResult.OK)
{
   // display image in picture box
   pictureBox1.Image = new Bitmap(open.FileName);
    // image file path
    textBox1.Text = open.FileName;
  } 

What is an image filter? 

Image filter is basically a filter that restrict user to select only valid image file. You can remove image filter.