Tuesday, 18 December 2012

How to Prevent the Focus on datagridview columns in c#.net windows applications

Some times we need to prevent(avoid) the focus on some columns in datagridview,mostly these requirment for ReadOnly columns for that i gave some code for that,here which column is required for prevent the focus those columns are should be ReadOnly and write below code in CellEnter Event in datagridview.

 private void dgv_CellEnter(object sender, DataGridViewCellEventArgs e)
        {
            if (dgv.CurrentRow.Cells[e.ColumnIndex].ReadOnly)
            {
                SendKeys.Send("{tab}");
            }

        }
See below image:
Here Sl.No and Debit columns are ReadOnly so these two columns are out of focus.


I hope this code will help you.
Read more ►

Monday, 19 November 2012

What is Event handling in C#.net and How it is work

1)The Event handling is nothing but  executing some code whenever the user performs an action.
2)The Required code is to be return in a special method the method is that called as "Event handler".
3)The Event handler should be define in the Form class.
Syntax:
 private void btnAdd_Click(object sender, EventArgs e)
        {
              //write your code here
        } 
Note1:  1)"btnAdd"  is the control name
             2)"Click"       is the Event name
Note2:  1)sender: sender represents the control based on which the   Events raised.
             2)e: The "e" contains  some additional info about the Event for example in mouse Click event the position of the mouse will be represented  where it is clicked.

Example:
Steps:
1)Goto button control properties
2)Goto events
3)Double click on "Mouse Enter Event"
4)write below code

 private void button1_MouseEnter(object sender, EventArgs e)
        {
            button1.BackColor = Color.DarkRed;
            button1.ForeColor = Color.PowderBlue;
        }

shown below figures:
.Above figure shows form with button

 .Above figure shows form with Mouse Enter event fire(change the button color)
I hope this code will help you.
Read more ►

Sunday, 18 November 2012

How to validate the TextBox it takes only Numerics and special charecters in C#.net windows applications

Some times in our Projects we need Validation for TextBox it takes only Numbers and special charecters for that i gave some RealTime dotnet snippet shown below snippet.
Code snipppet:
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;
namespace Blogcode
{
    public partial class txtnumeric : Form
    {
        public txtnumeric()
        {
            InitializeComponent();
        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if(char.IsLetter(e.KeyChar))
            {
                e.Handled=true;
            }
        }
    }
}



    I hope this code will help you
Read more ►

Friday, 16 November 2012

How to add the Image Column and Image to DataGridview control

As we know DataGridView is more important and more use full in realtime windows applications.And this control and classes are Designed to be more flexible and also Extensible           
And We can add an Image control in a column of DataGridView.Setting the column Image property results in that image being displayed by default for all the cells in that column.
The following C# program shows how to add a Image in column of a DataGridView control.

 figur1 shows form and datagridview:

code snippet:
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;

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

        private void btnDgv_Click(object sender, EventArgs e)
       {
            dataGridView1.ColumnCount = 3;
            dataGridView1.Columns[0].Name = "Product ID";
            dataGridView1.Columns[1].Name = "Product Name";
            dataGridView1.Columns[2].Name = "Product Price";

            string[] row = new string[] { "1", "Product 1", "1000" };
            dataGridView1.Rows.Add(row);
            row = new string[] { "2", "Product 2", "2000" };
            dataGridView1.Rows.Add(row);
            row = new string[] { "3", "Product 3", "3000" };
            dataGridView1.Rows.Add(row);
            row = new string[] { "4", "Product 4", "4000" };
            dataGridView1.Rows.Add(row);

            DataGridViewImageColumn img = new DataGridViewImageColumn();
            Image image = Image.FromFile("image Path");//Here we have to give the image path
            img.Image = image;
            dataGridView1.Columns.Add(img);
            img.HeaderText = "Image";
            img.Name = "img";

        }
    }
}
figur2 shows Result of the form:

I hope this code will help you
Read more ►

Thursday, 8 November 2012

How to add the Multiple SplitContainers in a Single form

Basically the SplitContainer devides the form Two Separate Panels,Some times we need to add the One more SplitContainer control to existed Panels  of SplitContainer1 ,as well as multliple SplitContainers.Here problem is Showing the Childforms.Why because?The Adding of the panels and show the ChildForm in  the Last Added SplitContainer Panel.

shown Below figure:


In SplitContainer Major Property is Orientation

Orientation:It is nothing but Placing the SplitContainer Horizantally (or) Vertically shown above figure.

For Example let us Discuss about above figure

In the figure SplitContainer1 it's Pane2 having the SplitContainer2 In Horizantal Orientation and also SplitContainer2 having two Panels Panel1(Pink color),Panel2(Light blue color)
Aftet that our task is showing  the ChildForm in the SplitContaner2-Panel2.For that first we have to add the SplitContainers by coding.

CodeSnippet:

     private void splitContainer1_Panel2_Paint(object sender, PaintEventArgs e)
        {
         SplitContainer1.Panel2.Controls.Add(SplitContainer2);
         }

  Note:  Above code the SplitContainer2 is added to SplitContainer1-panel2


private void SplitContainer2 _Panel2_Paint(object sender, PaintEventArgs e)
        {
           SplitContainer2.Panel2.Controls.Add(SplitContainer3);
        }

  Note:  Above code the SplitContainer3 is added to SplitContainer2-panel2       

In the Below code we showing how display the ChildForm in the middle of the form
 OrganizationPrflFrm frm = new OrganizationPrflFrm();    //creating the object for OrganizationPrflFrm
 frm.MdiParent = this;
 frm.Left = (this.spltcntnr3MdiFrm.Width - frm.Width) / 2;        //setting the width for the ChildForm
 frm.Top = (this.spltcntnr3MdiFrm.Height - frm.Height) / 2;    //setting the Height for the ChildForm
 spltcntnr3MdiFrm.Panel1.Controls.Add(frm);    //adding the form to SplitContainer3-panel1
 frm.Show();                                                    //Showing the ChildForm

Get More Details
Read more ►